Saturday, July 25, 2020

parameter and arguments in python function


Arguments:


arguments are the actual values that are passed in the function call. They are also known as actual parameters or actual arguments

Parameters:


parameters are the variable/objects that are declared in the function defination. They are also know as formal parameters or formal arguments
In [2]:
def ad(a,b,c):
    print(a+b+c)

ad(10,20,30)
60
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

Types of arguments



Positional arguments:
these are the required arguments that needs to be supplied during the function call, else the function call will through error.
In [3]:
ad(10,10,20)   ## CORRECT FUNCTION CALL
40
In [4]:
ad(10,20)   ## INCORRECT FUNCTIONAL CALL
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-c9500b6e19e9> in <module>
----> 1 ad(10,20)   ## INCORRECT FUNCTIONAL CALL

TypeError: ad() missing 1 required positional argument: 'c'
NOTE: The number and order of arguments must match in the function call as defined in the function defination, this is known as positional arguments matching

Default Arguments:
In these type of arguments, the parameters in the function definition are given a default value, so that if in the function call, the value for a parameter (i.e. argument) is not provided, then the default value if used. default arguments are kind of optional in a function 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
60
In [10]:
ad1(10,20) # also a valid call as argument c will have a default value of 10, so the answer is 40
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)
  File "<ipython-input-12-e70f58bff9ed>", line 1
    def ad2(a=10,b,c):
            ^
SyntaxError: non-default argument follows default argument

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
10.0
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)
10.0

*args and **kwargs:
these are the ways to pass multiple arguments into a function but the function definition must have them. it makes our job easy when the function have large number of parameters. example:
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)
150
In [40]:
sm(10,20,30,40,50,60,70,80,90,20)
470
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')
name : john
In [62]:
studentInfo(name='john',std='12A',sub="CS")
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:
  1. computer science with python - sumita arora
  2. https://realpython.com/
Share: