Possible error in documentation of Function declaration

In the documentation concerning function declaration it says

"Our function needs to be declared outside any other function, so "myMultiplyFunction()" can go either above or below the "loop()" function. "

with code examples showing functions declared after the loop function. I thought this was a bit strange because in every other language I've used the function had to be declared and defined before using it. Sure enough, when I placed the function definition after the loop function I got error messages. I moved them to a location before the setup function and after all my const declarations and these error messages went away.

In C, a function needs to be declared before it can be used. It does NOT have to be defined where it is declared, though.

You can define function prototypes before referencing the function, and then define the function later, after loop(), for instance.

The IDE takes care of creating function prototypes for you, if you don't create them, IN MOST CASES.

There are things that can cause the function prototypes to be placed in the wrong place, and there are some functions that the IDE can not figure out what the prototype should look like.

You need to post your code, so we can see why the function prototypes were not created, or were not created in the proper place.

You'll need to post examples.

But you knew that.

PaulS:
The IDE takes care of creating function prototypes for you, if you don't create them, IN MOST CASES.

Right. So you can define functions after you call them, in the source code. But I don't think that happens with declarations. So that's probably the problem, if the OP has declarations that follow their use. But, yeah, need to look at it.

I've been using VB for the last few years. I forgot that in C you could put a declaration at the beginning of the code and put the definition elsewhere. I never liked that and always put the full definition up front. The example code didn't show a leading declaration.

I've attached the code as currently configured. It's incomplete but at least it compiles without errors.

Thanks for putting up with me.

Coal_Creek.ino (5.43 KB)

I moved setup() and loop() above your functions:

/*
  Coal Creek Interlocking
 
 \ s4b
 s2___b2________s4a-4 ______\t5-5________________________________________s9-9______ _______b4________________
 \                     /
 \t6-6               /t8
 s1___b1________   ____________________\_________________/_______________s10a-10_____ _______b3________________
 s3-3                               \t7-7
 \ s10b
 
 Data Assignments
 Address Pins
 Strobe  Pini
 Byte0   Pins
 Byte1   Pins
 
 Address  Data Byte
 OUTPUTS
 DisSignal_1       0         0
 DisSignal_2       0         1
 Signal_3          1         0
 Signal_4a         1         1
 Signal_9          2         0
 Signal_10a        2         1
 Signal_10b (d)    3         0, bit 0-3
 Signal_4b (d)     3         0, bit 4-7
 Lever_Locks       3         1
 
 INPUTS
 Levers            4         0
 Blocks            4         1
 
 */

const int Normal = 0;
const int Reverse = 1;

const byte Proceed = 0;          // R/G
const byte Approach_Medium = 1;  // Y/G
const byte Clear_Medium = 2;     // R/G
const byte Approach = 3;         // Y/R
const byte Restricting = 4;      // R/Y
const byte Stop = 5;             // R/R

const byte Top_Red = 1;          //Define bits for aspect colors
const byte Top_Yellow = 2;       //These can be added to produce the signal display
const byte Top_Green = 4;
const byte Bottom_Yellow = 16;
const byte Bottom_Green = 32;
const byte Bottom_Red = 64;

const int Distant_Signal_1 = 0;
const int Distant_Signal_2 = 1;
const int Signal_3 = 2;
const int Signal_4a = 3;
const int Signal_4b = 4;
const int Signal_9 = 5;
const int Signal_10a = 6;
const int Signal_10b = 7;

const int Lever_3 = 1;    //Turnout
const int Lever_4 = 2;    //Turnout
const int Lever_5 = 4;    //Signal
const int Lever_6 = 8;    //Signal
const int Lever_7 = 16;   //Signal
const int Lever_8 = 32;   //Signal
const int Lever_9 = 64;   //Turnout
const int Lever_10 = 128; //Turnout

const int Block_1 = 0;
const int Block_2 = 1;
const int Block_3 = 2;
const int Block_4 = 3;

