In order to finalize our understanding of functions we have to talk about scope.
Another one of our key terms what is scope let's have a look
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 error a name error 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
name there or a variable is not defined our 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.
Anytime 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 underline 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 spot.
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 that one by.