AQA 7517, section 4.12.1.2
First-class objects
A first-class object (or first-class value) is a value that a programming language treats like any other value. It may:
- appear in expressions;
- be assigned to a variable;
- be passed as an argument to a function;
- be returned as the result of a function call.
You already use first-class objects all the time. Integers, floating-point values, characters and strings are first-class objects in many languages: you can store a number in a variable, pass it to a function, and return it from one.
Functions as first-class objects
In functional programming languages, and in imperative languages that support them (such as Python), a function is a first-class object. This means a function can be:
- assigned to a variable;
- passed as an argument to another function;
- returned as the result of a function call;
- used in an expression (for example stored in a list).
The examples below use this function:
Assigning a function to a variable
Let . Now refers to the same function, so
The name double is just a label for a function value, and is another label for the same value.
Passing a function as an argument
A function can take another function as one of its arguments:
Here the function double is the argument .
Returning a function from a function
A function can give a new function as its result:
So returns a function; call it add5. Then
Storing functions in a data structure
A list can hold functions, just as it holds numbers:
Applying each one in turn to gives .
Why does it matter?
First-class functions are what make the main tools of functional programming possible.
- Higher-order functions (4.12.2.1), such as map, filter and reduce, take functions as arguments.
- Partial application (4.12.1.4) and composition (4.12.1.5) return functions as results.
- They let you build small, reusable pieces and combine them.
A function that takes a function as an argument, or returns one, is called a higher-order function.
Languages where functions aren't first-class
In some languages, functions can only be called, and can't be stored in variables, passed around or returned. In a language such as that, none of the above is possible, and many functional techniques can't be used.
Worked example
Task: a function applyAll takes a list of functions and a value, and returns the list of results of applying each function to the value. Work out
and explain which first-class properties it uses.
- , and , so the result is .
- The functions square, negate and abs are stored in a list (used in an expression).
- The list of functions is passed as an argument to applyAll.
- Inside applyAll, each function in turn is assigned to a variable and applied to the value.
Exam tip: give the four properties of a first-class object (appear in expressions, be assigned to a variable, be passed as an argument, be returned from a function), and say that, in functional languages, a function has all of them.
Check yourself
What does it mean for a function to be a first-class object?
It can be treated like any other value: used in expressions, assigned to a variable, passed as an argument to another function, and returned as the result of a function call.
Give an example of a function being passed as an argument
For example, map(double, [1, 2, 3]) passes the function double to map, giving [2, 4, 6].
What is a higher-order function?
A function that takes a function as an argument, returns a function as a result, or both.
Name two things other than functions that are first-class objects in many languages
Any two of: integers, floating-point values, characters, strings.