9.function arguments

Arbitary Arguments

Arbitary arguments are shortened as *args in python documentation.

If we are not sure on number of arguments that will be passed into our function, then we use * sign before the parameter name while defining the function.

If the number of arguments is unknown, just add * before the parameter name. In this manner, the function accepts the tuple of elements as arguments and access the elements accordingly.

Example

def myfun(*args): 
    for item in args:
        print(item)

myfun(40,60,40,50,60,70)

Example

def myfunc(*args):
    mylist = []
    for item in args:
        if item%2 == 0:
            mylist.append(item)
    return mylist

myfunc(5,6,7,8)

keyword Arguments

We can also send the arguments as key = value pair.
In this case, the order of the arguments does not matter.

Example

def my_function(child3, child2, child1):
  print("The youngest child is " + child3)

my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")

Arbitary Keyword Arguments

Arbitary Keyword Arguments are shortened as **kwargs in python documentation.

If we are not sure on number of keyword arguments that will be passed into our function, then we use ** sign before the parameter name while defining the function.

In this manner, function will accept dictionary items and can access elements accordingly.

Example

def myfunc(**kwargs):
    print(kwargs)
    if 'fruit' in kwargs:
        print('my fruit of choice is {}'.format(kwargs['fruit']))
    else:
        print('I did not find any fruit here')

myfunc(fruit='apple', veggie='lettuce')

Example

def myfunc(*args, **kwargs):
    print('I would like {} {}'.format(args[0], kwargs['food']))

myfunc(10,20,30,fruit='orange', food='eggs', animal='dog')

Passing a List as an argument.

We can pass any type of data as argument to a function, that will be treated as same data type inside the function.

Example

def my_function(food):
  for x in food:
    print(x)

fruits = ["apple", "banana", "cherry"]

my_function(fruits)

Recursion

Recursion of function indicates calling function by itself.

Example

def recursion_fun(k):
  if(k > 0):
    result = k + recursion_fun(k - 1)
    print(result)
  else:
    result = 0
  return result

print("\n\nRecursion Example Results")
recursion_fun(2)

In this case, recursion_fun is a function that is defined to call itself. Here, K value decrements by -1 everytime and recursion stops when the k value is not greater than 0.

In the above example, the operation will be as follows

  1. recusrion_fun when k=2
    result = 2 + rescursion_fun(1) This will wait for the result of recursion_fun(1), once recursion_fun(1) returns a result, that will be added to 2 and total result will be printed.
  2. recursion_fun when k=1
    result = 1 + recusrion_fun(0) recursion_fun(0) result will be returned as 0 and this won't be printed as there is no print result in else part.
    result of the recusrion_fun(1) will be printed as 1 (1+0) and result will be returned.
    Now, the result of recursion_fun(2) will be 3 (2+1) and this will result be printed and returned to the function.