Python Pass Function As Argument With Code Examples

One of the most powerful programming languages, Python is utilized globally for several different types of applications. Its ability to send functions to other functions as factors is one of its most intriguing features. This method increases code reuse while allowing a more adaptable and modular approach to code creation. This blog post will examine the process of passing functions as arguments in Python, giving lucid and succinct 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

[1, 4, 9, 16, 25]

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

HELLO!
hello…

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

[1, 4, 9, 16, 25]

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top