Are you getting the error: ‘YourVariable’ was not declared in this scope? What is variable scope anyway?
Isn’t Scope a brand of mouthwash? I mean, what gives?
In this lesson, we are going to talk SUPER BASICALLY about variable scope. There are so many technical details we could dive into, but this lesson is going to try and help you understand variable scope enough to help you fix this error.
Roughly speaking, variable scope has to do with where you can use a variable you have defined. Let’s take a look at an Arduino program and talk about some sections.
If I define a variable inside the setup function, I can only use that variable in the setup. Trying to use that variable in the loop would get me the error message…
void setup() int dogBreath; // Defined here > void loop() dogBreath = 7; // Error. not declared in this scope >
If I define a variable inside the loop function, I can only use that variable in the loop. Trying to use that variable in the setup, I get the error message…
void setup() catBreath = 5; // Error. not declared in this scope > void loop() int catBreath = 10; // Defined here >
If I create my own function, and I define a variable inside that function, it can only be used in that function. Were I to use it in another function, like setup or loop, I’ll get that error! Can you begin to see how variable scope is working?
void setup() > void loop() giraffeBreath = 63;// Error. not declared in this scope > void myFunction() int giraffeBreath; // Defined here >
Can you see how the curly braces sort of compartmentalize our variables? If I define a variable inside curly braces, I cannot use that variable outside of those curly braces. Other functions can’t see the variable outside of it’s curly braces, they don’t even know they exist!
I mean check this out. If I put curly braces around a variable declaration, and then try to use that variable outside the curly braces, I get that error.
void setup() int hippoBreath; // Defined here > hippoBreath = 9; // Error. not declared in this scope >
It’s kind of like the curly braces are force fields – holding in your variable. The curly braces set the scope of the variable.
Now here is where it gets interesting…
If you create a variable outside of and before a set of curly braces, that variable can “get into” curly braces after it…
Let’s do a little demonstration here:
void loop() int freshBreath = 0; // Defined here for (int i = 0; i & lt; 3; i++) freshBreath += i; // used inside curly braces here > >
In this example, freshBreath can be used anywhere inside its scope, including the for loop.
Now what if we did something crazy…What if we create a variable outside of any curly braces! What is the variable scope then?
This is what we call global scope. A variable with global scope, known as a global variable can be used anywhere in your program.
int genieBreath = 8; // Defined here void setup() genieBreath = 1; > void loop() genieBreath = 898; > void myFunction() genieBreath = 21; >
Now, you might be tempted to think that using global variables is the way to go, since you can use them everywhere – seems to make things easier. For a really small program, yes, you can get away with a couple global variables, but as your programs grow in complexity, you really want to limit global variable use.
There’s a bunch of reasons not to use global variables too much, but a big argument against their use is that using global variables can make your code far more difficult to debug. So use global variables sparingly…
George W Shaiffer on May 17, 2021 at 11:03 am
Thanks Michael
That helps a lot < I think. Still letting my stiff brain digest the scope error. The importance of the curly braces will
help. I'm a little aware of global vs local variables but only in the context of the HP42s and HP 48–HP 49 series
calculators (revealing my age). The later series where much harder to program for a couple of reasons.
Internally they were actually algebraic calculators with a poor RPN front end pasted on. Then as somewhat for a similare
reason the programming language is very much like C. This was, I believe, because all of the latter 48-49 design engineers
went through school their entire lives with only T I calculators. By then the rea; reason for RPN had been lost! A tremendous
amount of time and money along with several different prototype calculators were built and tested by a range of skilled users
inside HP before the decision that the Polish mathematician Jan Lucasiewicz was on to something with his proof about all
machine math except he listed operations first follow by the data. HP's prototype studies showed that data on a stack followed
by the operation(s) worked better for the test subjects. Since they had changed the order to data followed by operations one of the
worst mistakes in manufacturing history was made! They had reversed Lucasiewicz 's order and in honor of his contribution they
called it Reverse Polish Notation. At that time Polish and other ethnic jokes were permitted and it worked against the adoption of HP
machines. Mathematicians did not like the lack of parenthesis and the = sign not realizing that that very lack forced students to be more
aware of order of operations not less! On the bright side of this I adopted and required the latest HP calculators in my classes/program
and taught programming of them (in RPN mode). At least one student near completion of his degree wanted to take a C class but had not
taken the prerequisite course. He/we were able to get the prerequisite waived and his report back to me was that anyone who had been
though my 48/49 class did not need the prerequisite. His comment was that working full time limited his access to college computers so
he would do the C assignments on his 48 then when on campus would quickly translate that program into the college computer. Very minimum
changes were required according to him, I have retrieved my HP 48 and 49 machines from storage and been again struggling with programming
the beasts to hopefully aid me in Arduino programming. Sorry for the length of this rant….hope it is not to time consuming or boring. gws
Michael James on May 17, 2021 at 11:14 am
Love this history George – thanks for sharing!John Vankoeveringe on May 28, 2021 at 4:30 pm
Although still a little hazy,it does clear things up a lot.Paul on July 13, 2022 at 1:11 pm
Is it me or does the line that says : “…… that variable can get into you can get inside an curly after it…” just not make any sense? Totally threw me.
Michael James on July 13, 2022 at 7:14 pm