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
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.
If your function has variables, do you need to list out each one?
Updated:
Sorry for not making my question clearer. If i have a function which accepts variables, I’m assuming i need to include that in the declaration and it needs to be after i have declared the individual variables?
int i, x, tempVariable;
//Can i declare it here?
void SomeFunction();
//Does it need to have the variable included?
void SomeFunction(int i, int x);
void setup()
{
}
void loop()
{
SomeFunction(i,x);
}
void SomeFunction(int i, int x)
{
tempVariable = i + x;
}
Variables which are only declared and used inside a function do not need to be forward-declared outside the function – only if you want to share a global variables or use call a function, it needs to be known nbefore-hand.
Aha. You’re talking about parameters of a function, not a variable on the stack of the function or something.
Yes the function signature is the return type, the function name, and all parameters (in C++).
In a forward declaration it is however optional to include the names of the parameters, but should be done anyways to get good intellisense.
If you have
void SomeFunction(int i, int x)
{
tempVariable = i + x;
}
then
void SomeFunction(); //wrong, actual parameters not include
void SomeFunction(int i, int x); //right
void SomeFunction(int, int); // still correct, but less info for IntelliSense
As a rule of thumb, simply copy the first line of the function defintion (or generally the function head) and put a semicolon after it.
Leaving the parameter list empty when there are parameters in C is bad style in my opinion. You should always use the full function signature for the forward declaration.