Programming Terms: First-Class Functions
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.
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?
Why does removing parentheses change the meaning of code like f = square vs f = square(5)?
How does map illustrate first-class functions and higher-order functions?
What is the difference between passing a function and executing it when calling another function?
What does it mean to return a function from another function, and why does it matter?
How do HTML tag factories demonstrate returning functions in a practical way?
Review Questions
- In your own words, list the three operations that define first-class functions and give one example of each from the transcript.
- Explain what happens if you accidentally include parentheses when passing square into a map-like function.
- 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
First-class functions treat functions as values that can be assigned to variables, passed as arguments, and returned from other functions.
- 2
Omitting parentheses when assigning or passing a function stores the function object; including parentheses executes it immediately.
- 3
A higher-order function is one that takes functions as inputs and/or returns functions as outputs.
- 4
A map-style abstraction works by applying a provided function to each element of an array and returning the transformed results.
- 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
Function factories like logger and HTML tag show how pre-configured behavior can be created by capturing arguments and returning a callable function.