i have a few questions on variable scope and the differences between arduino and c++.
first any variable declared outside of all functions is a global variable. i totally get that.
a variable declared inside a function is a local variable that can only be accessed by that function. this is where it gets fussy.
for example
void loop()
{ int bob; // this declares an integer bob
int steve = 5; // this declares the integer steve and sets it to five}
if i were to run this code the declaration of bob as void loop repeats would not affect the value of bob after a value is stored.
but would steve be redeclared with the value 5 every time void loop repeats?
if it does always reset to 5 how can variables get an initial value inside a local scope without declaring them globally?
First of all, why not write a little test program to try it?
if i were to run this code the declaration of bob as void loop repeats would not affect the value of bob after a value is stored.
Not so. Bob has a random value every time loop() is called, because local variables which are not declared static are not initialized.
but would steve be redeclared with the value 5 every time void loop repeats?
if it does always reset to 5 how can variables get an initial value inside a local scope without declaring them globally?
The variable steve is initialized to 5 every time loop is called(). I don't understand the question, could you try again?
vini_i:
i have a few questions on variable scope and the differences between arduino and c++.
There's no difference. There is no such thing as the "Arduino language." It is C++.
vini_i:
a variable declared inside a function is a local variable that can only be accessed by that function. this is where it gets fussy.
There is no fuss.
vini_i:
if i were to run this code the declaration of bob as void loop repeats would not affect the value of bob after a value is stored.
No. Each time loop() repeats, bob gets re-initilized. If you happen to be seeing the same value every time, you're getting lucky as the same memory location keeps getting reused. This behavior is no guaranteed.
vini_i:
but would steve be redeclared with the value 5 every time void loop repeats?
That's exactly what you told the code to do.
vini_i:
if it does always reset to 5 how can variables get an initial value inside a local scope without declaring them globally?
i guess what i'm asking, is there a way to keep variables local to void loop without them being screwed up by a redecoration when void loop repeats?
or is the only good way to work with void loop is using global variables.
(from what i have read about c++; int main is where one would declare local variables and int main does not repeat unless the programmer makes it repeat with some sort of a loop where as long as the variable declarations are kept outside that loop there is no problem and the variables stay local to int main)
vini_i:
i guess what i'm asking, is there a way to keep variables local to void loop without them being screwed up by a redecoration when void loop repeats?