const int Turnout_5 = 0;
const int Turnout_6 = 1;
const int Turnout_7 = 2;
const int Turnout_8 = 3;

unsigned long Lever_Lock_Timer[] = {
  0, 0, 0, 0, 0, 0, 0, 0}; //Turnout release delay time in milliseconds

byte GLevers;                     //Evaluated in Setup and must pass to Loop
byte GBlocks;                     //Evaluated in Setup and must pass to Loop


void setup()
{
  // put your setup code here, to run once:
  // Assign Pin Functions
  //Address
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(17, OUTPUT);
  pinMode(18, OUTPUT);
  pinMode(19, OUTPUT);
  pinMode(20, OUTPUT);
  pinMode(21, OUTPUT);  //strobe
  //Output byte 0
  pinMode(22, OUTPUT);
  pinMode(23, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
  pinMode(27, OUTPUT);
  pinMode(28, OUTPUT);
  pinMode(29, OUTPUT);
  //Output byte 1
  pinMode(30, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(32, OUTPUT);
  pinMode(33, OUTPUT);
  pinMode(34, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(36, OUTPUT);
  pinMode(37, OUTPUT);
  //Input byte 0
  pinMode(38, INPUT);
  pinMode(39, INPUT);
  pinMode(40, INPUT);
  pinMode(41, INPUT);
  pinMode(42, INPUT);
  pinMode(43, INPUT);
  pinMode(44, INPUT);
  pinMode(45, INPUT);
  //Input byte 1
  pinMode(46, INPUT);
  pinMode(47, INPUT);
  pinMode(48, INPUT);
  pinMode(49, INPUT);
  pinMode(50, INPUT);
  pinMode(51, INPUT);
  pinMode(52, INPUT);
  pinMode(53, INPUT);

  //Initialize railroad
  //Read Railroad

  int Return_Data;
  Load_Address (4);              //Levers in byte 0, blocks in byte 1
  Return_Data = Read_Input();    //Returns the values of the input pins 38 - 53 as an integer
  GLevers = Return_Data & 255;   //Extract byte 0, Position data on all 8 levers
  GBlocks = Return_Data & 65280; //Extract byte 1

}

void loop() 
{
  // put your main code here, to run repeatedly:
  //
}

void Load_Address (byte Address) {
  //Sets the address register, pins 14 - 20
  int i;
  int j;
  int Address_Pin;
  for (i = 0; i < 7; i++) {
    Address_Pin = i + 14;
    if (bitRead(Address, i) != 0) {
      digitalWrite(Address_Pin, HIGH);
    }
    else {
      digitalWrite(Address_Pin, LOW);
    }
  }
}

int Read_Input() {
  //Returns the values of the input pins 38 - 53 as an integer
  int i;
  int Result = 0;
  int Start = 38;
  for (i = 0; i < 16; i++) {
    if (digitalRead(i + Start) != 0) {
      Result = Result + bit(i);
    }
  }
  return Result;
}

byte Set_Aspect_Colors (byte Aspect_Colors) { //Aspect_Colors enters as a value of 0 to 5
  switch (Aspect_Colors) {
  case Proceed:
    Aspect_Colors = Top_Green + Bottom_Red;
  case Approach_Medium:
    Aspect_Colors = Top_Yellow + Bottom_Green;
  case Clear_Medium:
    Aspect_Colors = Top_Red + Bottom_Green;
  case Approach:
    Aspect_Colors = Top_Yellow + Bottom_Red;
  case Restricting:
    Aspect_Colors = Top_Red + Bottom_Yellow;
  case Stop:
    Aspect_Colors = Top_Red + Bottom_Red;
  default:
    break;
    return Aspect_Colors;  //Aspect_Colors is returned with the sum of the bit values
  }
}

void Load_Output (byte Data0, byte Data1) {
  //Load output data in two bytes on pins 22 - 29 and 30 - 37
}

void Strobe() {
  digitalWrite(21, HIGH);
  delay(1);
  digitalWrite(21, LOW);
}

Compiles fine for me.

Could we maybe see the code that didn't compile?