+4  A: 

Eric Lippert's blog does a pretty good job at explaining this in a practical sense.

And so does Kyle at SO

Sev
+1  A: 

Hi

An operation is said to be closed over a set when the result of that operation also belongs to that set.

For example - Consider a set of positive integers. Addition is a closed operation over this set because adding two positive integers would always give a positive integer.

However subtraction is not closed over this set because 2 - 3 would give -1 which does not belong to the set of positive integers.

cheers

Andriyev
True, but I don't think that's the type of closure than he was looking for.
JW
That's the definition of a closure in linear algebra and set theory. What he was looking for is the definition of a closure in programming languages.
Matthias
+1  A: 

Two one sentence summaries:

• a closure is the local variables for a function - kept alive after the function has returned, or

• a closure is a stack-frame which is not deallocated when the function returns. (as if a 'stack-frame' were malloc'ed instead of being on the stack!)

http://blog.morrisjohns.com/javascript_closures_for_dummies

Robert Harvey
+1  A: 

probably best demonstrated by an example

program output (artificially) prefixed by *

Javascript:

js> function newCounter() {
    var i = 0;      
    var counterFunction = function () {
        i += 1; 
        return i;
    }
    return counterFunction;
}
js> aCounter = newCounter()
* function () {
*     i += 1;
*     return i;
* }
js> aCounter()
* 1
js> aCounter()
* 2
js> aCounter()
* 3
js> bCounter = newCounter()
* function () {
*     i += 1;
*     return i;
* }
js> bCounter()
* 1
js> aCounter()
* 4
cobbal
+1  A: 

"Real world language" is a difficult measure to gauge objectively, but I'll give it a try since after all I also lack a CS background (EE major here) and am a little self-taught;-)

In many languages, a function "sees" more than one "scope" (group) of variables -- not only its local variables, and that of the module or namespace it's in, but also (if it's within another function) the local variables of the function that contains it.

So, for example (in Python, but many other languages work similarly!):

def outer(haystack):

  def inner(needle):
    eldeen = needle[::-1]
    return (needle in haystack) or (eldeen in haystack)

  return [x for x in ['gold','silver','diamond','straw'] if inner(x)]

inner can "see" haystack without needing to see it as an argument, just because its containing function outer has haystack "in scope" (i.e. "visible"). So far, so clear, I hope -- and this isn't yet about closures, it's about lexical scoping.

Now suppose the outer function (in a language treating functions as first-class objects, and in particular allowing them to be returned as results of other functions) the outer function returns the inner one instead of just calling it internally (in Python, that's for example what usually happens when you use decorator syntax @something).

How can the inner function (returned as a result) still refer to the outer function's variable, since the outer function has finished?

The answer is exactly this "closure" business -- those variables from the outer function which the inner (returned) function may still need are preserved and attached as attributes of the inner-function object that's returned.

Alex Martelli