Python Mastery – From Basics to Brilliance / Functions

Higher Order Functions

🔝 Higher-Order Functions

A Higher-Order Function is a function that does at least one of these:

  • Takes another function as an argument

  • Returns another function

This is possible because Python functions are first-class objects.


1️⃣ Function as an Argument

def greet():
    print("Hello!")

def execute(function):
    function()

execute(greet) #Hello!

Here, execute() is a Higher-Order Function because it receives greet as an argument.


2️⃣ Function as a Return Value

def outer():
    def inner():
        print("Hello!")

    return inner

result = outer()
result() #Hello!

Here, outer() is a Higher-Order Function because it returns another function.


⭐ Remember

Some examples of Higher order functions are map(), filter(), reduce(), zip(). We will discuss these in further chapters.

First-Class Functions → Functions can be treated like values.

Higher-Order Functions → Functions work with other functions.