×

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

By IncludeHelp Last updated : December 08, 2024

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

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement



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