WEBVTT

00:00.180 --> 00:00.500
Great.

00:00.540 --> 00:05.700
You now have a basic understanding of variables and functions all along the course.

00:05.700 --> 00:09.720
You will use them extensively and develop a better understanding with time.

00:09.720 --> 00:15.990
But there is one last important thing to understand about variables, and this is the scope of a variable.

00:16.350 --> 00:19.170
Depending on where you declare a variable.

00:19.170 --> 00:25.800
For example, if it's declared here or here, well, it may or may not be accessible from other parts

00:25.800 --> 00:26.880
of the program.

00:26.880 --> 00:29.100
And this is what we call the scope.

00:29.100 --> 00:35.550
So as you can see in this example, I create a variable named number one, and that's outside of any

00:35.550 --> 00:36.240
function.

00:36.420 --> 00:38.340
You can see this is outside of anything.

00:38.340 --> 00:41.970
And then I use number one here in this void set of function.

00:41.970 --> 00:48.450
But now you can see that I also create a variable A, but I create the variable A In the setup function,

00:48.450 --> 00:52.020
I create a variable A between those two curly brackets here.

00:52.620 --> 00:56.940
And let's see what will happen if I try to access this variable.

00:56.940 --> 00:58.230
A in the void loop.

00:58.230 --> 01:07.500
For example, if I do serial print ln with a like this, let's see what's going to happen if I try to

01:07.500 --> 01:08.280
compile.

01:09.220 --> 01:15.310
I'm going to have an error saying that A was not declared in the scope.

01:16.360 --> 01:18.610
What if I try to print number one?

01:19.120 --> 01:23.440
If I print number one, you see that it is going to work.

01:23.800 --> 01:29.950
But if I print a, I get this error not declared in the scope.

01:30.100 --> 01:31.290
So what is the scope?

01:31.300 --> 01:34.330
Basically with C++ and Arduino.

01:34.510 --> 01:37.810
The scope will just be something that's inside curly brackets.

01:38.020 --> 01:44.410
So when you create a new function, you will open and close curly brackets and later on with conditions

01:44.410 --> 01:45.160
and other stuff.

01:45.160 --> 01:48.000
You will also see that we open close curly brackets.

01:48.010 --> 01:55.540
And so when you have this here, it means that whatever is inside those curly brackets is inside a nested

01:55.540 --> 02:01.290
scope, a more nested scope than the global scope, which has no curly brackets by default.

02:01.300 --> 02:08.080
And so when you create a variable here in a more global scope and you use it in a more nested scope,

02:08.080 --> 02:09.220
then it's working.

02:09.430 --> 02:10.990
The variable is accessible.

02:10.990 --> 02:15.610
As you can see here, we have a more nested scope and we can access to number one.

02:15.610 --> 02:23.200
But here you can see I create the variable in this scope, so I can't use it outside of this scope.

02:23.680 --> 02:29.410
And you can see to go from the setup to the loop, we have to go out of the curly brackets and then

02:29.410 --> 02:31.600
we have to go inside another scope.

02:31.600 --> 02:38.230
So another set of curly brackets and that means that the variable A will not be accessible in this scope

02:38.230 --> 02:46.630
here, the variable A when it's created here, it will be accessible only in this scope and if you create

02:46.630 --> 02:47.230
another scope.

02:47.230 --> 02:52.150
So basically, if you have another set of curly brackets like this with conditions and stuff that we're

02:52.150 --> 02:56.650
going to see later, then you can access the variable here, no problem.

02:57.280 --> 03:01.720
But you cannot access it outside of the scope where it is created.

03:01.720 --> 03:09.160
So you cannot go out of the curly brackets and then expect that you can use this variable basically,

03:09.160 --> 03:10.030
that's the rule.

03:10.030 --> 03:16.420
So if you want to have a variable that you can use, for example in both setup and loop, you need to

03:16.420 --> 03:18.220
make it a global variable.

03:18.220 --> 03:23.080
So in the global scope like this, you can see it's not in any curly brackets.

03:23.080 --> 03:27.430
And as you could see with this example, the number one, we can use it actually in the setup and we

03:27.430 --> 03:28.810
can use it in the loop.

03:28.810 --> 03:34.510
But any variable that you create in the setup, you can only use it in the setup and not in the loop.

03:34.930 --> 03:40.450
And there is another thing is you can see that here we have so in this function and in that function

03:40.450 --> 03:46.120
we have a parameter for the function and this parameter for the function here, when you have this for

03:46.150 --> 03:51.730
function, this is also going to be a nested scope and we call that a local variable.

03:52.030 --> 03:58.030
We call that a local variable to the scope, which means that here the number, so the variable int

03:58.030 --> 04:02.380
number is going to be created here when you call the function.

04:02.380 --> 04:07.660
So the value that you pass to the function is going to create a new variable with this value.

04:07.660 --> 04:12.190
And this variable will only exist within the curly brackets here.

04:12.220 --> 04:17.500
After the curly brackets, the variable doesn't exist anymore, so you can't get access to it.

04:17.500 --> 04:22.360
And so that's why you can see that basically we have for example, int number here and we have int number

04:22.360 --> 04:22.810
here.

04:22.810 --> 04:25.750
We have the same name, but that's not a problem.

04:25.750 --> 04:30.340
It's not going to be a collision because of course you cannot create two variables with the same name.

04:30.340 --> 04:34.270
That's going to be a collision issue and you will have an error.

04:34.270 --> 04:40.180
But if the two variables are in the two different scopes, you can see here we have one scope and another

04:40.180 --> 04:42.910
scope because we have different set of curly brackets.

04:42.910 --> 04:47.290
So there is no collision, there is no problem between those scopes.

04:47.290 --> 04:49.480
You can create any variable you want here.

04:49.660 --> 04:53.500
You will not be able to access them in this scope.

04:53.500 --> 04:58.180
So basically what you can do is you can create the same name for the same variables, just like we did

04:58.180 --> 05:00.880
here into number and int number.

05:01.480 --> 05:03.610
And I want you to talk about the scope right now.

05:03.610 --> 05:09.340
I know it's a bit more complex, but I want you to talk about it now just so you can get a starting

05:09.340 --> 05:14.860
of an understanding because that's a very important concept and also because, well, you will probably

05:14.860 --> 05:20.290
have some errors when you start to program and some of those errors are going to be because of the scope.

05:20.290 --> 05:25.720
So when you have an error because of something like the scope, well, you can come back to this lesson

05:25.720 --> 05:27.670
and things will make more sense.

05:27.670 --> 05:31.510
You will understand more and maybe be able to fix your error.

05:31.540 --> 05:31.960
All right.

05:31.960 --> 05:37.570
So to recap on the scope, when you create a variable, pay attention to the scope.

05:37.720 --> 05:45.520
A variable is only accessible in its own scope or in any nested scope inside the scope.

05:46.030 --> 05:50.710
Thus, you could have different variables with the same name in different scopes.
