In this tutorial we will see the different method of passing arguments into functions in Python.
We will start with positional argument.
Let's consider then an example for positional arguments.
So I have defined a function your student with these parameters.
Name each class and Mark.
And within this function we are printing each of the arguments that we have received during the function
call.
So let's make a call to this function student by passing positional parameters and when passing positional
parameter it is important to get the argument in the right order.
That's because Python matches the position of the argument with the parameters defined in the function.
So the forced argument that I'm going to parse is matched with the first parameter which just name.
So the Fausto argument that I'm going to parse is the name Sam and then h B10 class is great.
5 and then marks 70 when we make a function call when passing these bodies positional parameters Python
matches this value some with the parameter name and then it goes to the next while you 10 it matches
with the parameter age and then decode and default values and so on.
So this is pretty straightforward but we have to make sure to get the arguments in the right order and
if we mess up the order.
This would result in an error.
So let's execute this function in the Jupiter note so here is our function.
Student in 4 and we are going to print the arguments received through the function call in this function.
So let's make a call to this function.
Student in 4 and then parse the parameters the argument so the first argument is Sam and then the h
is in grade is Grade 5.
And then the marks so let's execute this and we execute this.
This is the output that we have got.
Python has matched each of the values and the parameters and then displayed the values.
Now let us change the order of the first and the last argument that we have passed.
So letters use the first argument as 70 and the last argument as the name Sam and then execute this.
So as you can see if you change the order of the positional parameters this will result in arrows unwanted
arrows because Python matches the values to deepen amateurs by position.
This is how the output would look like.
Now so it is important to give the right position for each of the arguments.
Now let's discuss about the second method.
Keyword arguments Python allows us to use a syntax called keyword argument.
In this case we can get the argument in any order.
When we call the function as long as we use the name of the argument in the function call as you can
see we have used the name of the argument when we are passing the argument.
John H is equal to 10.
Class is equal to grade 5 and so on because we are associating the parameter name the D values the order
of the argument in the function call doesn't matter.
When we add passing keyword arguments but in the case of positional arguments we pass the arguments
to a function in the same ordered as these respective parameters in D function header.
Now let's pass keyword argument to this function in the Jupiter notebook.
In this function call.
I have parsed keyword arguments to the function in to this function student info so let's execute this
cell.
And here is the out.
So as discussed before the order of the argument in the function call doesn't matter when we are passing
keyword arguments.
So let
give a different order for the argument that we have passed so in this function call I have given the
first keyword argument as marks.
And then it is the age and then name and great.
So let's execute this cell and then see the output.
So as you can see even though we have changed the order of the argument that we have passed to the function
call Python matches the parameter name that devalues the order of the arguments and the function call
doesn't matter.
Next we will discuss about default argument in Python.
We can define a function that default parameter values and this default value will be used when a function
is invoked without any argument.
So let's see this in the Jupiter non book.
So here to define this function student info that some default parameter values.
And next we will make a function call and when we make a function call without passing any argument
the default values that we have defined using the function definition will be used when a function is
invoked without any argument.
And in order to specify default values for a parameter just specify the value using the assignment operator
followed by the parameter name and defining default values for parameters is useful when we have a number
of arguments in our function and some of these arguments almost always have the same value.
Now after defining a function that default parameters I want to make a function call but with the different
parameter value for the parameter name and all other values should remain the same in that case.
Python allows us to make a function call by passing a single argument like this.
So let make a function call.
I only want to have the name changed and all other default while loose fixed.
In that case I can only parsed a single argument like this and then execute.
So as you can see the first value that we have parsed here has been mapped to the first parameter and
all are the default values have been picked from the function definition and also we can mix positional
arguments and keyword arguments in a function called see this example.
So I'm gonna pass positional argument like this.
I'm going to pass muster as the first positional argument and then I'm going to see the marks.
Hundred then let's execute this.
So the first argument that we have past has been matched to the first parameter name and then the second
and the third parameters have been taken from the default values that we have defined.
And the last parameter has been matched to the last argument is picked from the keyword argument that
we have passed in the function call.
So here there is a mix of positional and keyword arguments when passing positional and keyword arguments
in a function call.
You need to remember that positional arguments must appear before any keyword arguments.
In this example I have past three arguments that are two positional arguments and that is one keyword
arguments.
The requirement when we make a function call when passing both positional and keyword arguments is positional
arguments have to be followed by the keyword arguments.
But in this case the last argument that we have past is a positional argument and hence we have a syntax
error when we execute this code.
This brings us to the end of this tutorial.