Pages: [1]   Go Down
Author Topic: initialize variables inside void setup() or before it?  (Read 2424 times)
0 Members and 1 Guest are viewing this topic.
Colorado, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

On the Arduino web page under Language Reference and Structure, I was reading about void setup(). It says, "Use it to initialize variables, pin modes, start using libraries, etc." But the example shows "int buttonPin = 3;" right ahead of void setup() instead of inside it. I know "int buttonPin = 3;" is both a declaration and an initialization of a variable, so strictly speaking it's not just initializing a variable, but it doesn't seem like a clear example of the explanation in the text. I'm posting because it said "corrections, suggestions, ..." should be posted to the forum.
It seems like I should declare variables ahead of setup() and initialize them inside of setup().
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It seems like I should declare variables ahead of setup() and initialize them inside of setup().
From my point of view, the variable should be initialized when it is defined. There are cases where that is not practical, such as when the initialization relies on hardware functioning, but those cases are pretty rare.
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1899
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

sellonoid
A reference to the page would be welcome.
Best regards
Jantje
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It seems like I should declare variables ahead of setup() and initialize them inside of setup().

Keep in mind that more fundamentally where you declare a variable determines where that variable can be used, it's 'scope'. Declared before setup() makes it a 'global' variable able to be referenced anywhere in the sketch, inside setup(), loop(), and any user functions you might define. If you declare it in setup() then the variable is only available inside the setup() function.

Generally making all variable declaration global is not considered a good programming practice as variables declaration should be limited to the function that uses them, and only if variables must be shared by multiple functions should they be declared global. That 'good practice' aside, I tend to think in a micro-controller environment when using small sketches that declaring variables global is not the sin that the experts would have you believe and that there is some flexibility from such ridged rules that one can deviate from.
 

Lefty

Logged

Colorado, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sellonoid
A reference to the page would be welcome.
Best regards
Jantje

Jantje- This is the page: http://arduino.cc/en/Reference/Setup

Thanks to the other posters too for the info.
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1899
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

sellonoid
A reference to the page would be welcome.
Best regards
Jantje

Jantje- This is the page: http://arduino.cc/en/Reference/Setup

Thanks to the other posters too for the info.
Reading through the text I understand your confusion. I do think the text is correct.
It states
Quote
Use it to initialize variables, pin modes, start using libraries, etc.
This does not imply this is the only way to initialize variables.
If you feel it could be worded better give it a try.
Best regards
Jantje
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Colorado, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


This does not imply this is the only way to initialize variables.
If you feel it could be worded better give it a try.
Best regards
Jantje

I wouldn't word the text differently... I'd just change the example. The text says you can initialize variables in setup(), so I would show a variable being initialized in setup(), not before it.

Thanks,
Jim
Logged

Pages: [1]   Go Up
Jump to: