Go Down

Topic: initialize variables inside void setup() or before it? (Read 2926 times) previous topic - next topic

sellonoid

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().

PaulS

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.

Jantje

sellonoid
A reference to the page would be welcome.
Best regards
Jantje
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 -

retrolefty


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


sellonoid


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.

Jantje



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
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 -

sellonoid


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

Go Up