[no audio]
Friends, here we are going to
discuss about scope of the variables for
your functions.
So before starting with the scope of the variables, let me
show you how to call a function from another function.
So I have taken a script name as 'scope_of_the_variables'.
Right. Fine.
Now, what I am doing is I'm going to define a function.
Let's say simply I'm calling
'myfunction1'. You can take any function names based on
your rules.
Let's say I want to display
[no audio]
"Welcome to functions".
Right. So at the end just try to write 'return None'. That's it.
Now, I'm going to define one more function, 'myfunction2'.
[no audio]
"Thank you".
Then just write 'return None'. That's it.
Now you have two functions in your code.
So to execute, to get the output from your functions, what
we have to do is, we need to call them, right. Suppose
I am going to call 'myfunction1'.
So this is the way how we can call your functions, right. See
that. You're getting your output from first function.
Now, if you want to execute your code which is there in second
function as well, what we have to do
is, we can call your second function.
[no audio]
That's it.
But sometimes while going forward while writing real-time
scripts, you may need to call your function from inside of
another function.
Yes, you can call it.
Let's say I want to call 'myfunction2' from 'myfunction1',
so I am calling from my function.
[no audio]
That's it. You're getting your output.
"Thank you". Right. So from function one, I'm calling function
two. Yes, you can call any function from any place.
Right. Fine.
Now, what I am doing is, and one more thing guys, if you observe,
now this function is in your 'main' code, right?
If you observe it is outside of your function.
[no audio]
But while going forward in your real time, right, maximum
cases we won't write any logic outside of a function each and
everything even suppose sometimes if you have a single line
we'll try to write that line in terms of a function.
That's a good practice, right?
That's fine.
[no audio]
Now see that, what I am doing is, I am writing some 'x=10',
and I want to print that 'x' value from your function
one. Let's say, "x value from fun1 :", just I'm trying to
print 'x' value. At the same time
let me print the 'x' value,
[no audio]
"x value from fun2: ". Yes. Now, let me run it and see the
output. You are getting your output.
[no audio]
Right. Now I am commenting this.
[no audio]
And in function one, I am defining 'x = 10'. Not only
10, you can take any value.
Let's say, 60. Now see the result.
Observe the output. From your function one you are getting
value as 60, but while executing from function two you are
getting an error called, name 'x' is not defined.
So this is called scope of the variables, means if any variable
is defined inside of a function, by default that variable
is specific to that function only. You can't access that variable
outside of that function.
So I defined 'x = 60' here, and I'm trying to display 'x'
value from your second function.
It won't work.
It is specific to this function, or this is a local variable
for this function.
But if you observe, previously we did this one, right. Let's
say first, let me comment this.
[no audio]
Whenever if any variable is
defined outside of a function that variable you are able
to access from function 1 and function 2. That means if any
variable is defined outside of a function, then that is
global variable.
So by default if you define any variable outside of a function,
see nowhere we have here,
we don't have any function here, right?
We are calling, but 'x = 10' in the 'main' code, it is not
under any function. Then this 'x' is by default global variable.
Global variable means, you can access that variable anywhere
inside of your function, inside of your script.
Right. Now what we are doing.
If any variable is defined inside of a function, then that
variable is specific to your function only.
Right, now we have two types of variables, local and global variables.
Right. First thing, scope means this is the meaning for your scope
of the variable.
So based on this scope, we have local and global variables.
Now I can say this is a global variable.
[no audio]
And now if I enable this. Let's say this is local.
[no audio]
Now I'm running. See the result what you are getting.
[no audio]
So in 'main' code I have taken 'x = 10', and in 'myfunction1'
I am defining 'x = 60'. Now, whenever if we take same
variables in the 'main' code, and in the function, they are
not same. Now this 'x' is different, and this 'x' is different.
That's why while executing from 'myfunction1',
what is the result you are getting? 60 value, because
you defined 60 here.
But from function two we don't have any 'x' variable here, then
what your Python will do means,
is there any global variable related with 'x', then we'll try to
print that 'x', but before that in case if we have a local variable,
now see the result, you are getting 40.
So your function will give, your Python will give priority
for your local variables inside of your function
while trying to display your 'x' variable value. If 'x' is not
defined inside of your function,
I mean, if no local variable 'x', then what your Python will
do means, it will try to find is there any 'x' as a global variable?
If it is there you will try to print that variable value.
If it is not there, then it will throw an error. See that, now
I am commenting 'x = 10' in your 'main' code.
Now you're getting an error.
That's it. So this is simply scope of the variables.
[no audio]
Right, and last one,
let me explain that. Now while going forward, right,
we are going to write each and everything in terms of functions
only. Now what I will do is,
[no audio]
you can take any function, not
only 'main', but if we take 'main' then somewhat meaningful that's
it. 'main' means
your code will start from this place. That's it.
Other than that nothing is there.
[no audio]
Defining 'x = 10', and calling 'myfunction1',
I'm also taking as under one function, right. Now,
if I run this there is no output because you defined three
functions, and nowhere you are calling the execution point
from where you want to start your code, whether you want to
start from function 2, or function 1, or from main function,
from which place you want to execute your code. As of now,
right, I am feeling that this is my 'main' code. I have to start
execution from this point, that's why I need to call this
function somewhere.
So now I am calling 'main'.
[no audio]
That's it.
Now, whenever if you are trying to call in this way, you're
getting an error called, name 'x' is not defined, from your
'main', in which function? 'myfunction2'.
[no audio]
Right. Now, what I want to do is, as of now if you observe 'x = 10'
is not a global variable, right?
This is under, let me delete this.
Now, this is under the 'main' function. 'main' is also one function
like 'myfunction'.
So instead of 'main' you can also take 'mycodestartsfromhere'.
[no audio]
You can take anything, but just we are following. Now
we have to right here, instead of 'main' you have to write
this one. That's it.
Now see the result. Same thing.
[no audio]
So instead of this, just generally all are following like
this. 'main', that's it. Now, instead of this, 'main'. Right. 'main' is
also like one function.
Now if you observe inside of your 'main', 'x = 10' is there.
Now, this is a function, right. Now 'x = 10' is local, local
for this 'main' function.
You can't access now 'x = 10' from outside of your function,
'main' function, or any of your functions.
I defined 'x = 60',
that's why from my 'myfunction1'.
I can able to access 'x = 60', but in 'myfunction2' I
don't have 'x' value. Then how can I get it?
That's why you're getting an error. Now whatever the variable
is there in 'main' function, 'x = 10', that variable
value I want to access from 'myfunction2'. Then how? In case
if it is there here, then that is the 'main' function,
I mean in main logic you have, now you're getting your 'x' value
from function 2 as 10, but now 'x = 10' is there in 'main'
function, but I want to access this 'x = 10' from 'myfunction2'.
[no audio]
Right. Then in 'main' function,
I mean whatever the function you have, in that function you
have to write 'global x'.
That means now 'x' is global.
You can access my 'x' variable anywhere inside of your Python
Script. Now see the result. Now you're getting from function 2, 10.
[no audio]
Fine. But guys making your variable as global is not always good.
Okay. Sometimes you can take it as a, variable as global,
but instead of global we have one more way to pass your
corresponding variable to your required function.
Right. See that. Now, I am going to comment from here.
[no audio]
Don't get confused,
okay. We'll discuss this again in next video. As of now just
I am giving the way how you can pass. See now
I'm going to comment this, and as of now if I run my code
what you are getting?
From 'myfunction1' you are getting your value as 60. Now,
I'm not calling anywhere 'myfunction2'. Now, I want to call
'myfunction2', suppose from 'main' function itself.
Now I am calling, but in 'myfunction2' I don't
have any variable called 'x',
that's why we are getting an error.
I don't want to use global. In case if I use global,
yes, it will work without an error, but I don't want to use
variable as a global.
But I want to send the value of, 'x' value 10 to 'myfunction2',
so other way to pass your value to your required function
is just inside of your parentheses,
just write 'x'.
[no audio]
Now here also you have to write
'x'. Now what will happen? The 10 value, while calling this
'myfunction2' you are also passing, not 'x', you are not passing
'x'. The value of 'x', 10, 10 you are passing.
So the meaning for this is, you are not passing 'x', value of
'x', 10 you're passing to this variable. Now this 'x' and this
'x', they are completely different. In case if you are
having a confusion,
right, first let me run it and see the result.
Now what I am doing, instead of 'x', I'm taking 'y'. Now,
I want to print that 'y'.
That's it.
Right. So other than global, how can you, if I take global
then this variable is,
you can access that variable from any function, from any line
anywhere in your Python Script. But I don't want to access
in all places, only from this function only.
I want to access this variable 'x', that value, not even that
'x', only the value of 'x', then I can send in this way.
That's it. And one more important point is, now this variable and this
variable are completely different. The value of 'x' will be assigned
to 'y', that value we are printing.
That's it. Okay.
So now here, whenever if you pass your variable to your function,
then that is called argument.
And here that is called
[no audio]
parameter. Okay. We will discuss in detail in the next video
about this argument and parameter, okay.
Okay guys, thank you for watching this video.
[no audio]