A function (often anonymous) bound to the referencing environment in its original lexical scope in such a way that it will still have access to that environment (its variables and other references) if executed outside that scope.

It is the binding between the function and its original scope which distinguishes a closure from a simple anonymous function. Many imperative languages (e.g. Pascal and C) support function pointers, but when the referenced function is executed, it will only have access to its internal variables and to the referencing environment of the scope in which it is executed.

Closures have been closely associated with functional programming since the invention of the Scheme language (in which all lambda functions are closures). While it is possible to construct a functional language without closures (the first versions of LISP did not have them), all modern functional languages currently support them.

Closures are found much less frequently in imperative languages (while Smalltalk had them, Java still does not - though they are promised in version 8). Dynamic languages (e.g Perl, Ruby, JavaScript) have historically been much more forward in this area than their non-dynamic cousins.

history | show excerpt | excerpt history