Python supports three type of formal argument / parameters.
- Positional argument (Required)
- Default argument
- Keyword argument (named argument)
Positional argument (Required)
For example, function sum_of_three (a, b, c) calculating the sum of three variables. In this number of passed values must matched with number of received values.
Output is :
sum= 60
sum= 35
sum= 600
The argument must be provided for all parameters (Required) if I miss any there will be error. eg sum_of_three (x,y) here third parameter is missing hence python shows error .
Another thing value of argument are matched with parameters (position) order wise.
For example
Here in function intro(age, name) , age is integer type and name is string type so we taken care about position also while calling function. Look at the example where we call
- intro(a,s) # integer and string
- intro(s,a) # string and integer
Output is :
your name is Rahul and you are 18 years old
your name is 18 and you are Rahul years old (due to wrong position of arguments )
2) Default Parameters: –
What if we already know the value of certain parameters then eg same function sum_of_three (a, b, c) calculating the sum of three variables. In this number of passed values must matched with number of received values. If we call this as sum_of_three(x,y) with two parameters it will give us error because three parameters are must.
But what happens when we know the value of c in sum_of_three (a, b, c), then it is possible to call this with two parameters also sum_of_three(x,y) ..here third parameter is missing
Look at the example.
In above example sum_of_three(x,y,z) and sum_of_three(x,y) both are worked
Output is :
sum= 60
sum= 31
Default parameters may be one, two or n but default parameter before required parameter is not allowed eg
sum_of_three(a, b=1 ,c) gives error
if function having three parameters and two from them are default then they must before require parameter.
sum_of_three(a, b ,c=1) # legal
sum_of_three(a, b=1,c=1) # legal
sum_of_three(a, b=1,c) # illegal
sum_of_three(a=1, b, c=1) # illegal
sum_of_three(a=3, b=2,c=1) # legal
In same way if we calculating simple interest in python and company mostly have interest rate 10% then we take rate as default parameter with value 10.0 and we call this function with three parameters as well as with two parameters also.
3) Keyword argument: – Default argument gives you flexibility to specify the default value for the parameter but still you cannot change the order, in short you remember the correct order of argument.
def interest(prin, time, rate=10.0):
amt=(prin*time*rate)/100
return amt
print(interest(20000,2,12.0))
print(interest(20000,12.0,2)) # here time is 12 years and rate is 2 %
so keywords arguments works here we can write any argument in any order providing name the argument. eg
print (interest (20000, rate=12.0, time=2))
print (interest (rate=12.0, time=2, prin=20000))
this is the way for specifying names for the values is known as keyword arguments.
Awesome explanation sir you are the best teacher forever. Keep it up