In order to finalize our understanding of functions, we have to talk about scope, another one of our
key terms.
What is scope?
Scope is something present in a lot of programming languages and scope simply means what variables do
I have access to?
So what do I mean by that?
Well, we've seen this before, right, where I do print and then let's say, Nate, if I click run.
I'll get an air, a name, air name, name is not defined because, well, it doesn't exist, right?
And up to this point, we haven't really discussed this, but this is because of scope.
What variables do I have access to the Python interpreter says, hey, what do I have access to?
And when we use something that it doesn't understand or doesn't have access to, it's going to throw
a nightmare.
Or variable is not defined, air and scope in Python has what we call functional scope or function scope.
What does that mean?
Well, up until now, when we create a variable like total equals to a hundred, this is part of what
we call global scope.
That means anybody on this file has access to this total variable.
I can use this inside of a conditional block.
I can use it inside of a function.
It has global scope.
But when I say Python has function scope.
What I mean is that when we create a function and let's say we create total as some or let's say some
func.
And we create total inside of the function, if I print total here and I click run.
I get the same thing, name total is not defined, this is because of Python's function scope, any
time we create a variable, if it's not inside of a function.
Then it is part of the global scope, we have access to it, but you see over here we get that red underlined,
undefined name total.
If let's say, for example, I had a condition if true.
Then X equals to 10 if I do this and I say X and I click run.
I have 10 because, well, although I have indentation.
The only time I create a new scope, you can think of this as a new universe that we spawn.
Well, we can only do that when we define a function like this, so think of scope as a new world that
we create in our case, when we create a function, we create a new world that anything that's indented
inside of the function is its own world that we don't really have access to.
We can only use total if we indent print.
And it's part of this world.
That's what scope is, who has access to who.
So let's take a break here and I want to play a little game in the next video where we try and figure
out who has access to who.
I'll see in that one by.