17. Functions: Advance

Arbitrary Arguments :

If you do not know how many arguments that will be passed into your function, add a * before the parameter name in the function definition. It creates a tuple of arguments and pass it into function. By using arbitrary arguments, we can pass any number of Arguments. For Example :

In [4]:

Copy

def add(*x):   #*x is an arbitrary argument.
    print(x)

add(5,3,3,7,8,9)  

From above code , it is clear that arbitrary argument creates a tuple of parameters and pass it. How can we use it to add all the values? let's continue this code to achieve desired result.

In [5]:

Copy

def add(*x):   #*x is an arbitrary argument.
    s = 0
    for i in x : 
        s = s+i
    
    print(f'sum of numbers is {s}')
    

add(5,3,3,7,8,9)  

You can pass almost anything in function as an argument like number,string,list,set,dictionary or even a function.

Calling a function as an argument :

In [1]:

Copy

def hello(x):
    x('hello world')

hello(print)

Types of Functions :

There are two types of functions which are as follows:

Non - Return Type Function : Non return type functions are the functions that do not return any values. A function that does not return any values returns None. For Example :

In [2]:

Copy

def add(x,y):
    print(x+y)

value = add(5,3)
print(f'returned value to calling function is : ', value)

Copy

8
returned value to calling function is :  None

Here You can see add function did not return any value. It is known as no return type function. We have worked yet only on non return type Function.

Return Type Function : Return type function are the functions that return value. We use keyword return to return values to calling Function. For Example :

In [3]:

Copy

def add(x,y):
    return x+y

value = add(5,3)
print(f'returned value to calling function is : ', value)

Copy

returned value to calling function is :  8

return multiple values to function :

You can also return multiple values to function by using comma separator. It will internally create all values into a tuple and will return a tuple. For Example :

In [4]:

Copy

def calc(x,y):
    return x+y , x*y , x/y , x**y

value = calc(2,2)
print(value)

As you can see, it returned a tuple of addition , multiplication , division and exponent. But what if we want all values separated . We can use concept of Assigning multiple values in multiple variable. For Example :

In [5]:

Copy

def calc(x,y):
    return x+y , x*y , x/y , x**y

add,mul,div,exp = calc(2,2)
print(f'addition : {add}, multiplication : {mul} , division : {div} , exponent : {exp}')

Copy

addition : 4, multiplication : 4 , division : 1.0 , exponent : 4

Recursion :

The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function.

In more simpler terms , when a function calls itself is known as recursion. For Example :

Factorial of a number with the help of recursion :

In [6]:

Copy

def facto(x):
    if(x==1):      #x = 5 , hence it will go to else
        return 1
    else:
        return x * facto(x-1)      #value that returns is 5*facto(4)

fact = facto(5)  #calling a function
print('factorial is : ',fact)

facto(5) will return 5 $*$ facto(4) which means fact = 5 $*$ facto(4)

facto(4) will return 4 $*$ facto(3) which means fact = 5 $*$ 4 $*$ facto(3)

facto(3) will return 3 $*$ facto(2) which means fact = 5 $*$ 4 $*$ 3 $*$ facto(2)

facto(2) will return 2 $*$ facto(1) which means fact = 5 $*$ 4 $*$ 3 $*$ 2 $*$ facto(1)

facto(1) will return 1 which means fact = 5 $*$ 4 $*$ 3 $*$ 2 $*$ 1

Lambda Function :

In Python, an anonymous function is a function that is defined without a name.

While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword.

Hence, anonymous functions are also called lambda functions.

How to use lambda Functions in Python?

A lambda function in python has the following syntax.

Lambda functions can have any number of arguments but only one expression. The expression is evaluated and returned. Lambda functions can be used wherever function objects are required.

In [ ]:

Copy

lambda arguments: expression

Example of Lambda Function in python:

Here is an example of lambda function that squares the input value :

In [7]:

Copy

sq = lambda b : b**2

value  = sq(2)
print(value)

In the above program, lambda b: b ** 2 is the lambda function. Here b is the argument and b ** 2 is the expression that gets evaluated and returned. This function has no name. This function is very same like this :

In [8]:

Copy

def sq(b):
    return b**2

value = sq(2)
print(value)

Lambda function is used whenever we want to do small tasks, small functions

Last updated