First-class function

first-classfirst classFirst class functionfirst class proceduresfirst-class functionsfirst-class objectsfunctionfunction passingFunction referencefunctions
In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens.wikipedia
140 Related Articles

Anonymous function

lambda expressionanonymous functionslambda expressions
Some programming language theorists require support for anonymous functions (function literals) as well. There are certain implementation difficulties in passing functions as arguments or returning them as results, especially in the presence of non-local variables introduced in nested and anonymous functions.
Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfill the same role for the function type as literals do for other data types.

First-class citizen

first-class objectfirst-classfirst class
In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens.

Scheme (programming language)

SchemeScheme programming languageR6RS
Proper support for lexically scoped first-class functions was introduced in Scheme and requires handling references to functions as closures instead of bare function pointers, which in turn makes garbage collection a necessity. Functional programming languages, such as Scheme, ML, Haskell, F#, and Scala, all have first-class functions. The later Scheme and Common Lisp dialects do have lexically scoped first-class functions.
Scheme uses strictly but dynamically typed variables and supports first class procedures.

Funarg problem

Funargdownwards funarg problemupwards funarg problem
Historically, these were termed the funarg problems, the name coming from "function argument".
In computer science, the funarg problem refers to the difficulty in implementing first-class functions (functions as first-class objects) in programming language implementations so as to use stack-based memory allocation of the functions.

Functional programming

functionalfunctional programming languagefunctional language
First-class functions are a necessity for the functional programming style, in which the use of higher-order functions is a standard practice.
Higher-order functions are closely related to first-class functions in that higher-order functions and first-class functions both allow functions as arguments and results of other functions.

Function type

FunctionarrowExponential type
In languages with first-class functions, the names of functions do not have any special status; they are treated like ordinary variables with a function type.

Map (higher-order function)

map map function
A simple example of a higher-ordered function is the map function, which takes, as its arguments, a function and a list, and returns the list formed by applying the function to each member of the list.
In languages which support first-class functions, may be partially applied to lift a function that works on only one value to an element-wise equivalent that works on an entire container; for example, is a Haskell function which squares each element of a list.

Non-local variable

non-local referencenonlocals
There are certain implementation difficulties in passing functions as arguments or returning them as results, especially in the presence of non-local variables introduced in nested and anonymous functions.
Non-local variables are the primary reason it is difficult to support nested, anonymous, higher-order and thereby first-class functions in a programming language.

JavaScript

Server-side JavaScriptclient-side JavaScriptJS
Many scripting languages, including Perl, Python, PHP, Lua, Tcl/Tk, JavaScript and Io, have first-class functions.
JavaScript has curly-bracket syntax, dynamic typing, prototype-based object-orientation, and first-class functions.

Higher-order function

higher-order functionshigher order functionfunctional form
First-class functions are a necessity for the functional programming style, in which the use of higher-order functions is a standard practice.
Defunctionalization can be used to implement higher-order functions in languages that lack first-class functions:

ML (programming language)

MLML programming languageML family
Functional programming languages, such as Scheme, ML, Haskell, F#, and Scala, all have first-class functions.
Features of ML include a call-by-value evaluation strategy, first-class functions, automatic memory management through garbage collection, parametric polymorphism, static typing, type inference, algebraic data types, pattern matching, and exception handling.

Closure (computer programming)

closuresclosurelexical closure
Proper support for lexically scoped first-class functions was introduced in Scheme and requires handling references to functions as closures instead of bare function pointers, which in turn makes garbage collection a necessity.
In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions.

Function pointer

function pointerspointers to functionsas an argument
Proper support for lexically scoped first-class functions was introduced in Scheme and requires handling references to functions as closures instead of bare function pointers, which in turn makes garbage collection a necessity.
In other languages that support first-class functions, functions are regarded as data, and can be passed, returned, and created dynamically directly by other functions, eliminating the need for function pointers.

Common Lisp

QuicklispANSI Common LispArmed Bear Common Lisp
The later Scheme and Common Lisp dialects do have lexically scoped first-class functions.
Common Lisp supports first-class functions.

Perl

Perl 5Perl programming languagePerl Script
Many scripting languages, including Perl, Python, PHP, Lua, Tcl/Tk, JavaScript and Io, have first-class functions.
Perl 5 added features that support complex data structures, first-class functions (that is, closures as values), and an object-oriented programming model.

Lua (programming language)

LuaLua programming languageLuaJIT
Many scripting languages, including Perl, Python, PHP, Lua, Tcl/Tk, JavaScript and Io, have first-class functions.
Similarly, Lua allows programmers to implement namespaces, classes, and other related features using its single table implementation; first-class functions allow the employment of many techniques from functional programming; and full lexical scoping allows fine-grained information hiding to enforce the principle of least privilege.

Function object

functorfunction objectsfunctors
Many modern (and some older) languages, e.g. C++, Eiffel, Groovy, Lisp, Smalltalk, Perl, PHP, Python, Ruby, Scala, and many others, support first-class function objects and may even make significant use of them.

Clojure

Rich HickeyClojureScriptClojure programming language
As a Lisp dialect, Clojure supports functions as first-class objects, a read–eval–print loop (REPL), and a macro system.

Scope (computer science)

scopelexical scopeLexical
Proper support for lexically scoped first-class functions was introduced in Scheme and requires handling references to functions as closures instead of bare function pointers, which in turn makes garbage collection a necessity. The early functional language Lisp took the approach of dynamic scoping, where non-local variables refer to the closest definition of that variable at the point where the function is executed, instead of where it was defined.
Correct implementation of static scope in languages with first-class nested functions is not trivial, as it requires each function value to carry with it a record of the values of the variables that it depends on (the pair of the function and this environment is called a closure).

Swift (programming language)

SwiftSwift 3Swift programming language

Computer science

computer scientistcomputer sciencescomputer scientists
In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens.

Programming language

programming languageslanguagedialect
In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens.

Subroutine

functionfunctionssubroutines
In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens.