×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python Passing a function as an argument

Last Updated : April 25, 2025

Python allows you to pass a function as an argument to another function which helps you to write flexible and reusable code.

Passing a function as an argument

You can pass a function as an argument by defining two functions and passing one function as a parameter when calling the other.

Syntax

Consider the below syntax (or, approach) to pass a function as an argument:

def func1():
    body

def func2():
    body

# Calling
func2(func1)

Example for passing a function as an argument

Here, we are defining two function foo() and koo(), function koo() will take an argument x that will be a function while calling.

The calling statement is koo(foo) - where, koo() is first function and foo() is second function. Function foo() is function as an argument here.

# defining a function
def foo():
    print("I am Foo")

# defining an another passing,
# it will take function as an argument
def koo(x):
    x()

# calling first function by passing
# second function as an argument
koo(foo)

The output of the above code will be:

I am Foo

Passing Functions as Arguments Exercise

Select the correct option to complete each statement about passing functions as arguments in Python.

  1. In Python, functions are treated as ___, which means they can be passed as arguments.
  2. What will def apply(f, x): return f(x) return for apply(lambda x: x ** 2, 3)?
  3. Which of the following correctly passes a function to another function?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.