Python Pass Function As Argument With Code Examples

by Muhammad Talha
Python Pass Function As Argument

Python is one of the most powerful programming languages that is widely used for various applications. One of its most interesting features is the ability to pass functions as arguments to other functions. This technique allows for a more flexible and modular approach to writing code, as well as promoting code reuse. In this blog, we will take a look at how to pass functions as arguments in Python with clear and concise code examples.

Pass Function as Argument

We have a list of integers that represent whole numbers.

l = [1,2,3,4,5]

Next, we have a function of the name ‘square’ that takes an integer as an argument and return the square of that number.

def square(a):
    return a**2

Now we will create another function that will take two arguments, one is a function and the other is a list. The function will iterate through the list ‘l‘ and passes its every item to the function ‘square‘. Create a new list and append every item that is returned from the square function. Finally, return this new list.

def my_map(func, l):
    new_list = []
    for item in l:
        new_list.append(func(item))
    
    return new_list

Here is the complete code of the above example for the python pass function as an argument.

l = [1,2,3,4,5]

def square(a):
    return a**2

def my_map(func, l):
    new_list = []
    for item in l:
        new_list.append(func(item))
    
    return new_list

print(list(my_map(square,l)))

Output

Another Example of Passing Function as an Argument

In this example, we have two functions ‘shout‘ and ‘whisper‘ that take takes a string argument and return a modified string. The shout function returns the uppercase string while the whisper function returns the lowercase string.

def shout(word):
    return word.upper() + "!"

def whisper(word):
    return word.lower() + "..."

Next, we have another function ‘speak‘ that takes two arguments: a function and a string. The speak function calls the function passed in as the first argument with the string passed in as a second argument. Here is the complete code of the above example.

def shout(word):
    return word.upper() + "!"

def whisper(word):
    return word.lower() + "..."

def speak(func, word):
    return func(word)

print(speak(shout, "Hello")) # HELLO!
print(speak(whisper, "Hello")) # hello...

Output

Python Pass function as argument using ‘map‘ function

In this example, we will use a built-in function ‘map‘ to pass a function as an argument. We have a list of integers. A function of the name ‘square‘ that takes in a number as an argument and returns its square.

The ‘map‘ function takes in a function and the list as parameters and applies the function to each item of the list. Here is the complete code of the example for the pass function as an argument using the ‘map‘ function.

def square(a):
    return a * a

numbers = [1, 2, 3, 4, 5]

squared_numbers = list(map(square, numbers))
print(squared_numbers)

Output

Summary

In Python, you can pass functions as an argument to other functions. In this post, we have explored some examples of how to define a function that accepts another function as an argument. Also, we have gone through the built-in function in Python ‘map‘ that does the same job. If you’ve any queries feel free to ask in the comments block below.

Related Posts

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.