Higher Order Functions

Master the core functional programming techniques that power Python frameworks, AI pipelines, backend logic, decorators, and more.

Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

This lesson takes you from "I know what functions are" → "I can write dynamic, scalable, factory-generated functions used in real systems."

Higher-order functions (HOFs) and function factories are two of the most important advanced concepts in Python — and they unlock a completely new style of coding.

Think of a coffee machine . You can give it different "programs" (espresso, latte, cappuccino) and it executes them. A higher-order function is like that machine—it takes a "program" (function) as input and runs it!

Imagine a cookie cutter factory . You tell the factory what shape you want (star, heart, circle), and it gives you a custom cookie cutter. Each cutter is different, but they all came from the same factory!

A closure is like a backpack . When you create an inner function, it packs any variables it needs from the outer function into its backpack. Even after the outer function finishes, the inner function still has its backpack!

💡 Closures = data + behavior bundled elegantly without classes. They're like lightweight objects!

Function factories = functions that create functions.

A decorator is simply a function that: (1) takes a function as input, (2) creates a wrapper function inside, (3) returns the wrapper. The wrapper "decorates" the original function with extra behavior!

Think of an assembly line . Raw material goes in, passes through multiple stations (cutting → painting → packaging), and a finished product comes out. Each station is a function!

A powerful real-world example of HOF + closure:

By mastering higher-order functions and function factories, you now understand:

These concepts fuel Python's most powerful systems and make your code:

You now understand how to pass, return, and compose functions — the foundation of clean, reusable Python code.

Up next: Closures — understand how functions capture and remember variables from their outer scope.

Practice quiz

What is a higher-order function?

  • A function with many parameters
  • A function defined at the top of a file
  • A function that takes or returns another function
  • A recursive function

Answer: A function that takes or returns another function. A higher-order function takes a function as an argument and/or returns a function.

When passing a function to another, why write add_one and not add_one()?

  • add_one passes the function itself; add_one() calls it first
  • They are the same
  • add_one() is a syntax error
  • add_one returns None

Answer: add_one passes the function itself; add_one() calls it first. Without parentheses you pass the function object; with them you pass its return value.

Given apply_twice(fn, value) returning fn(fn(value)) and add_one(x)=x+1, what is apply_twice(add_one, 5)?

  • 6
  • 10
  • 5
  • 7

Answer: 7. add_one(add_one(5)) = add_one(6) = 7.

Given power(exponent) returning inner(base)=base**exponent, what does power(2)(5) return?

  • 10
  • 25
  • 7
  • 32

Answer: 25. exponent=2 is captured, so inner(5) returns 5**2 = 25.

What does power(4)(2) return for that same factory?

  • 16
  • 8
  • 6
  • 64

Answer: 16. exponent=4, so 2**4 = 16.

Given compose(f, g) returning f(g(x)), with double(n)=n*2 and add_one(n)=n+1, what does compose(double, add_one)(3) return?

  • 7
  • 9
  • 8
  • 6

Answer: 8. g runs first: add_one(3)=4, then double(4)=8.

In compose(f, g) returning f(g(x)), which function runs first?

  • f
  • g
  • Both at once
  • Neither

Answer: g. Read right-to-left: g runs first, then f is applied to its result.

What is a function factory?

  • A class that builds objects
  • A built-in module
  • A decorator library
  • A function that creates and returns other functions

Answer: A function that creates and returns other functions. A function factory is a function that builds and returns customized functions.

Which keyword does the make_counter closure need to MODIFY its captured count?

  • global
  • nonlocal
  • yield
  • lambda

Answer: nonlocal. nonlocal lets the inner function change the outer function's count variable.

The decorator syntax @track applied to greet is shorthand for what?

  • greet = track
  • track = greet(track)
  • greet = track(greet)
  • greet()

Answer: greet = track(greet). @track is syntactic sugar for greet = track(greet).