Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Every now and then I see "closures" being mentioned, and I tried looking it up but Wiki doesn't give an explanation that I understand. Could someone help me out here?

share|improve this question
    
If you know Java/C# hope this link will help- http://www.developerfusion.com/article/8251/the-beauty-of-closures/ –  Gulshan Jan 27 '11 at 9:53
1  
Closures are hard to understand. You should try clicking all the links in the first sentence of that Wikipedia article, and understanding those articles first. –  Zach Jan 27 '11 at 10:25
8  
2  
What's the fundamental difference between a closure and a class though? Okay, a class with only one public method. –  biziclop Jan 27 '11 at 11:00
3  
@biziclop: You could emulate a closure with a class (that's what Java devs have to do). But they're usually slightly less verbose to create and you don't have to manage manually what you're carying around. (The hardcore lispers ask a similar question, but propably arrive at that other conclusion - that language-level OO support is unnecessary when you have closures). –  delnan Jan 27 '11 at 15:14
show 6 more comments

9 Answers

up vote 53 down vote accepted

(Disclaimer: this is a basic explanation; as far as the definition goes, I'm simplifying a little bit)

The most simple way to think of a closure is a function that can be stored as a variable (referred to as a "first-class function"), that has a special ability to access other variables local to the scope it was created in.

Example (JavaScript):

var setKeyPress = function(callback) {
    document.onkeypress = callback;
}

var initialize = function() {
    var black = false;

    document.onclick = function() {
        black = !black;
        document.body.style.backgroundColor = black ? "#000000" : "transparent";
    }

    var displayValOfBlack = function() {
        alert(black);
    }

    setKeyPress(displayValOfBlack);
};

initialize();

The functions1 assigned to document.onclick and displayValOfBlack are closures. You can see that they both reference the boolean variable black, but that variable is assigned outside the function. Because black is local to the scope where the function was defined, the pointer to this variable is preserved.

If you put this in an HTML page:

  1. Click to change to black
  2. Hit [enter] to see "true"
  3. Click again, changes back to white
  4. Hit [enter] to see "false"

This demonstrates that both have access to the same black, and can be used to store state without any wrapper object.

The call to setKeyPress is to demonstrate how a function can be passed just like any variable. The scope preserved in the closure is still the one where the function was defined.

Closures are commonly used as event handlers, especially in JavaScript and ActionScript. Good use of closures will help you implicitly bind variables to event handlers without having to create an object wrapper. However, careless use will lead to memory leaks (such as when an unused but preserved event handler is the only thing to hold on to large objects in memory, especially DOM objects, preventing garbage collection).


1: Actually, all functions in JavaScript are closures.

share|improve this answer
1  
As I was reading your answer, I felt a lightbulb turn on in my mind. Much appreciated! :) –  Cyrena Jan 27 '11 at 23:30
    
@Martin - the answer does incorporate state, with the black var. But, thanks for explaining the -1. –  NickC Jan 28 '11 at 22:07
    
@Martin, have done so, and added a comment about state. Thank you! –  NickC Jan 28 '11 at 22:32
1  
Since black is declared inside a function, wouldn't that get destroyed as the stack unwinds...? –  gablin Jan 29 '11 at 9:32
2  
@gablin, that's a good question. I don't think they can't — but I only brought up garbage collection since that what JS uses and that's what you seemed to be referring to when you said "Since black is declared inside a function, wouldn't that get destroyed". Remember also that if you declare an object in a function and then assign it to a variable that lives on somewhere else, that object is preserved because there are other references to it. –  NickC Jan 30 '11 at 17:25
show 7 more comments

A closure is basically just a different way of looking at an object. An object is data that has one or more functions bound to it. A closure is a function that has one or more variables bound to it. The two are basically identical, at an implementation level at least. The real difference is in where they come from.

In object-oriented programming, you declare an object class by defining its member variables and its methods (member functions) up-front, and then you create instances of that class. Each instance comes with a copy of the member data, initialized by the constructor. You then have a variable of an object type, and pass it around as a piece of data, because the focus is on its nature as data.

In a closure, on the other hand, the object is not defined up-front like an object class, or instantiated through a constructor call in your code. Instead, you write the closure as a function inside of another function. The closure can refer to any of the outer function's local variables, and the compiler detects that and moves these variables from the outer function's stack space to the closure's hidden object declaration. You then have a variable of a closure type, and even though it's basically an object under the hood, you pass it around as a function reference, because the focus is on its nature as a function.

share|improve this answer
    
+1: Good answer. You can see a closure as an object with only one method, and an arbitrary object as a collection of closures over some common underlying data (the object's member variables). I think these two views are quite symmetric. –  Giorgio Nov 6 '12 at 11:03
    
Very good answer. It actually explains the insight of closure. –  RoboAlex Sep 8 '13 at 13:34
    
@Mason Wheeler : Where is closure data get stored? In stack like a function? Or in heap like an object? –  RoboAlex Sep 8 '13 at 13:37
    
@RoboAlex: In the heap, because it's an object that looks like a function. –  Mason Wheeler Sep 8 '13 at 14:36
add comment

A simple explanation in javascript:

var closure_example = function() {
    var closure = 0;
    // after first iteration the value will not be erased from the memory
    // because it is bound with the returned alertValue function.
    return {
        alertValue : function() {
            closure++;
            alert(closure);
        }
    };
};
closure_example();

alert(closure) Will use the previously created value of closure. The returned alertValue function's namespace will be connected to the namespace in wich the closure variable resides. When you delete the whole function the value of the closure will be deleted but until then the alertValue function will allways be able to read/write the value of variable closure.

if you run this code the first iteration will assign a value 0 to closure variable and rewrite the function to:

var closure_example = function(){
    alertValue : function(){
        closure++;
        alert(closure);
    }       
}

And because alertValue needs the local variable closure to execute the function it binds itself with the value of previously assigned local variable closure.

And now every time you call the closure_example function it will write out th incremented value of closure variable because alert(closure) is bound.

closure_example.alertValue()//alerts value 1 
closure_example.alertValue()//alerts value 2 
closure_example.alertValue()//alerts value 3
//etc 
share|improve this answer
    
thanks, i didn't test the code =) everything seems alright now. –  Muha Jan 28 '11 at 21:12
add comment

The term closure comes from the fact that a piece of code (block, function) can have free variables that are closed (i.e. bound to a value) by the environment in which the block of code is defined.

Take for example the Scala function definition:

def addConstant(v: Int): Int = v + k

In the function body there are two names (variables) v and k indicating two integer values. The name v is bound because it is declared as an argument of the function addConstant (by looking at the function declaration we know that v will be assigned a value when the function is invoked). The name k is free wrt the function addConstant because the function contains no clue as to what value k is bound to (and how).

In order to evaluate a call like:

val n = addConstant(10)

we have to assign k a value, which can only happen if the name k is defined in the context in which addConstant is defined. For example:

def increaseAll(values: List[Int]): List[Int] =
{
  val k = 2

  def addConstant(v: Int): Int = v + k

  values.map(addConstant)
}

Now that we have defined addConstant in a context where k is defined, addConstant has become a closure because all its free variables are now closed (bound to a value): addConstant can be invoked and passed around as if it were a function. Note the the free variable k is bound to a value when the closure is defined, whereas the argument variable v is bound when the closure is invoked.

So a closure is basically a function or code block that can access non-local values through its free variables after these have been bound by the context.

In many languages, if you use a closure only once you can make it anonymous, e.g.

def increaseAll(values: List[Int]): List[Int] =
{
  val k = 2

  values.map(v => v + k)
}

Note that a function with no free variables is a special case of a closure (with an empty set of free variables). Analogously, an anonymous function is a special case of an anonymous closure, i.e. an anonymous function is an anonymous closure with no free variables.

share|improve this answer
add comment

A "closure" is, in essence, some local state and some code, combined into a package. Typically, the local state comes from a surrounding (lexical) scope and the code is (essentially) an inner function that is then returned to outside. The closure is then a combination of the captured variables that the inner function sees and the code of the inner function.

It's one of those things that is, unfortunately, a bit hard to explain, due to being unfamiliar.

One analogy I successfully used in the past was "imagine we have something we call 'the book', in the room-closure, 'the book' is that copy there, over in the corner, of TAOCP, but on the table-closure, it's that copy of a Dresden Files book. So depending on what closure you're in, the code 'give me the book' results in different things happening."

share|improve this answer
    
You forgot this: en.wikipedia.org/wiki/Closure_(computer_programming) in your answer. –  S.Lott Jan 27 '11 at 11:14
2  
No, I consiously chose not to close over that page. –  Vatine Jan 27 '11 at 15:11
1  
+1 basically correct. State and function. –  Loki Astari Jan 28 '11 at 21:10
    
"State and function.": Can a C function with a static local variable be considered a closure? Do closures in Haskell involve state? –  Giorgio Nov 6 '12 at 20:43
    
@Giorgio Closures in Haskell do (I believe) close over the arguments in the lexical scope they're defined in, so, I'd say "yes" (although I am at best unfamiliar with Haskell). A C function with a static variable is, at best, a very limited closure (you really want to be able to create multiple closures from a single function, with a static local variable, you have exactly one). –  Vatine Nov 7 '12 at 8:54
show 2 more comments

A closure is a function that has associated state:

In perl you create closures like this:

#!/usr/bin/perl

# This function creates a closure.
sub getHelloPrint
{
    # Bind state for the function we are returning.
    my ($first) = @_;a

    # The function returned will have access to the variable $first
    return sub { my ($second) = @_; print  "$first $second\n"; };
}

my $hw = getHelloPrint("Hello");
my $gw = getHelloPrint("Goodby");

&$hw("World"); // Print Hello World
&$gw("World"); // PRint Goodby World

If we look at the new functionality provided with C++.
It also allows you to bind current state to the object:

#include <string>
#include <iostream>
#include <functional>


std::function<void(std::string const&)> getLambda(std::string const& first)
{
    // Here we bind `first` to the function
    // The second parameter will be passed when we call the function
    return [first](std::string const& second) -> void
    {   std::cout << first << " " << second << "\n";
    };
}

int main(int argc, char* argv[])
{
    auto hw = getLambda("Hello");
    auto gw = getLambda("GoodBye");

    hw("World");
    gw("World");
}
share|improve this answer
add comment

It is hard to define what closure is without defining the concept of 'state'.

Basically, in a language with full lexical scoping that treats functions as first class values, something special happens. If I were to do something like:

function foo(x)
return x
end

x = foo

The variable x not only references function foo() but it also references the state foo was left in last time it returned. The real magic happens when foo has other functions further defined within its scope; it's like its own mini-environment (just as 'normally' we define functions in a global environment).

Functionally it can solve many of the same problems as the C++(C?)'s 'static' keyword, which retains a local variable's state throughout multiple function calls; however it's more like applying that same principle (static variable) to a function, as functions are first class values; closure adds support for the entire function's state to be saved (nothing to do with C++'s static functions).

Treating functions as first class values and adding support for closures also means that you can have more than one instance of the same function in memory (similar to classes). What this means is you can re-use the same code without having to reset the function's state, as is required when dealing with C++ static variables inside a function(may be wrong about this?).

Here is some testing of Lua's closure support.

--Closure testing
--By Trae Barlow
--

function myclosure()
    print(pvalue)--nil
    local pvalue = pvalue or 10
    return function()
        pvalue = pvalue + 10 --20, 31, 42, 53(53 never printed)
        print(pvalue)
        pvalue = pvalue + 1 --21, 32, 43(pvalue state saved through multiple calls)
        return pvalue
    end
end

x = myclosure() --x now references anonymous function inside myclosure()

x()--nil, 20
x() --21, 31
x() --32, 42
    --43, 53 -- if we iterated x() again

results:

nil
20
31
42

It can get tricky, and it probably varies from language to language, but it seems in Lua that whenever a function is executed, its state is reset. I say this because the results from the code above would be different if we were accessing the myclosure function / state directly (instead of through the anonymous function it returns), as pvalue would be reset back to 10; but if we access myclosure's state through x (the anonymous function) you can see that pvalue is alive and well somewhere in memory. I suspect there is a bit more to it, perhaps someone can better explain the nature of the implementation.

PS: I don't know a lick of C++11 (other than what's in previous versions) so do note that this isn't a comparison between closures in C++11 and Lua. Also, all the 'lines drawn' from Lua to C++ are similarities as static variables and closures are not 100% the same; even if they are sometimes used to solve similar problems.

The thing I'm not sure of is, in the code example above, whether the anonymous function or the higher order function is considered the closure?

share|improve this answer
add comment

Let's consider a simple function:

function f1(x) {
    // ... something
}

This function is called a top-level function because it's not nested within any other function. Every JavaScript function associates with itself a list of objects called a "Scope Chain". This scope chain is an ordered list of objects. Each of these objects defines some variables.

In top-level functions, the scope chain consists of a single object, the global object. For example, the function f1 above has a scope chain that has a single object in it that defines all the global variables. (note that the term "object" here does not mean JavaScript object, it's just an implementation defined object that acts as a variable container, in which JavaScript can "look up" variables.)

When this function is invoked, JavaScript creates something called an "Activation object", and puts it at the top of the scope chain. This object contains all the local variables (for example x here). Hence now we have two objects in the scope chain: the first is the activation object and beneath it is the global object.

Note very carefully that the two objects are put into the scope chain at DIFFERENT times. The global object is put when the function is defined (i.e., when JavaScript parsed the function and created the function object), and the activation object enters when the function is invoked.

So, we now know this:

  • Every function has a scope chain associated with it
  • When the function is defined (when the function object is created), JavaScript saves a scope chain with that function
  • For top level functions, the scope chain contains only the global object at the function definition time and adds an additional activation object on top at invocation time

The situation gets interesting when we deal with nested functions. So, let's create one:

function f1(x) {

    function f2(y) {
        // ... something
    }

}

When f1 gets defined we get a scope chain for it containing only the global object.

Now when f1 gets called, the scope chain of f1 gets the activation object. This activation object contains the variable x and the variable f2 which is a function. And, note that f2 is getting defined. Hence, at this point, JavaScript also saves a new scope chain for f2. The scope chain saved for this inner function is the current scope chain in effect. The current scope chain in effect is that of f1's. Hence f2's scope chain is f1's current scope chain -- which contains the activation object of f1 and the global object.

When f2 is called, it's gets it's own activation object containing y, added to its scope chain which already contains the activation object of f1 and the global object.

If there were another nested function defined within f2, its scope chain would contain three objects at definition time (2 activation objects of two outer functions, and the global object), and 4 at the invocation time.

So, now we understand how scope chain works but we haven't talked about closures yet.

The combination of a function object and a scope (a set of variable bindings) in which the function’s variables are resolved is called a closure in the computer science literature -- JavaScript the definitive guide by David Flanagan

Most functions are invoked using the same scope chain that was in effect when the function was defined, and it doesn’t really matter that there is a closure involved. Closures become interesting when they are invoked under a different scope chain than the one that was in effect when they were defined. This happens most commonly when a nested function object is returned from the function within which it was defined.

When the function returns, that activation object is removed from the scope chain. If there were no nested functions, there are no more references to the activation object and it gets garbage collected. If there were nested functions defined, then each of those functions has a reference to the scope chain, and that scope chain refers to the activation object.

If those nested functions objects remained within their outer function, however, then they themselves will be garbage collected, along with the activation object they referred to. But if the function defines a nested function and returns it or stores it into a property somewhere, then there will be an external reference to the nested function. It won’t be garbage collected, and the activation object it refers to won’t be garbage collected either.

In our above example, we don't return f2 from f1, hence, when a call to f1 returns, its activation object will be removed from its scope chain and garbage collected. But if we had something like this:

function f1(x) {

    function f2(y) {
        // ... something
    }

    return f2;
}

Here, the returning f2 will have a scope chain that will contain the activation object of f1, and hence it won't be garbage collected. At this point, if we call f2, it will be able to access f1's variable x even though we're out of f1.

Hence we can see that a function keeps it's scope chain with it and with the scope chain come all the activation objects of outer functions. This is the essence of closure. We say that functions in JavaScript are "lexically scoped", meaning that they save the scope that was active when they were defined as opposed to the scope that was active when they got called.

There are a number of powerful programming techniques that involve closures like approximating private variables, event driven programming, partial application, etc.

Also note that all of this applies to all those languages that support closures. For example PHP (5.3+), Python, Ruby, etc.

share|improve this answer
add comment

A closure is a compiler optimization (aka syntactic sugar?). Some people have referred to this as the Poor Man's Object as well.

See the answer by Eric Lippert : (excerpt below)

The compiler will generate code like this:

private class Locals
{
  public int count;
  public void Anonymous()
  {
    this.count++;
  }
}

public Action Counter()
{
  Locals locals = new Locals();
  locals.count = 0;
  Action counter = new Action(locals.Anonymous);
  return counter;
}

Make sense?

Also, you asked for comparisons. VB and JScript both create closures in pretty much the same way.

share|improve this answer
    
This answer is a CW because I don't deserve points for Eric's great answer. Please upvote it as you see fit. HTH –  makerofthings7 Jan 27 '11 at 22:43
2  
-1: Your explanation is too root in C#. Closure is used in many languages and is much more than syntactic sugar in these languages and encompass both the function and state. –  Loki Astari Jan 28 '11 at 21:09
    
No, a closure is neither just a "compiler optimization" nor syntactic sugar. -1 –  Matt Fenwick Nov 6 '12 at 12:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.