Lỗi sort was not declared in this scope năm 2024

Are you getting the error: ‘YourVariable’ was not declared in this scope? What is variable scope anyway?

Lỗi sort was not declared in this scope năm 2024

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.

![](https://i.ytimg.com/vi/LMmutCMfHHQ/maxresdefault.jpg

auto)

Are you getting the error your variable was not declared in this scope. What is this variable scope anyway? why are you getting this error? Pretty sure doesn't have anything to do with mouthwash but I could be wrong. Well, in this lesson we are going to talk super, basically about variable scope. There's a lot of technical details that you could dive into. But in this lesson we're going to try to explain the very basics of variable scope to help you get this error fixed, stay tuned. (upbeat music) Subscribe to our YouTube channel to get more videos like this. Roughly speaking variable scope has to do with where you can use a variable that you have defined. So let's take a look at an Arduino program and talk about some sections. So here we are in the Arduino IDE and we're looking at pretty much just a bare sketch. We've got some space up here. We've got a function called setup and we have a function called loop. If I create a variable inside setup I can't use that variable in the loop. Let me demonstrate this. So I created a variable an integer called dog breath inside setup and I'm trying to set the value of dog breath in the loop but I'm getting the error dog breath not declared in this scope. Likewise, if I create a variable in the loop and try to use it in the setup, I'll have the same issue. Let me demonstrate that. (upbeat music) Here I can only use the cat breath variable inside the loop. I can't use cat breath up and set up. If I create my own function and create a variable inside that function it can't be used outside of that function. Let me demonstrate this. (upbeat music) So here I made a function called my function and I created an integer variable called giraffe breath inside that function. If I try to use that giraffe breath variable inside another function like loop I get that error was not declared in this scope. 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 its curly braces. They don't even know it exists. The loop function here has no idea that giraffe breath was created and initialized over in my function. Same with setup. If we tried to use giraffe breath in set up set up has no idea that giraffe breath was already initialized in my function because giraffe breath would be out of scope in setup. Let me show you another example here. If I create a variable and then I put curly braces around it and I try to use that variable outside of its curly braces I get the error not declared in scope. It's kind of like these curly braces are force fields holding in your variable. The curly braces are setting 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 the curly braces after it. So let's do a little demonstration here. (upbeat music) So here We're inside the loop and we created a variable called fresh breath. So what is the scope of fresh breath? The scope of fresh breath are within it's curly braces. So we've got, it was defined inside the loop. So here's the opening curly brace of the loop. And here's the closing curly brace of the loop. So fresh breath can be used anywhere inside of the loop. So even though we have a for loop here right here so this is a for loop function. Notice that this has curly braces right here fresh breath can be used inside of these curly braces. So this for-loop can see fresh breath. So fresh breath can get in, but any variable defined inside this for-loop couldn't get out. For example, you'll see in our for-loop we are actually initializing a variable a counter variable here called I if I tried to use I outside of the four loop, for example here, I would get that error. I was not declared in this scope because I'm trying to use I outside of I scopes I scope is limited to those curly braces. Now let's say I added an if statement in here. (upbeat music) So now you can see I'm using I, which was created in the for-loop inside of this nested if statement. So I can go into these curly braces and be used but anything we created in this if statement couldn't be used outside of that hopefully you get the gist there. Now, what if we did something crazy? What if we created a variable outside of any curly braces? What would the scope be then? This is what we call global scope. A variable global scope known as a global variable can be used anywhere in your program. Let's show a quick example of this. (upbeat music) Here our global variable genie breath which has been created outside of any curly braces can be used by any function after where it's initially defined. So we can use genie breath in set-up. We can use genie breath in loop and we can use genie breath in this user defined function we made my function. Now, if we had genie breath and instead of defining it above void set up, but instead we defined it below and then we compiled, we would get the error again. And it would say the error here is on line five. That genie breath was not declared in scope. And that's because we've declared this global variable after it first shows up here in set up. So you want to define your global variables to be used by any function at the very top of your sketch. Now you might be tempted to think that using global variables is the way to go since you can use them everywhere. It seems to make things a whole lot easier. And for a really small program yeah, you can get away with a couple of global variables, but as your programs grow in complexity, you really want to try to put a kibosh on your global variable use. And there's a bunch of reasons not to use global variables too much. But the biggest argument is probably that trying to track down bugs with a ton of global variables just is extremely difficult to do so try to use global variables sparingly. So if you're getting that error, variable not declared in the scope think about where are you creating your variable? Are you trying to access that variable out of its scope kind of track down where those curly braces are? Is it nested inside some other statement? And you're trying to use it outside of that. These are the questions you need to ask. Now it may be that this lesson created more questions than it did answers. If that's the case, I highly recommend checking out programming electronics academy. We have training that walks step-by-step through details just like this. You can check it out at programmingelectronics.com. Thanks a lot. And I hope you have a great day. Bye. (soft music)

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

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; } 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…