Closure (computer programming)
![]() | The English used in this article or section may not be easy for everybody to understand. (December 2011) |
In computer science, a closure is a function that has an environment of its own. In this environment, there is at least one bound variable. Closures were first used in computer programming languages such as ML and Lisp.
When a closure is used more than once, the bound variables stay the same in between.
Anonymous functions are sometimes wrongly called closures. This is probably because most languages that have anonymous functions also have closures and it is common for programmers to learn about both at the same time. Closures and anonymous functions are similar but have important differences.
Computer scientists worked out how closures should work in the 1960s and programmers were first able to use them in the Scheme programming language. Since then, many languages have been designed to support closures.
Function objects are sometimes also called closures.
Closures and first-class functions
Closures are normal in languages where functions are first-class values, meaning they can be moved around in the program like other variable types: returned by functions, bound to a variable name, etc. For example, take a look at the following Scheme function:
; Return a list of all books with at least THRESHOLD copies sold.
(define (best-selling-books threshold)
(filter
(lambda (book) (>= (book-sales book) threshold))
book-list))
In this example, the lambda expression (lambda (book) (>= (book-sales book) threshold))
is part of the function best-selling-books
. When the function is run, Scheme needs to determine the value of the lambda. It does this by creating a closure with 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 and the function runs it on each book in the list to determine which books to add. 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.
function bestSellingBooks(threshold) {
return bookList.filter(
function(book) { return book.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 code does the same thing in the same way.
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 (derivative f dx)
(lambda (x) (/ (- (f (+ x dx)) (f x)) dx)))
In ECMAScript:
// Return a function that approximates the derivative of f
// using an interval of dx, which should be appropriately small.
function derivative(f, dx) {
return function(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.
def derivative(f, dx):
def gradient(x):
return (f(x + dx) - f(x)) / dx
return gradient
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]
Uses of closures
Closures have many uses:
- 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).
In Scheme
(define foo #f)
(define bar #f)
(let ((secret-message "none"))
(set! foo (lambda (msg) (set! secret-message msg)))
(set! bar (lambda () secret-message)))
(display (bar)) ; prints "none"
(newline)
(foo "meet me by the docks at midnight")
(display (bar)) ; prints "meet me by the docks at midnight"
Note: Some speakers call any data structure that binds a lexical environment a closure, but the term usually refers specifically to functions.