filmov
tv
Variable Scope and fixing error: '_______' was not declared in this scope?

Показать описание
🤩 FREE Arduino Crash Course 👇👇
Want to learn more? Check out our courses!
***Get the code, transcript, challenges, etc for this lesson on our website***
We designed this circuit board for beginners!
SHOP OUR FAVORITE STUFF! (affiliate links)
---------------------------------------------------
We use Rev Captions for our subtitles
Arduino UNO R3:
Budget Arduino Kits:
Multimeter Options:
Helping Hands:
Soldering Stations:
AFFILIATES & REFERRALS
---------------------------------------------------
FOLLOW US ELSEWHERE
---------------------------------------------------
VARIABLE SCOPE | FIX ERROR: ‘YOURVARIABLE’ WAS NOT DECLARED IN THIS SCOPE? | SOLVED
Are you getting the error: ‘YourVariable’ was not declared in this scope? What is variable scope anyway?
Arduino not declared in variable scope error message
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.
VARIABLE SCOPE IN LAYMAN’S TERMS
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.
NESTED VARIABLE SCOPE
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 you can get inside an curly 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;
}
}
In this example, freshBreath can be used anywhere inside its scope, including the for loop.
GLOBAL SCOPE
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;
}
CONTINUED…
***About Us:***
This Arduino lesson was created by Programming Electronics Academy. We are an online education company who seeks to help people learn about electronics and programming through the ubiquitous Arduino development board.
***We have no affiliation whatsoever with Arduino LLC, other than we think they are cool.***
Want to learn more? Check out our courses!
***Get the code, transcript, challenges, etc for this lesson on our website***
We designed this circuit board for beginners!
SHOP OUR FAVORITE STUFF! (affiliate links)
---------------------------------------------------
We use Rev Captions for our subtitles
Arduino UNO R3:
Budget Arduino Kits:
Multimeter Options:
Helping Hands:
Soldering Stations:
AFFILIATES & REFERRALS
---------------------------------------------------
FOLLOW US ELSEWHERE
---------------------------------------------------
VARIABLE SCOPE | FIX ERROR: ‘YOURVARIABLE’ WAS NOT DECLARED IN THIS SCOPE? | SOLVED
Are you getting the error: ‘YourVariable’ was not declared in this scope? What is variable scope anyway?
Arduino not declared in variable scope error message
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.
VARIABLE SCOPE IN LAYMAN’S TERMS
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.
NESTED VARIABLE SCOPE
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 you can get inside an curly 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;
}
}
In this example, freshBreath can be used anywhere inside its scope, including the for loop.
GLOBAL SCOPE
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;
}
CONTINUED…
***About Us:***
This Arduino lesson was created by Programming Electronics Academy. We are an online education company who seeks to help people learn about electronics and programming through the ubiquitous Arduino development board.
***We have no affiliation whatsoever with Arduino LLC, other than we think they are cool.***
Комментарии