Welcome back.
One last thing I want us to talk about before we round up the functions section
will be the concept of variable scope.
See in Python or in fact in any programming language,
we have both local and global variables.
Variables have the scope which will determine where they can be accessed.
Now, variables that inside of a function will be local to that function
and you cannot access them outside of that function. While variables
that are defined outside of any function are global
as an example. OK, X equals let's say 12,
right?
This right here
is what we refer to as a global variable.
Why? Because it's on its own,
it's not inside any function or any loop or any el
it's on it's own, it's just the X equals 12.
However,
if I created my own function and I said, define underscore modify a variable
brackets, colon.
And now I said X equals three,
the X right here
will be what we call a local variable because it is
inside of the function which is modified on the score variable.
So the global variable of X is 12, the local variable of X is three because again
this three right here, it's inside all the modified VA function.
So
let's take a look at this, I'm going to tell my function to print out the value of X.
And now
I'm going to call the function,
modify variable.
What do you think the value of X here is going to be
the value of X here is going to be three
because we're simply calling the function but the I variable
which already has the value of X to be equal to three. So if I run my function,
you can see it's right there three.
But what if I wanted to call
the global variable X equals to I want to print out that value.
All I need to do is just to call the default
command
a print function for
Python.
And then simply say print X.
Now if I run the program again, you can see three and now 12
again because this print right here, it's not the same print that's on the
modify variable function.
This print right here is meant to produce or print out the value
of X which is the local value that's inside of the function,
modify variable.
However,
this print X right here notice again the indentation,
it is not inside of this function, it is outside.
So the X here is going to be the one that belongs to the global variable, which is 12,
that is the difference between global variables and local variables.
If you wanted to
get this particular value of X, which is three,
you will need to always call the function
that houses or that holds that particular value,
which is of course modify variable.
But the global variable,
you can access it anywhere as long as it's not inside of the function.
However,
we can actually
modify
the value of our global variables inside of a function.
Yes, we can do that. Check this out.
I'm gonna go back to my function in here.
All right.
And just underneath, I'm gonna say global
X
just by using this statement called global,
we can modify
the global value of X.
So now I'm saying global XX equals three.
In this case, right now, we are simply
modifying
the global variable. That's what we're doing right now.
We're simply going to modify
the global variable of X.
So
now
if I called, if I run my program,
you can see right now
that first of all the function modify variable
X equals three will produce three.
And because we have now modified the global variable
of X to now be three instead of 12.
Even when I call the print function that
is outside of the modified variable function,
it's still going to produce three.
Because again, inside of this function, we modified
the global variable X,
we change the value from 12 to 3 by using the keyword global right
here.
So this is how you can also modify
the global variables inside of a local function.
Thank you so much for watching the video. I will see you in the next class.