WEBVTT

00:00.380 --> 00:09.380
In this lecture I want to go over a very important concept called scope scope is the region of program

00:09.380 --> 00:13.280
that you can access and use variables.

00:13.730 --> 00:21.450
So far we've had our variables only in our main function like this x variable rate here.

00:22.010 --> 00:30.230
So X it can only be used in this main function it can't use anywhere else in the program so I couldn't

00:30.230 --> 00:41.460
have X being used in this other function that I called func x is x here.

00:42.390 --> 00:48.140
And all so c the compiler is complaining to me.

00:48.140 --> 00:54.170
So symbol X could not be resolved right.

00:54.430 --> 01:03.310
So it doesn't understand what this symbol x is because X is local only to this main function can't use

01:03.310 --> 01:05.770
X anywhere else.

01:06.230 --> 01:06.500
Can.

01:06.770 --> 01:10.250
So this is called a local scope.

01:10.280 --> 01:14.080
So X has only local scope it's local to me.

01:14.090 --> 01:20.190
Only so we saw another example of this in fact with the for loop.

01:20.190 --> 01:29.670
So we had our FOR loop in we declare the inside for loop and we say I'm less than you know some number

01:29.770 --> 01:38.480
say and then I plus plus and I said we couldn't do something like this.

01:38.610 --> 01:43.500
Can't say I is this

01:45.750 --> 01:56.660
is I Andel because I is local to only the for loop.

01:56.690 --> 02:04.300
It's not you can't use I anywhere else sides inside this for.

02:04.430 --> 02:13.240
And so it's local it's for live only just like x' local only to the main function.

02:13.240 --> 02:26.150
Now we could have X be outside of the main function like this we could declare it in or define it up

02:26.150 --> 02:29.340
here and then.

02:29.360 --> 02:31.350
So let's just get rid of this.

02:31.700 --> 02:33.170
Then we could have

02:35.680 --> 02:41.800
the X being used in this function here.

02:42.050 --> 02:49.840
And all a year is to distinguish it.

02:50.120 --> 02:53.120
It run it and it says X equals 5.

02:53.120 --> 02:54.420
That's this one right here.

02:54.430 --> 02:55.420
So this line.

02:55.550 --> 02:57.620
And then it says phunk X is 5.

02:57.620 --> 03:00.380
So it actually did work.

03:00.410 --> 03:07.700
So when you declare or define a variable outside of any function.

03:07.700 --> 03:17.640
So here we have it above main and this phunk This is called global scope.

03:17.890 --> 03:21.400
So it's global to the entire program.

03:22.530 --> 03:28.580
And you may be asking why not just use global scope for all variables.

03:28.590 --> 03:32.070
Well this is usually a bad idea.

03:33.080 --> 03:42.670
In if a function modifies a global variable x here and you're not aware or you forgot maybe you wrote

03:42.670 --> 03:45.090
it a little while ago and you can't remember.

03:45.250 --> 03:48.310
You might get some very strange results.

03:48.340 --> 03:57.200
You don't usually want variables being modified without us understanding why it's being modified and

03:57.330 --> 04:02.160
all variables really make things harder and it's hard to reason about.

04:02.200 --> 04:10.030
So we're never going to be using global variables in any of our programs and I suggest you don't use

04:10.030 --> 04:14.510
it either in the C really understand how things really work.

04:14.590 --> 04:20.780
So these will be seeing some other examples of scope in some upcoming lectures.
