Functions as Parameters and Returning Functions for understanding Decorators

In this class, we discuss Functions as Parameters and Returning Functions for understanding Decorators.

For Complete YouTube Video: Click Here

Functions as Parameters

The reader should have prior knowledge of functions and how memory is allocated to functions. Click here.

This concept of functions as a parameter and returning functions will help us understand the decorator’s concept we discuss in our next class.

Take an example and understand the concept of functions as parameters and returning functions.

def fun(z):
      print("hello")
      print(z)
      return z
x=fun
x(5)

In the above program, we had a function f with parameter z.

The function is displaying hello and the value of z.

Finally, returning the variable z.

The statement x=fun is creating a function object. Means memory is assigned.

Assume memory is allocated at location 35. Now the fun is referencing memory location 35.

Fun is having a value of 35. and fun is assigned to variable x.

The variable x is now used as a function. Because the variable x is referencing to function object.

The basics of how memory allocated to functions discussed in our previous classes.

Note:

Take x=fun(10) in this statement, we are not assigning a function object to variable x.

We call the function fun. Means fun is executed, and the returned variable z reference is given to x.

with the basics we discussed above, we take some examples to understand the concept of functions as parameters.

Example:

def funct1(func):
    func()
def funct2():
    print("hello function 2")
funct1(funct2)

Output:
hello function 2

In the above example, we defined two functions funct1 and funct2.

funct1(funct2) is called funct1, and funct2 is passed as an argument to funct1.

The object reference of funct2 is taken by parameter func in funct1.

We can use the parameter func to call the funct2 function.

The output displayed is hello function 2.

Few more examples are given below. Try to analyze the output.

# functions with arguments
def funct1(func):
    print(func(5))
def funct2(x):
    return x+1

funct1(funct2)

Output:
6
def funct1(func):
    print(func(5))
def funct2(x):
    return x+1

funct1(funct2(5))

Output:
error
Int object is not callable

Returning Functions

Take an example and understand the concept of returning functions.

def parent(num):
    def first_child():
        return "Hi, I am suresh"

    def second_child():
        return "Call me naresh"

    if num == 1:
        return first_child
    else:
        return second_child
result=parent(2)
result()

Output:
Call me naresh

In the above example, We have a function parent with parameter num.

Inside the function parent, we have two functions, first_child and second_child.

For the nested functions, concept click here.

We have written a condition if parameter num is equal to one. Return first_child.

We are returning the object of function first_child.

 If the condition fails, we are returning function second_child.

Analyze the program given below for better understanding.

def parent(num):
    def first_child(x):
        return x+num

    def second_child(x):
        return x

    if num == 1:
        return first_child
    else:
        return second_child
result=parent(1)
result(5)

Output:
6