Great.
You now have a basic understanding of variables and functions all along the course.
You will use them extensively and develop a better understanding with time.
But there is one last important thing to understand about variables, and this is the scope of a variable.
Depending on where you declare a variable.
For example, if it's declared here or here, well, it may or may not be accessible from other parts
of the program.
And this is what we call the scope.
So as you can see in this example, I create a variable named number one, and that's outside of any
function.
You can see this is outside of anything.
And then I use number one here in this void set of function.
But now you can see that I also create a variable A, but I create the variable A In the setup function,
I create a variable A between those two curly brackets here.
And let's see what will happen if I try to access this variable.
A in the void loop.
For example, if I do serial print ln with a like this, let's see what's going to happen if I try to
compile.
I'm going to have an error saying that A was not declared in the scope.
What if I try to print number one?
If I print number one, you see that it is going to work.
But if I print a, I get this error not declared in the scope.
So what is the scope?
Basically with C++ and Arduino.
The scope will just be something that's inside curly brackets.
So when you create a new function, you will open and close curly brackets and later on with conditions
and other stuff.
You will also see that we open close curly brackets.
And so when you have this here, it means that whatever is inside those curly brackets is inside a nested
scope, a more nested scope than the global scope, which has no curly brackets by default.
And so when you create a variable here in a more global scope and you use it in a more nested scope,
then it's working.
The variable is accessible.
As you can see here, we have a more nested scope and we can access to number one.
But here you can see I create the variable in this scope, so I can't use it outside of this scope.
And you can see to go from the setup to the loop, we have to go out of the curly brackets and then
we have to go inside another scope.
So another set of curly brackets and that means that the variable A will not be accessible in this scope
here, the variable A when it's created here, it will be accessible only in this scope and if you create
another scope.
So basically, if you have another set of curly brackets like this with conditions and stuff that we're
going to see later, then you can access the variable here, no problem.
But you cannot access it outside of the scope where it is created.
So you cannot go out of the curly brackets and then expect that you can use this variable basically,
that's the rule.
So if you want to have a variable that you can use, for example in both setup and loop, you need to
make it a global variable.
So in the global scope like this, you can see it's not in any curly brackets.
And as you could see with this example, the number one, we can use it actually in the setup and we
can use it in the loop.
But any variable that you create in the setup, you can only use it in the setup and not in the loop.
And there is another thing is you can see that here we have so in this function and in that function
we have a parameter for the function and this parameter for the function here, when you have this for
function, this is also going to be a nested scope and we call that a local variable.
We call that a local variable to the scope, which means that here the number, so the variable int
number is going to be created here when you call the function.
So the value that you pass to the function is going to create a new variable with this value.
And this variable will only exist within the curly brackets here.
After the curly brackets, the variable doesn't exist anymore, so you can't get access to it.
And so that's why you can see that basically we have for example, int number here and we have int number
here.
We have the same name, but that's not a problem.
It's not going to be a collision because of course you cannot create two variables with the same name.
That's going to be a collision issue and you will have an error.
But if the two variables are in the two different scopes, you can see here we have one scope and another
scope because we have different set of curly brackets.
So there is no collision, there is no problem between those scopes.
You can create any variable you want here.
You will not be able to access them in this scope.
So basically what you can do is you can create the same name for the same variables, just like we did
here into number and int number.
And I want you to talk about the scope right now.
I know it's a bit more complex, but I want you to talk about it now just so you can get a starting
of an understanding because that's a very important concept and also because, well, you will probably
have some errors when you start to program and some of those errors are going to be because of the scope.
So when you have an error because of something like the scope, well, you can come back to this lesson
and things will make more sense.
You will understand more and maybe be able to fix your error.
All right.
So to recap on the scope, when you create a variable, pay attention to the scope.
A variable is only accessible in its own scope or in any nested scope inside the scope.
Thus, you could have different variables with the same name in different scopes.