[no audio]
Friends here we are going to
discuss about the simple functions with arguments.
Let me explain this with an example.
Let me open my editor.
I am taking a script name called
'function_with_simple_arguments.py'.
Right. See, I have a small requirement. Just
assume that that requirement is read a number
and increment it by 10.
So, first I am writing without functions concept.
Let me take 'num=
[no audio]
(input("Enter your number: "))'.
[no audio]
That's it. Then I want to find 'result=num+10'.
[no audio]
Then I want to print that result. 'print("
[no audio]
Your result is :",
something like that.
You can take anything.
[no audio]
That's it. Right. Now,
let me go to my command line and let me run it. Before running,
this is your script.
I am running this script. See the result.
I am passing suppose some 20, then you have to get your output
as 30. Yes, you are getting. Now,
this is a simple Python Script.
But what I want to do is, I want to convert this entire code
in terms of functions.
So, first step what I am doing is, adding 10 to your given
number and then printing, I am writing under one function.
Right. So, you know whenever if you want to define a function,
yes, 'def get_result', that is suppose my function, and under this function
I am writing these two lines, and finally simply write
return 'None'. That's it.
[no audio]
Now, I am reading the, I mean I want to get my number.
Yes, I am getting. Right. Now,
I'm running. I am providing but you're not getting an output.
The reason is, your logic is there under 'get_result' function,
but you are not calling that. If you call, then only that logic
will execute. Now, after reading a number now I want to call that logic.
[no audio]
Right. Now, see the code.
[no audio]
So, you are reading your number. After that
you're calling your logic. Now, we will execute. Now, I am running.
[no audio]
I am giving some 4, you're getting result as 14.
Right. Now, actually to read your number, and to execute your
logic you have two lines. Now these two lines also, I want to
write under one more function.
Yes, you can write it.
Let me do one thing.
[no audio]
'my_code', I'm taking. That's it.
[no audio]
Right. And I'm running. You're not getting any output.
The reason is, now your entire logic, reading a number and
then finding your result, both are there under two different functions,
[no audio]
Right. So, in your 'my_code' you are calling your,
you're reading your number, and then you are calling this function.
[no audio]
Now to execute this logic, you need to call this
function, to execute this logic
you need to call this function, right.
So that's why what I am doing is, somewhere I am calling
'my_code'. That's it.
Now, instead of 'my_code' if we take it as 'main', that's good.
You can take any function name, but this is the simple way
to take your function name as 'main', so that you can easily
understand that 'main' is nothing
but, from there you're going to execute your logic.
So, it's not mandatory to take your function name as 'main',
but if we take then it's good. That's it.
Right. Now, previously we've done our code, and we are
getting our output. Now,
if I do that you will get an error, that is - 'num' is not defined.
Why you're getting this error? Where you are getting this
error? In line number 2.
What is that? While finding 'result', this 'num' variable is unable
to find by your Python.
What is the reason for that?
[no audio]
See 'num', now it is there under 'main' function.
It is one function, right. Under function
you define a 'num' variable.
Then this variable is specific to this function.
How can you access in this way function?
Now this 'num', this 'num' variable, right is specific to this
'main' function,
how can you access inside of 'get_result'?
[no audio]
So that's why what you have to do?
You need to make your variable as global.
[no audio]
Now it will work.
[no audio]
But making your variables as global is not that much good.
Whenever if you have a requirement then only we'll do that.
A requirement means, suppose assume that 'num' variable, suppose
in my function, in my code
I have some 10 functions. In all functions
I need to use suppose
let's say 'num' variable.
Then I can simply make it as global,
so that I can access that 'num' variable in all the functions.
[no audio]
As of now, right,
I need this 'num' variable only inside of my one function,
'get_result' function.
That's why, don't make your variables as global whenever if
there is no requirement.
If you have some specific requirement, then only we will
make it as global.
We will see that situation. Right. So forget about global now.
Then how you can send your 'num' variable to this 'get_result'
function? Simple, while calling you just pass your 'num'.
Now you're passing your 'num' value, right. Now see that.
I am passing that, right. But if I run that you are going
to get an error. That is
'get_result() takes 0 positional arguments, but 1 was given.'
'get_result' is a function.
[no audio]
Right. So here 'get_result' function. Here
nothing is there, but you are sending one value.
So whenever if you are passing your variable value in this
way, here also you should have some variable to store whatever
the value coming from here.
That means here we define a variable, right?
Let me define here also some variable. Then it will match.
Let me take here also 'num', but you can take any variable.
No need to take 'num' also,
You can take any variable like 'value'.
Right. Now, whatever the variable you define, that variable
is available inside of this function.
Right. So as of now, I'm running in this way.
[no audio]
Yes. You are getting. But see, while calling you're passing 'num'
means you are not passing 'num' variable,
you are passing the value of 'num'. Guys from this line
only the value will be passed to here, and here we are capturing
that value in terms of 'num' variable.
Right. Now what I am doing is I am removing, instead of 'num'
I am taking a 'value'.
Now this 'value' variable is available inside of this function.
Now instead of 'num', now you have to take 'value'.
That's it. Now see the result.
[no audio]
It's working.
Right. So, if you pass your variables while calling, then that variables
are called arguments,
and the variable which is defined in your defined function, defined
function means 'def' somewhere here
it is there, no. And this variable is called parameter, parameters
or positional arguments. Why we are calling them as a positional
arguments? See, as of now you have only one variable.
That's why you can't understand
what is the position.
Let me take simply one more code.
[no audio]
That is just for addition.
[no audio]
Let me write. Directly
I'm writing your logic without functions.
Let's say number 'a'.
[no audio]
"Enter your first number :"
[no audio]
Then number 'b',
[no audio]
"Enter your second number :".
[no audio]
Then I am finding 'result = a + b'.
Then I am printing the 'result'.
[no audio]
"The addition of
[no audio]
and is :", your 'result'.
[no audio]
Right. First let me run it and see the output.
I am passing 4 and 5, then you are getting the result as 9.
Right. That's fine.
Now, same way I want to find subtraction as well.
Let's say this is addition result, 'aresult', then subtraction result, 'sresult'
I am finding 'a-b'. Then I want to print, "The subtraction
of
and
is :",
'sresult'.
That's it.
[no audio]
Right. 3, 4, you're getting. What happened?
[no audio]
Okay, here 'aresult'.
Yeah, it's fine.
You're getting. Right. Now, of course
this is a logic without functions.
But now I want to convert this into functions concept. So
reading 'a' and 'b', right, I will do from 'main' function.
So, I am taking this as a 'main' function. Inside of 'main' function
I am reading my 'a' and 'b' values.
[no audio]
Right. Then
I'm going to take these two lines, and I want to give the
name for that as addition, 'get_addition'.
[no audio]
That's it. So I am finding for addition here.
[no audio]
Same way remaining two lines
I want to write one more function,
that is 'get_sub'.
Let me take these two lines.
[no audio]
Let me write finally 'return None'.
And in 'main' function also we have returned 'None'. Yeah, fine. Now,
let me run it. No output because now if you observe you have
three functions, nowhere we are calling any function,
how it will execute?
Right. Anyway, our intention is, our starting logic should be from
here. That's why I need to call this 'main' after
defining all your functions. Somewhere here
I am calling that.
That's it.
Right. Now see.
I'm running. Let me do one thing.
I will remove empty lines.
[no audio]
See, this is your logic. Now, I am running that.
[no audio]
"Enter number", three, four. You're not getting an output.
[no audio]
See, we are reading 'a' and 'b' values. Where we are calling
'get_add', 'get_result'? Nowhere we are calling.
That's why you're not getting any output. Now,
I want to get addition and also subtraction, then I need to
call those functions, right. But here we'll get an error. But obseve that.
[no audio]
While finding addition 'a' and 'b', you are getting
an error called, name 'a' is not defined. Why? See your
'a' and 'b' variables are there under your 'main' function.
That means they're local variable.
They are specific to this 'main' function. How you can use that
in 'get_add' function or 'get_sub' function?
So, if you want to use those variable values you need to pass
them to that function whenever if you are calling.
Right. Now, 'get_add(a,b)',
'get_sub(a,b)'. And variables, whatever the variables
value you are sending, you need to capture here
also. That's why in your defined function
also you have to write 'a,b', then 'a,b'. That's it.
Right. Now, let me run it.
I am giving number 4 and 7, see the result.
[no audio]
Addition 11, subtraction -3. Now what I am doing is, while
getting subtraction
I am passing 'b,a'.
[no audio]
So previously you got result as -3 for 4 and 7. Now,
I am running, I am giving same values, and see the result what
you are getting. '+3' you're getting
because we changed the position. See that.
If we change here position, based on that position only these
values will be assigned to your, 'b' value now you're assigning
to 'a', 'a' value you're assigning to 'b', right.
So based on this position you are getting values here.
That's why these are called positional arguments or simply parameters.
[no audio]
Right. Now you're fine with positional arguments. Right.
and one more thing,
we already discussed that
whenever if you are calling
function, you're passing 'a' and 'b' variables,
and we are assigning them to 'a' and 'b'. You need to understand
here 'a' value will be assigned to this 'a', and 'b' value will
be assigned to this 'b'. Same way this is 'a' value will be assigned
to 'a', and this 'b' value will be assigned to 'b', and these variables
all we are using inside of this, and this 'a' and this 'b'
not from here, they are from this parenthesis. Be clear. This 'a', 'b'
are from this, from this place.
Whatever you define here that variables you can use inside
of your function.
Right. And while calling you're passing 'a,b' right.
No need to take 'a,b'.
You can take some 'p,q' also.
Let me take 'p,q'.
So, you're taking 'p' and 'q'. Now on there are no more 'a' and 'b'.
Now variables are 'p' and 'q'. Now,
you have to write your logic in terms of 'p' and 'q'.
[no audio]
Nowhere we have 'a' and 'b', we have only 'p' and 'q'. The same way here
also you can take, no need to take 'a', 'b' or 'p', 'q', you can take
some 'm, n'. No problem.
The only thing what will happen whenever if you're calling
your functions, we are sending the values.
This first value will be assigned to first variable,
second value will be assigned to second variable. Here
also 'a' value will be assigned to 'm', 'b' value will be assigned 'n'.
Now inside of your 'get_sub', there are no more 'a' and
'b'. Now you have in terms of 'm' and 'n' your variables.
[no audio]
That's it.
Let me modify completely, and let me run this code. You are
going to get same results. See that.
[no audio]
Right. So be clear. 'a' and 'b' are specific to 'main' function, 'p'
and 'q' are specific to 'get_add' function, 'm' and 'n' are specific
to 'get_sub' function.
Right. While calling in this way
only the values will be assigned to your variables respectively.
First value - first variable, second value - second variable.
That's it.
[no audio]
Okay. Now suppose I have directly values called 9 and 10.
I want to find the subtraction for this.
Yes, we can find, 'get_sub(9,10)'. Directly you can provide
your values also.
[no audio]
Now, if I do in this way, let me comment these two functions.
[no audio]
Now, I'm not reading 'a' and 'b' values. In my hand directly I
have values 9 and 10.
Yes, you can call your 'get_sub' or 'get_add' function, whatever it may be.
Anyway, those two are taking two values - 'p,q' or 'm,n'.
That's why I'm passing two values, 'get_sub(9,10)'.
Now, let me run it and see the result.
We are getting -1. Let me take 19 and 10,
[no audio]
or let me define some variable called,
let's say some 'x = 50'.
[no audio]
Now you can pass one value as variable and one more as directly
value, no problem. It will work.
Only thing, finally it is going to match the values 19 with
the 'm' and 'x' value with the 'n'.
So, what you are mapping? Only values you are more mapping
here. That's very important thing.
So, guys, this is a simple way to work with your arguments.
Right. Okay guys,
thank you for watching this video.
[no audio]