Different forms of calling function and passing arguments to functions in python


Function definition
Function Caller
Function call mapping to function definition
def func(x,y)
func(a,b)
Positional matching of argument
func(y=b, x=a)
Argument name matching
def func(x,y=10)
func(a)
func(a,b)
Default value for argument
def func(x,y, *tuple_arg)
func(a,b)
func(a,b,c) and many other arguments can be passed as positional arguments
Function with varying positional arguments stored in a tuple
Example:
def add(x,y, *tup):
    temp = x+y
    for elem in tup:
        temp = temp + elem
    return temp

print add(1,2) # prints 3
print add(1,2,3,4) # prints 10
def func(x,y, **dict_arg)
func(a,b)
func(a,b, c=10)
func(a,b, c=10, name='abcd' ) and many other arguments can be passed as keyword argument
Function with varying keyword arguments stored in dictionary.
Example:
def percentage(mks1, mks2, **dict):
    total_mks = mks1 + mks2
    return total_mks / float( dict['out_of'] )

print percentage(65, 50, out_of=150)

No comments:

Post a Comment