Closures keep the state of their bound variables over several invocations.

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 different concepts.

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.

Closures typically appear in languages in which functions are first-class values–in other words, such languages allow functions to be passed as arguments, returned from function calls, bound to variable names, etc., just like simpler types such as strings and integers. For example, consider the following Scheme function:

; Return a list of all books with at least THRESHOLD copies sold.(define (best-selling-booksthreshold)(filter(lambda (book)(>= (book-salesbook)threshold))book-list))

In this example, the lambda expression(lambda (book) (>= (book-sales book) threshold)) appears within the function best-selling-books. When the lambda expression is evaluated, Scheme creates a closure consisting of the code for the lambda and a reference to the threshold variable, which is a free variable inside the lambda.

The closure is then passed to the filter function, which calls it repeatedly to determine which books are to be added to the result list and which are to be discarded. Because the closure itself has a reference to threshold, it can use that variable each time filter calls it. The function filter itself might be defined in a completely separate file.

Here is the same example rewritten in ECMAScript (JavaScript), another popular language with support for closures:

// Return a list of all books with at least 'threshold' copies sold.functionbestSellingBooks(threshold){returnbookList.filter(function(book){returnbook.sales>=threshold;});}

The function keyword is used here instead of lambda, and an Array.filter method [1] instead of a global filter function, but otherwise the structure and the effect of the code are the same.

A function may create a closure and return it. The following example is a function that returns a function.

In Scheme:

; Return a function that approximates the derivative of f; using an interval of dx, which should be appropriately small.(define (derivativefdx)(lambda (x)(/ (- (f(+ xdx))(fx))dx)))

In ECMAScript:

// Return a function that approximates the derivative of f// using an interval of dx, which should be appropriately small.functionderivative(f,dx){returnfunction(x){return(f(x+dx)-f(x))/dx;};}

Because the closure in this case outlives the scope of the function that creates it, the variables f and dx live on after the function derivative returns. In languages without closures, the lifetime of a local variable coincides with the execution of the scope where that variable is declared. In languages with closures, variables must continue to exist as long as any existing closures have references to them. This is most commonly implemented using some form of garbage collection.

While this is not always clarified, a closure need not be formed using an anonymous function. The Python programming language, for example, has very limited support for anonymous functions but fully supports closures. For example, one way the above ECMAScript example could be implemented in Python is:

# Return a function that approximates the derivative of f# using an interval of dx, which should be appropriately small.defderivative(f,dx):defgradient(x):return(f(x+dx)-f(x))/dxreturngradient

In this example, the function named gradient forms a closure together with the variables f and dx. This closure is then returned by the outer function named derivative. In fact, closures in Python must often be formed using named functions, where an anonymous function might be equally appropriate in other languages, because of the restrictions on lambda forms.[2]

Designers of software libraries can allow users to customize behavior by passing closures as arguments to important functions. For example, a function that sorts values can accept a closure argument that compares the values to be sorted according to a user-defined criterion.

Because closures delay evaluation–i.e., they do not "do" anything until they are called–they can be used to define control structures. For example, all Smalltalk's standard control structures, including branches (if/then/else) and loops (while and for), are defined using objects whose methods accept closures. Users can easily define their own control structures as well.

Multiple functions can be produced which close over the same environment, enabling them to communicate privately by altering that environment (in languages that allow assignment).