Get AI summaries of any video or article — Sign up free
Programming Terms: First-Class Functions thumbnail

Programming Terms: First-Class Functions

Corey Schafer·
5 min read

Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

First-class functions treat functions as values that can be assigned to variables, passed as arguments, and returned from other functions.

Briefing

First-class functions let programmers treat functions like ordinary values—assigning them to variables, passing them into other functions, and returning them from functions—rather than treating them as something that can only be called directly. That flexibility matters because it unlocks a whole toolkit of functional programming patterns, including higher-order functions, currying, and closures.

A programming language is said to have first-class functions when it treats functions as “first-class citizens.” In practical terms, functions behave like first-class objects: they can be stored in variables, supplied as arguments to other functions, and returned as results. The key mental shift is that a function’s name can refer to the function itself, not just the outcome of running it.

The transcript demonstrates this in both Python and JavaScript by focusing on assignment. In the usual case, calling a function like Square(5) produces a value (25). But first-class behavior shows up when parentheses are removed so the variable holds the function object itself: setting f = square means f refers to the function, not its execution. Once f is a function value, it can be invoked later as f(5), producing the same result as square(5). The examples emphasize a common pitfall: parentheses execute the function, while omitting them stores the function for later use.

Next comes the “pass functions around” part, illustrated with higher-order functions. A custom map implementation takes a function and an array, then applies the provided function to each element and returns a new array of results. Crucially, the function argument is passed without parentheses—so it isn’t executed immediately. Instead, execution happens inside map’s loop, where each array item is transformed. The transcript shows this working by swapping the transformation function: passing square yields squared outputs, while passing cube yields cubed outputs. This demonstrates why first-class functions are foundational for reusable abstractions.

Finally, returning a function from another function is presented as the most confusing aspect for many learners, but it’s also where closures begin to take shape. A simple logger factory (logger) accepts a message and returns an inner function (log message). When the returned function is called later, it prints the original message captured from the outer call. That “remembering” of the initial argument is the essence of a closure, even though closures are flagged as a deeper topic for later.

A more practical example uses an HTML tag factory. An outer function (HTML tag) takes a tag name like h1 or p and returns a wrapper function that accepts message text. The returned wrapper prints opening and closing tags around the message. By creating print h1 and print p ahead of time, the code demonstrates how returning functions enables parameterized behavior—useful for patterns like logging and, in Python, decorators—while also making code easier to compose once the first-class-function mindset clicks.

Cornell Notes

First-class functions treat functions as values that can be stored, passed, and returned. That means a function can be assigned to a variable (without executing it), invoked later through that variable, and supplied as an argument to other functions. When a function accepts other functions as inputs or returns functions as outputs, it becomes a higher-order function. Returning a function from another function allows the returned function to “remember” variables from the outer scope, which is the core intuition behind closures. These capabilities show up in both Python and JavaScript through examples like map (passing functions) and logger/HTML tag factories (returning functions).

What does it mean for functions to be “first-class citizens” in a programming language?

A language treats functions as first-class citizens when functions support the same kinds of operations as other values: they can be assigned to variables, passed as arguments to other functions, and returned from functions. In the transcript’s terms, this is what makes functions behave like first-class objects—entities that can be manipulated and reused rather than only executed immediately.

Why does removing parentheses change the meaning of code like f = square vs f = square(5)?

Parentheses execute a function. So square(5) runs square immediately and produces a numeric result (25). In contrast, f = square stores the function object itself in f. Later, calling f(5) executes the stored function with the argument 5, yielding 25. The transcript highlights this as a common confusion point in both Python and JavaScript.

How does map illustrate first-class functions and higher-order functions?

A map-style function takes a function and an array. It then applies the provided function to each element of the array and returns a new array of transformed values. The provided function is passed in without parentheses (so it isn’t executed right away). Inside map, the function is called for each item. Swapping square for cube changes the transformation while keeping the map logic the same—showing why passing functions is powerful.

What is the difference between passing a function and executing it when calling another function?

Passing a function means providing the function value as an argument (e.g., passing square into map without parentheses). Executing it means calling it immediately (e.g., square(5)), which would compute a result too early and break the intended abstraction. The transcript repeatedly uses this distinction to explain why parentheses are omitted when passing functions into other functions.

What does it mean to return a function from another function, and why does it matter?

Returning a function means the outer function creates an inner function and hands that inner function back as a value. The returned function can be called later like any other function variable. The transcript’s logger example returns log message from logger; when the returned function runs, it prints using the message captured from the original logger call. That captured state is the intuition behind closures.

How do HTML tag factories demonstrate returning functions in a practical way?

The outer HTML tag function takes a tag name (like h1 or p) and returns a wrapper function that accepts message text. Calling print h1('test headline') produces output with <h1>test headline</h1>. The wrapper function “remembers” the tag argument passed earlier, showing how returning functions enables parameterized behavior and reusable formatting logic.

Review Questions

  1. In your own words, list the three operations that define first-class functions and give one example of each from the transcript.
  2. Explain what happens if you accidentally include parentheses when passing square into a map-like function.
  3. Describe how returning a function enables the returned function to use data from the outer function call. What term is mentioned as the deeper concept behind that behavior?

Key Points

  1. 1

    First-class functions treat functions as values that can be assigned to variables, passed as arguments, and returned from other functions.

  2. 2

    Omitting parentheses when assigning or passing a function stores the function object; including parentheses executes it immediately.

  3. 3

    A higher-order function is one that takes functions as inputs and/or returns functions as outputs.

  4. 4

    A map-style abstraction works by applying a provided function to each element of an array and returning the transformed results.

  5. 5

    Returning a function from another function supports delayed execution and enables the returned function to retain access to outer-scope variables (closure intuition).

  6. 6

    Function factories like logger and HTML tag show how pre-configured behavior can be created by capturing arguments and returning a callable function.

Highlights

Setting f = square stores the function itself; calling f(5) later produces the result, demonstrating first-class behavior.
A custom map implementation becomes reusable because it accepts a transformation function and applies it inside a loop.
Returning an inner function from logger lets the returned function print the original message captured from the outer call—an entry point to closures.
The HTML tag example shows practical function factories: create print h1 and print p once, then supply messages later to generate tagged output.

Topics

Mentioned