Is there any difference or advantages if globals are declared before setup() or between setup() and loop()? For that matter, can they be declared after loop()?
The main rule here, is you should declare them before you use them.
You can definitely declare a global value after setup() and before loop(), if setup does not use it. i have done so, and it works.
After loop( ), I can't say from personal experience. But the main rule is, you should declare it before you use it. I suppose you can, if the variable is only accessed from functions which are after loop( ) in your code.
Easier to find if all are identified at the top of the sketch. I use this format:
/* program notes */
any #includes and #defines
global variables & pin definitions
ISRs if used (interrupt service routines) and
other functions (I don't write many functions)
void setup
void loop
I haven't tried mixing the placement up. When I've tried compiling others code that did, I think I got errors that pertained to code not being part of setup, loop, or a function, but that may have been for actual code and not variable declarations.
rickso234:
Is there any difference or advantages if globals are declared before setup() or between setup() and loop()? For that matter, can they be declared after loop()?
Why would you want to ?
globals can be declared anywhere in global scope, even after loop().
The issue is, if you do that, you won't be able to use them in loop(), but you will be able to use them in any functions below where you declare them.
There is no advantage to their placement, and like others have said, people generally put them under the #include block.
Was curious so I tested.
int int1 = 1;
int int2 = 2;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print(int1);
Serial.print(" ");
Serial.println(int2);
outsideLoop();
delay(500);
}
void outsideLoop() {
Serial.print(int1 + 5);
Serial.print(" ");
Serial.println(int2 + 5);
}
Above worked as expected printing 1 2 then 6 7.
So lets move to between void setup and void loop
void setup() {
Serial.begin(9600);
}
int int1 = 1;
int int2 = 2;
void loop() {
Serial.print(int1);
Serial.print(" ");
Serial.println(int2);
outsideLoop();
delay(500);
}
void outsideLoop() {
Serial.print(int1 + 5);
Serial.print(" ");
Serial.println(int2 + 5);
}
Again works just fine.
So lets declare them below below void loop
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print(int1);
Serial.print(" ");
Serial.println(int2);
outsideLoop();
delay(500);
}
int int1 = 1;
int int2 = 2;
void outsideLoop() {
Serial.print(int1 + 5);
Serial.print(" ");
Serial.println(int2 + 5);
}
Utoh, problems.....
Arduino: 1.6.4 (Windows 8.1), Board: "Arduino Uno"
testingVariPlacement.ino: In function 'void loop()':
testingVariPlacement:5: error: 'int1' was not declared in this scope
testingVariPlacement:7: error: 'int2' was not declared in this scope
'int1' was not declared in this scope
So void loop at least needs globals declared before it or they are out of scope.
Lets comment out the bits in void loop and see if the function outside of void loop will run.
void setup() {
Serial.begin(9600);
}
void loop() {
// Serial.print(int1);
// Serial.print(" ");
// Serial.println(int2);
outsideLoop();
delay(500);
}
int int1 = 1;
int int2 = 2;
void outsideLoop() {
Serial.print(int1 + 5);
Serial.print(" ");
Serial.println(int2 + 5);
}
It prints 6 7 to screen as expected.
Now what if the global variables are at the very bottom of the code?
We get the outside of scope errors again.
So to sum up global variables must be declared above/before they are ever used.
Pretty much. There are ways, however, though not particularly useful here, for example:
extern int g_int1;
extern int g_int2;
void loop() {
int test = g_int1 + g_int2;
}
int g_int1 = 5;
int g_int2 = 3;
tammytam:
Pretty much. There are ways, however, though not particularly useful here, for example:
But the extern has to be before the void loop() so the variable is at least mentioned in the code prior to that right?
void setup(){
}
void loop() {
int test = g_int1 + g_int2;
}
extern int g_int1;
extern int g_int2;
int g_int1 = 5;
int g_int2 = 3;
That generates the out of scope error too.
So something that declares the variable or tells the compiler 'hey this variable is declared somewhere else' has to be in the code before the variable can be used yes?
Yes. I mean without a mystic ball or a lot of luck there is no way the compiler knows what your global is unless its defined before use ![]()
Thanks everyone... didn't think this post would have had such "global" interest!
Modern pre-compilers are not as sensitive to order of things; however, us humans prefer to see things in specific order.
I use:
- #includes
- #defines
- #pragmas
- EXTERNS
- CONSTANTS
- GLOBALS
- setup()
- loop()
- FUNCTIONS
Google's Style Guide says thus:
- Typedefs and Enums
- Constants (static const data members)
- Constructors
- Destructor
- Methods, including static methods
- Data Members (except static const data members)
I see a lot of code in headers in libraries for Arduino. That was a no-no last century and sort of drives me nuts.
$0.02,
Jon