Chapter 1: Introduction

Higher-order functions

face Josiah Wang

Remember higher-order functions? We also discussed these back in Core Lesson 10 when talking about functional-style programming.

Recall that higher-order functions are functions that take a function as an input argument or return a function. For example, the Python map() and filter() functions are higher-order functions that take functions as input.

Since all functions are objects in Python, we can pass them into a function as input arguments. Here is a recap: what is the output for the following piece of code?

def laugh():
    print("MUAHAHAHAHA!! :D")

def cry():
    print("WAAA!! TT_TT")      

def multiplier(func, repeats):
    for i in range(repeats):
        func()

multiplier(laugh, 5)
multiplier(cry, 2)