Order of function declaration


#1

Hello everyone, and thank you for the great work, I am very happy to enjoy coding in a real advanced IDE !
I am facing a problem that did not happen with the Arduino IDE though…

It seems that the order I declare functions is having an impact on compilation, as this works perfectly :

 #include <Arduino.h>
void setup(){}
void bb(){
  // any code here
}
void loop(){
  bb();
}

but this does not compile :

 #include <Arduino.h>
void setup(){}
void loop(){
  bb();
}
void bb(){
  // any code here
}

src\Blink.cpp:17:6: error: ‘bb’ was not declared in this scope
bb();
^
*** [.pioenvs\digispark-pro64\src\Blink.cpp.o] Error 1

Both code work in Arduino IDE

Thank you for your help :slight_smile:


#2

This is why.

Strictly speaking C requires that functions be forward declared for the compiler to compile and link them.

In the first example you have not only declared the function but also defined it in the same space.
This is perfectly fine and legal for proper C.

The second example has the function definition after the loop but no declaration before the setup() function.

In order to get the compiler to “see” the bb() function you must declare it before the setup function.

This is valid and will compile.

#include <Arduino.h>

void bb();

void setup(){}
void loop(){
  bb();
}
void bb(){
  // any code here
}

Now the compiler knows that you have a function named bb() defined elsewhere in the file. its prototype ( i.e returning void and taking no parameters) being declared ahead of its actual usage will allow the compiler to compile.

Arduino framework by itself does this job for you behind the scenes. thats why it compiles fine in the Arduino IDE.


#3

Thank you very much for this rapid, clear, and complete answer =)