Home »
Python
Python - Passing a function as an argument
In this tutorial, we will learn how to pass a function as an argument in Python.
By Pankaj Singh Last updated : December 30, 2023
Passing a function as an argument
Python allows passing functions inside another function. To pass a function as an argument, define two functions and pass a function as an argument while calling the second function.
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)
Output
I am Foo
Python Tutorial