4

Possible Duplicate:
What is a ‘Closure’?

I read more and more about closures but I don't really get what it is from a practical standpoint. I read the wikipedia page on it but it doesn't really clear anything up for me because I have more of a practical background in programming (self taught) as opposed to a computer science background. If this is a reundant question, I apologize as my initial search didn't yield anything that really answered it for me.

edit: Thanks for pointing me in the right direction! I see this has already been clearly answered before so I will close the question out.

Community
  • 1
  • 1
MattC
  • 12,285
  • 10
  • 54
  • 78
  • 1
    See: http://stackoverflow.com/questions/36636/what-is-a-closure – Shog9 Jul 05 '09 at 04:00
  • So lets see if I've been able to distil all this wisdom: It's the case when an anonymous inner function is able to access external variables that would normally be out-of-scope at the time of execution, but not declaration? – MattC Jul 05 '09 at 04:16

5 Answers5

5

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

And so does Kyle at SO

Community
  • 1
  • 1
Sev
  • 15,401
  • 9
  • 56
  • 75
1

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

Arnkrishn
  • 29,828
  • 40
  • 114
  • 128
  • True, but I don't think that's the type of closure than he was looking for. – JW. Oct 21 '09 at 03:05
  • 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. – mxk Nov 07 '09 at 17:10
1

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
  • 178,213
  • 47
  • 333
  • 501
1

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
  • 69,903
  • 20
  • 143
  • 156
1

"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
  • 854,459
  • 170
  • 1,222
  • 1,395