Closure (computer science)

related topics
{math, number, function}
{work, book, publish}
{film, series, show}
{system, computer, user}
{day, year, event}
{language, word, form}
{style, bgcolor, rowspan}

In computer science, a closure is a first-class function with free variables that are bound in the lexical environment. Such a function is said to be "closed over" its free variables. A closure is defined within the scope of its free variables, and the extent of those variables is at least as long as the lifetime of the closure itself. The explicit use of closures is associated with functional programming and with languages such as ML and Lisp. Closures are used to implement continuation passing style, and in this manner, hide state. Constructs such as objects and control structures can thus be implemented with closures.

The concept of closures was developed in the 1960s and was first fully implemented as a language feature in the programming language Scheme. Since then, many languages have been designed to support closures.

In some languages, a closure may occur when a function is defined within another function, and the inner function refers to local variables of the outer function. At runtime, when the outer function executes, a closure is formed, consisting of the inner function’s code and references to any variables of the outer function required by the closure; such variables are called the upvalues of the closure.

The term closure is often mistakenly used to mean anonymous function. This is probably because most languages implementing anonymous functions allow them to form closures and programmers are usually introduced to both concepts at the same time. These are, however, distinct concepts.

Closures are closely related to function objects; the transformation from the former to the latter is known as defunctionalization or lambda lifting.

Contents

Full article ▸

related documents
Continued fraction
Fast Fourier transform
Logarithm
Axiom of choice
Tensor product
Adjoint functors
Algebraic geometry
Variable
Hilbert's tenth problem
Singleton pattern
Functional programming
Dedekind domain
Orthogonal matrix
Kolmogorov complexity
Scheme (programming language)
Fourier series
Combinatorics
Class (computer science)
Numeral system
Calculus
Limit (category theory)
LR parser
Design Patterns
Lie group
Elliptic curve cryptography
Binary search tree
Riemann integral
Recurrence relation
Μ-recursive function
Axiom