Types Of Arguments
Types of arguments
1.Positional arguments
2.Default arguments
3.Keyword arguments
4. Variable length arguments
5.Variable keyword length arguments
what is Arguments
The input passed through a function is technically called as argument.
Parameters collect the inputs, which is passed to the function when it is called
Positional Arguments
In the above example we are performing power operation. Here the arguments are 2,5 and parameters are a,b where a gets assigned as 2 and b gets assigned as 5 based on the positions. To verify this let’s try to interchange the positions and check whether the value of c changes or remains same.
As we can see, a gets assigned as 5 and b gets assigned as 2. Which proves the above statement, if passed in the above format, position definitely matters. If we miss a single argument then error appears, let us see what the error is
Error clearly tells us that we are missing an argument b. So now let us see how to get away with this.
Default Arguments
In the above example we have only single argument, assuming b will take the default value assigned to it as 0. Giving a value while declaring the parameter is referred to as default value.
Certainly we can also give two arguments
Here we have third argument. Let us see if we get the output as expected, that is 5*3*2 = 30
As the message reflected, non-default argument cannot follow default argument. But certainly after positional arguments we can have as many default arguments as possible.
Keyword arguments
In positional arguments we noticed that if order changes the output also changes. We can overcome this by using keyword arguments, let us see how
We can see that, if we mention the keywords while passing arguments the output does not depend on the order or the position of the arguments. This is the advantage of using keyword arguments.
Variable length argument
Passing any number of arguments to a function is called as variable length argument. Let us know better by an example
In the above example we are trying to pass different number of arguments but only first argument is what the function is taking, so certainly some changes in function declaration is needed, let us see what is the change and how that change will accept multiple arguments
We can now see that by attaching a star or asterisk in front of the parameter, the function is now ready to take different number of arguments. This is because toppings is now considered as tuple. Let us verify that by printing the type of toppings
Note: * in front of the parameter makes it a tuple. But while calling for it we should just enter name which does not contain *.
Let us see another case
In the above example we are trying to pass another argument. But crust is a positional argument/non-default argument. So if we use keyword and assign the value, the output is as expected. But what if we don’t use the keyword?? Let’s see
Here we see that cheese and thin both are considered as toppings. So if we are passing any arguments before or after the variable length argument it is mandatory to use keyword arguments
Variable keyword length arguments
In the previous example we saw how to pass different number of arguments, but all of them were toppings. What if each argument we pass represents different data. Let us see how to resolve this
We certainly got the output, but we still can’t recognise what is 28, 60.5, M. To resolve this we must give a key to each value entered as shown below
The error states that it doesn’t recognise name and similarly age, avg, gender. To resolve this we must make another change, let us see what that is, and how it resolves the issue
All we did was add another * to the parameter. And we can see in output that the keys and values are enclosed with { } which states ** in front of a parameter makes it a dictionary.
Which can store different arguments along with keywords associated with it. Here also we have the same rule as seen earlier that if we are passing any arguments before or after the variable keyword length argument it is mandatory to use keyword arguments