initialize variables inside void setup() or before it?

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

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.

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

retrolefty:

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

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

Jantje- This is the page: setup() - Arduino Reference

Thanks to the other posters too for the info.

sellonoid:

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

Jantje- This is the page: setup() - Arduino Reference

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

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

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