In Python, everything is an object. It means that everything can be transmitted and received by reference. The same applies to functions.
In languages where functions can be taken and passed as values, functions are first-class citizens. In Python, we refer to functions that take other functions as arguments or return functions as higher-order functions.
First-order functions accept and return regular values, not functions. Let us implement it:
def call_with_five(function):
return function(5)
def add_one(x):
return x + 1
call_with_five(add_one)
# 6
The call_with_five
takes another function at the input and returns the result of its call with the argument 5
. To complicate the example, we will also add what the function returns: