First-Class Function

A first-class function is a function that the language treats as an ordinary value. That means a function can be bound to a name, passed as an argument to another function, returned as the result of a function, and stored inside data structures, exactly like a number or a string. When functions have these privileges, they are said to be “first-class.”

“Structure and Interpretation of Computer Programs” lists the rights and privileges that mark an element of a language as first-class: it may be named by variables, passed as an argument, returned as a result, and included in data structures. SICP notes that procedures in its dialect have full first-class status, and that giving procedures this status places great demands on a language but yields enormous expressive power.

This property is a prerequisite for higher-order functions, which take or return functions, and for closures, which are functions paired with their surrounding environment. Without the ability to treat a function as a value, none of those constructions would be possible.

First-class functions are not a recent invention. LISP, designed in the late 1950s, allowed functions to be passed and returned as values from the start, and the idea is now standard in functional languages and common in mainstream languages such as JavaScript. The concept turns functions from fixed pieces of program text into data that other code can manipulate.