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.
That's it.
So what do I mean by that?
Well, we've seen this before, right where I do print and then let's say name 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 it doesn't have access to, it's going to throw
a name error.
Or variable is not defined here 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 100, 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 a 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 ten.
If I do this and I say X and I click Run.
I have ten 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 you in that one.
Bye bye.