Pass Python Function as a Function Argument



We can pass a Python function as a function argument by treating it like any other object. In Python, functions are first-class objects. This means you can treat them like any other value, assign them to variables, return them from other functions, and even pass them as arguments to other functions.

Passing functions as arguments allows you to create flexible and reusable code. This is useful in cases like sorting, filtering, customizing behavior, and implementing callbacks.

In this article, we will explore how you can pass a function to another function in Python and how it helps in building higher-order functions.

Passing a Simple Function as an Argument

 To pass a function as an argument to another function, we just need to pass its name without parentheses. If we add parentheses, we will be executing the function will be executed right away instead of passing it.

Example

In the following example, we pass a greet() function to another function that runs it ?

# A simple function to be passed
def greet():
   print("Hello!")

# Function that accepts another function
def run_function(func):
   print("Running the function:")
   func()

# Pass greet function to run_function
run_function(greet)

The output will be ?

Running the function:
Hello!

Passing Functions with Arguments

Sometimes, the function you want to pass accepts arguments. In that case, the outer function (receiver) can call the inner function and (in-turn) pass the required arguments.

This makes your code more flexible, you can pass different functions to do different tasks, and the outer function doesn't have to know the details.

This is used in cases like custom logic, data processing, or performing a series of actions in a specific order.

Example

In this example, we define two math functions and pass them into a calculator function ?

# Define two simple operations
def add(a, b):
   return a + b

def subtract(a, b):
   return a - b

# Function that accepts another function and arguments
def calculator(func, x, y):
   return func(x, y)

# Pass different functions
print(calculator(add, 10, 5))      
print(calculator(subtract, 10, 5)) 

The output is as follows ?

15
5

Using Anonymous Functions (Lambdas)

In Python, you can pass a function without giving it a name. This is called an anonymous function, and it is created using the lambda keyword.

Lambdas are useful when you need a small function for a quick, one-time task. Instead of writing a full function with def, you can write it in a single line.

They are used with built-in functions like map(), filter(), and sorted() to make the code shorter and easier to read.

Example

Here, we use a lambda to square numbers inside a custom apply function ?

# Function that applies another function to a value
def apply(func, value):
   return func(value)

# Use a lambda to square a number
print(apply(lambda x: x ** 2, 4)) 

# Use a lambda to get half of a number
print(apply(lambda x: x / 2, 10))  

The output will be as follows ?

16
5.0

Using Built-in Higher-Order Functions

Python provides several built-in higher-order functions like map(), filter(), and sorted() that accept functions as arguments. These are called higher-order functions because they work with other functions.

You can use them to apply a function to each item in a list, filter out items based on a condition, or sort items in a custom way. By passing your own function, you can easily control how these built-in functions behave.

Example

In this example, we use map() function to apply a function to each element of a list ?

# Function to double a number
def double(n):
   return n * 2

numbers = [1, 2, 3, 4]
result = list(map(double, numbers))
print(result)  

The output is as shown below ?

[2, 4, 6, 8]

Passing Functions in Custom Workflows

Passing functions is very helpful when creating custom workflows or step-by-step processes (pipelines). It lets you decide what each step should do by simply passing a different function.

This makes your code cleaner and easier to reuse. Instead of writing the same code again and again, you can just change the function you pass to change the behavior.

Example

Here, the workflow runs different logic based on the passed function ?

# Different processing steps
def process_upper(text):
   return text.upper()

def process_lower(text):
   return text.lower()

# Function to run a processing function
def run_pipeline(data, step_func):
   return step_func(data)

text = "Hello World"
print(run_pipeline(text, process_upper)) 
print(run_pipeline(text, process_lower)) 

The output will be ?

HELLO WORLD
hello world
Updated on: 2025-04-11T14:43:30+05:30

27K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements