Take the 2-minute tour ×

Could someone explain? I understand the basic concepts behind them but I often see them used interchangeably and I get confused.

And now that we're here, how do they differ from a regular function?

share|improve this question
Lambdas are a language construct (anonymous functions), closures are an implementation technique to implement first-class functions (whether anonymous or not). Unfortunately, this is often confused by many people. –  Andreas Rossberg Jul 21 '12 at 16:54
Related: Function pointers, Closures, and Lambda –  legends2k Jul 14 '14 at 10:08
Nice article on JavaScript Closures –  Uri Nov 30 '14 at 9:58

8 Answers 8

up vote 368 down vote accepted

A lambda is just an anonymous function - a function defined with no name. In some languages, such as Scheme, they are equivalent to named functions. In fact, function definition is re-written as binding a lambda to a variable internally. In other languages, like Python, there are some (rather needless) distinctions between them, but they behave the same way otherwise.

A closure is any function which closes over the environment in which it was defined. This means that it can access variables not in its parameter list. Examples:

def func(): return h
def anotherfunc(h):
   return func()

This will cause an error, because func does not close over the environment in anotherfunc - h is undefined. func only closes over the global environment. This will work:

def anotherfunc(h):
    def func(): return h
    return func()

Because here, func is defined in anotherfunc, and in python 2.3 and greater (or some number like this) when they almost got closures correct (mutation still doesn't work), this means that it closes over anotherfunc's environment and can access variables inside of it. In Python 3.1+, mutation works too when using the nonlocal keyword.

Another important point - func will continue to close over anotherfunc's environment even when it's no longer being evaluated in anotherfunc. This code will also work:

def anotherfunc(h):
    def func(): return h
    return func

print anotherfunc(10)()

This will print 10.

This, as you notice, has nothing to do with lambda's - they are two different (although related) concepts.

share|improve this answer
Thanks, your answer made it more clear. –  sker Oct 21 '08 at 5:11
Claudiu, to my uncertain knowledge python has never quite got closures correct. Did they fix the mutability problem while I wasn't looking? Quite possible... –  simon Oct 21 '08 at 5:20
simon - you're right, they're still not fully correct. i think python 3.0 will add a hack to work around this. (something like a 'nonlocal' identifier). i'll change my ansewr to reflect that. –  Claudiu Oct 21 '08 at 5:26
Good, clear answer! –  David Koelle Dec 8 '09 at 3:07
@AlexanderOrlov: They are both lambdas and closures. Java had closures before via anonymous inner classes. Now that functionality has been made syntactically easier via lambda expressions. So probably the most relevant aspect of the new feature is that there are now lambdas. It's not incorrect to call them lambdas, they are indeed lambdas. Why Java 8 authors may choose not to highlight the fact that they are closures is not something I know. –  Claudiu Feb 16 at 22:51

When most people think of functions, they think of named functions:

function foo() { return "This string is returned from the 'foo' function"; }

These are called by name, of course:

foo(); //returns the string above

With lambda expressions, you can have anonymous functions:

 @foo = lambda() {return "This is returned from a function without a name";}

With the above example, you can call the lambda through the variable it was assigned to:


More useful than assigning anonymous functions to variables, however, are passing them to or from higher-order functions, i.e., functions that accept/return other functions. In a lot of these cases, naming a function is unecessary:

function filter(list, predicate) 
 { @filteredList = [];
   for-each (@x in list) if (predicate(x)) filteredList.add(x);
   return filteredList;

//filter for even numbers
filter([0,1,2,3,4,5,6], lambda(x) {return (x mod 2 == 0)});

A closure may be a named or anonymous function, but is known as such when it "closes over" variables in the scope where the function is defined, i.e., the closure will still refer to the environment with any outer variables that are used in the closure itself. Here's a named closure:

@x = 0;

function incrementX() { x = x + 1;}

incrementX(); // x now equals 1

That doesn't seem like much but what if this was all in another function and you passed incrementX to an external function?

function foo()
 { @x = 0;

   function incrementX() 
    { x = x + 1;
      return x;

   return incrementX;

@y = foo(); // y = closure of incrementX over foo.x
y(); //returns 1 (y.x == 0 + 1)
y(); //returns 2 (y.x == 1 + 1)

This is how you get stateful objects in functional programming. Since naming "incrementX" isn't needed, you can use a lambda in this case:

function foo()
 { @x = 0;

   return lambda() 
           { x = x + 1;
             return x;
share|improve this answer
what language are you ising here? –  Claudiu Oct 21 '08 at 3:53
It's basically pseudocode. There's some lisp and JavaScript in it, as well as a language I'm designing called "@" ("at"), named after the variable declaration operator. –  Mark Cidade Oct 21 '08 at 3:57
that will be a fun one to google for –  Claudiu Apr 26 '11 at 21:04
@MarkCidade, it's been 5 years... –  Pacerier Aug 29 '13 at 3:34
@Pacerier It's been longer that that actually : ) . It's been a while since I really last worked on it. I don't know if it will ever see the light of day. –  Mark Cidade Aug 29 '13 at 17:54

Not all closures are lambdas and not all lambdas are closures. Both are functions, but not necessarily in the manner we're used to knowing.

A lambda is essentially a function that is defined inline rather than the standard method of declaring functions. Lambdas can frequently be passed around as objects.

A closure is a function that encloses its surrounding state by referencing fields external to its body. The enclosed state remains across invocations of the closure.

In an object-oriented language, closures are normally provided through objects. However, some OO languages (e.g. C#) implement special functionality that is closer to the definition of closures provided by purely functional languages (such as lisp) that do not have objects to enclose state.

What's interesting is that the introduction of Lambdas and Closures in C# brings functional programming closer to mainstream usage.

share|improve this answer
So, could we say that closures are a subset of lambdas and lambdas are a subset of functions? –  sker Oct 21 '08 at 3:37
Closures are a subset of lambdas...but lambdas are more special than normal functions. Like I said, lambdas are defined inline. Essentially there is no way to reference them unless they are passed to another function or returned as a return value. –  Mike Brown Oct 21 '08 at 3:39
Lambdas and closures are each a subset of all functions, but there is only an intersection between lambdas and closures, where the non-intersecting part of closures would be named functions that are closures and non-intersecting lamdas are self-contained functions with fully-bound variables. –  Mark Cidade Oct 21 '08 at 4:06
MikeBrown - can you please modify your answer? The statement "A closure is a lambda" is very misleading, as it definitely is NOT necessarily a lambda! –  Claudiu Oct 21 '08 at 4:36
Thank you for setting me straight. I was a bit tired and lazy when I first answered. –  Mike Brown Oct 21 '08 at 5:30

It's as simple as this: lambda is a language construct, i.e. simply syntax for anonymous functions; a closure is a technique to implement it -- or any first-class functions, for that matter, named or anonymous.

More precisely, a closure is how a first-class function is represented at runtime, as a pair of its "code" and an environment "closing" over all the non-local variables used in that code. This way, those variables are still accessible even when the outer scopes where they originate have already been exited.

Unfortunately, there are many languages out there that do not support functions as first-class values, or only support them in crippled form. So people often use the term "closure" to distinguish "the real thing".

share|improve this answer

From the view of programming languages, they are completely two different things.

Basically for a Turing complete language we only needs very limited elements, e.g. abstraction, application and reduction. Abstraction and application provides the way you can build up lamdba expression, and reduction dertermines the meaning of the lambda expression.

Lambda provides a way you can abstract the computation process out. for example, to compute the sum of two numbers, a process which takes two parameters x, y and returns x+y can be abstracted out. In scheme, you can write it as

(lambda (x y) (+ x y))

You can rename the parameters, but the task that it completes doesn't change. In almost all of programming languages, you can give the lambda expression a name, which are named functions. But there is no much difference, they can be conceptually considered as just syntax sugar.

OK, now imagine how this can be implemented. Whenever we apply the lambda expression to some expressions, e.g.

((lambda (x y) (+ x y)) 2 3)

We can simply substitute the parameters with the expression to be evaluated. This model is already very powerful. But this model doesn't enable us to change the values of symbols, e.g. We can't mimic the change of status. Thus we need a more complex model. To make it short, whenever we want to calculate the meaning of the lambda expression, we put the pair of symbol and the corresponding value into an environment(or table). Then the rest (+ x y) is evaluated by looking up the corresponding symbols in the table. Now if we provide some primitives to operate on the environment directly, we can model the changes of status!

With this background, check this function:

(lambda (x y) (+ x y z))

We know that when we evaluate the lambda expression, x y will be bound in a new table. But how and where can we look z up? Actually z is called a free variable. There must be an outer an environment which contains z. Otherwise the meaning of the expression can't be determined by only binding x and y. To make this clear, you can write something as follows in scheme:

((lambda (z) (lambda (x y) (+ x y z))) 1)

So z would be bound to 1 in an outer table. We still get a function which accepts two parameters but the real meaning of it also depends on the outer environment. In other words the outer environment closes on the free variables. With the help of set!, we can make the function stateful, i.e, it's not a function in the sense of maths. What it returns not only depends on the input, but z as well.

This is something you already know very well, a method of objects almost always relies on the state of objects. That's why some people say "closures are poor man's objects. " But we could also consider objects as poor man's closures since we really like first class functions.

I use scheme to illustrate the ideas due to that scheme is one of the earliest language which has real closures. All of the materials here are much better presented in SICP chapter 3.

To sum up, lambda and closure are really different concepts. A lambda is a function. A closure is a pair of lambda and the corresponding environment which closes the lambda.

share|improve this answer
So one could substitute all closures by nested lambdas until no free variables are present anymore? In this case I would say that closures could be seen as special kind of lambdas. –  Trilarion Jun 13 '14 at 9:22

Concept is same as described above, but if you are from PHP background, this further explain using PHP code.

$input = array(1, 2, 3, 4, 5);
$output = array_filter($input, function ($v) { return $v > 2; });

function ($v) { return $v > 2; } is the lambda function definition. We can even store it in a variable, so it can be reusable:

$max = function ($v) { return $v > 2; };

$output = array_filter($input, $max);

Now, what if you want to change the maximum number allowed in the filtered array? You would have to write another lambda function or create a closure (PHP 5.3):

$max_comp = function ($max) {
  return function ($v) use ($max) { return $v > $max; };

$output = array_filter($input, $max_comp(2));

A closure is a function that is evaluated in its own environment, which has one or more bound variables that can be accessed when the function is called. They come from the functional programming world, where there are a number of concepts in play. Closures are like lambda functions, but smarter in the sense that they have the ability to interact with variables from the outside environment of where the closure is defined.

Here is a simpler example of PHP closure:

$string = "Hello World!";
$closure = function() use ($string) { echo $string; };


Nicely explained in this article.

share|improve this answer

This question is old and got many answers. Now with Java 8 and Official Lambda that are unofficial closure project it revives the question.

answer in java context : http://www.lambdafaq.org/lambdas-and-closures-whats-the-difference/ copy-pasting content :

A closure is a lambda expression paired with an environment that binds each of its free variables to a value. In Java, lambda expressions will be implemented by means of closures, so the two terms have come to be used interchangeably in the community.

share|improve this answer

Simply speaking, closure is a trick about scope, lambda is an anonymous function. We can realize closure with lambda more elegantly and lambda is often used as a parameter passed to a higher function

share|improve this answer

Your Answer


