Contents¶
- Arguments
- Parameters
- Types of arguments passed in a function
- Rules for using different types of arguments together
In [2]:
def ad(a,b,c):
print(a+b+c)
ad(10,20,30)
in the above example code, **a,b,c** are the parameters and **10,20,30** are the arguments. so we can say that arguments appear in a function call statement and parameters appear in function definition header
In [3]:
ad(10,10,20) ## CORRECT FUNCTION CALL
In [4]:
ad(10,20) ## INCORRECT FUNCTIONAL CALL
In [8]:
def ad1(a,b,c=10):
print(a+b+c)
here in the above function, c is a default argument as it have a default value of 10
In [9]:
ad1(10,20,30) # valid function call
In [10]:
ad1(10,20) # also a valid call as argument c will have a default value of 10, so the answer is 40
NOTE: Default argument can not be followed by non-default arguments in a function definition. Example
In [12]:
def ad2(a=10,b,c):
print(a+b+c)
Keyword Argument:
In this type we make the function call by explicitly mentioning the name of the parameter and providing the value, thus giving us the flexibility over positional argument matching. so keyword argument allows us to target individual parameter and we don't have to depend on the order or arguments
In [19]:
def si(principal, rate, time):
'''
this function calculates the simple interest earned over a period of time based
on offered rate of interest
'''
tamt=principal+principal*(rate/100*time)
interest=tamt-principal
print(interest)
in the above function which calculates the simple interest, the order of the values passed in the function call is very important i.e.
In [20]:
si(100,10,1) #100 rupee, 10 % interest rate and 1 year of time
the above works fine as the order in this the values are passed are same in the function definition but if we mistakenly change the order, it might give unwanted result. hence we can use name of the parameter i.e. keyword argument
In [21]:
si(time=1,rate=10,principal=100)
In [27]:
def sm(a,b,c,d,e,f,g,h,i,j):
print(a+b+c+d+e+f+g+h+i+j)
sm(10,20,30,40,50,60,70,80,90,20)
so as we can see its already getting ugly and not a good approach also not allowing to passmore arguments without modifying the function definition. Now we can modify the function definition to accept variable length positional argument using *args
In [52]:
def sm(*args):
su=0
for i in args:
su+=i
print(su)
In [53]:
sm(10,20,30,40,50)
In [40]:
sm(10,20,30,40,50,60,70,80,90,20)
so we can see that function is able accept any number of arguments. NOTE: args is just an name(identifier) so any other valid name will work
Now **kwargs is used to provide multiple keyword arguments as opposed to positional arguments by *args
In [1]:
def studentInfo(**args):
for key,value in args.items():
print(key,end=" : ")
print(value)
In [60]:
studentInfo(name='john')
In [62]:
studentInfo(name='john',std='12A',sub="CS")
NOTE: data passed through \*args is stored in tuple and that of **kwargs is stored in dictionary, so they can support the operation on these data structure that are allowed in python
Rules for using different types of arguments together:
when we are using different types of arguments in a function definition and call, the relative ordering matters. Hence we have to take care of the following:
1. Non-default arguments have to precede(come before) default arguments
2. positional arguments have to precede *args and **kwargs
3. *args have to precede **kwargs
References:
- computer science with python - sumita arora
- https://realpython.com/