Go Down

Topic: variable scope arduino vs c++ (Read 609 times) previous topic - next topic

vini_i

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

Code: [Select]
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?

billroy

First of all, why not write a little test program to try it?

Quote

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.

Quote

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?

-br


James C4S

#2
May 01, 2013, 04:35 am Last Edit: May 01, 2013, 04:45 am by James C4S Reason: 1

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


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.



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.


but would steve be redeclared with the value 5 every time void loop repeats?

That's exactly what you told the code to do.


if it does always reset to 5 how can variables get an initial value inside a local scope without declaring them globally?

The keyword, a C++ keyword, is static.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

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?

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)

James C4S


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?



The keyword, a C++ keyword, is static.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Go Up