×

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 - Returning a function as a return value

Last Updated : April 25, 2025

Python allows you to return a function as a return value, which helps in creating higher-order functions and enables dynamic function generation.

Returning a function as a return value

Python allows returning a function as a return value. For that, you need to create two functions, and then while writing the definition of the second function use the first function with the return keyword.

Syntax

Consider the below syntax (or, approach) to return a function as a return value:

def func1():
    body

def func2()
    body
    return func1

Example for returning a function as a return value

Here, we are defining two function foo() and koo(), function koo() will return as a value (return value of the function).

The calling statement is x=koo() - where, koo() is first function and the return value of koo() (that is the function foo()) will store in the x (which is also a function).

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

# defining an another function
# it will return function as a return value
def koo():
    return foo

# function calling and assigning return value
# in x
x = koo()
x()

The output of the above code will be:

I am Foo

Returning Functions in Python Exercise

Select the correct option to complete each statement about returning functions from other functions in Python.

  1. In Python, a function can return another function because functions are ___.
  2. Given: def outer(): def inner(): return "Hi"; return inner, what does outer()() return?
  3. Which of the following best describes a use-case of returning a function?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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