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

preview_player
Показать описание
🤩 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.***
Рекомендации по теме
Комментарии
Автор

As a long time programmer, this is one of the best explanations of variable scope I’ve seen. You should show an example of declaring two variables with the same name in different scopes to show they are separate variables. It would have been nice to see an example of passing variables and an example to go with the part about global variable problems in larger programs. These examples would help explain scope in using variables in included functions. I guess you can only squeeze so much into these videos.

Excellent video, I would like to have seen it when I was first starting. I will pass it along to new programmers like my son.

bassman
Автор

Massive help. I was having a scope error issue with a function and didn't understand. I was missing a closing bracket but would not have found it without your help. Thank you so much.

stl
Автор

So with that last example genieBreath when you put it in between the first and the second function, it would be good for the last two functions right and not for the first one right?

RicardoPenders
Автор

i love this video your explanation is so simple and its quite easy to understand

kurtdecena
Автор

Another great video, interesting how the If or While allows you to pass an external local variable. Passing variables might have been a nice inclusion to this video.

DodgyBrothersEngineering
Автор

I've been banging my head against the wall. This really helps!

ADHDbuilt
Автор

Thank you so much dude, this video help me a lot

techtalk
Автор

LIGHT BULB 🙈🙈🙈🙈🙈

Sometimes when you don't know what you don't know you can spend days or weeks trying to fix some code that should be really simple. I think this could have been the reason many MANY attempts at making some Arduino code failed! 🙈

Thanks for the video, it makes perfect sense, now 🤪

ryansmithza
Автор

Sir, How can I add vertical lines between curly bracket in arduino ide, i mean settings

rithikadithi
Автор

When my problem is solved my heart rejoices

lexxatech
Автор

hello why positive not declared ? how to fix it ?

izzathafiz
Автор

yeah what if i create my variables outside the loop and setup, you know making them global and i still get the same error ??

ntontouu
Автор

i was frustrated for 25 minutes, it was giving me this error. and then later i realized that i forgot to put the F in scan.

jamworthy
Автор

Can you tell me why I am getting result1 and result2 "not declared in this scope in function void loop()"?

double value1 = 5.5;
double value2 = 4.4;
double result1;
int result2;

void setup(){
Serial.begin(9600);
}
void loop(){
result1 = (value1 + value2);
result2 = (value1 + value2);
Serial.println(" ");
Serial.print(result1);
Serial.print(" ");
Serial.println(result2);
delay(500);
}

Evelisk
Автор

it telling mine like this, Its a simple button to turn on and off the led. ' ledPin ' was not declared in this scope

tomatoasmr
Автор

char.cpp: In member function 'void CHARACTER::ApplyPoint(BYTE, int, bool)':
char.cpp:4757:8: error: 'APPLY_ATTBONUS_SHADOW' was not declared in this scope; did you mean 'APPLY_ATTBONUS_SHAMAN'?
4757 | case APPLY_ATTBONUS_SHADOW:

RBJxDeathRepare
join shbcf.ru