78

Possible Duplicates:
What is Closures/Lambda in PHP or Javascript in layman terms?
What is the difference between a 'closure' and a 'lambda'?

Hi,

I have been unable to find a definition that clearly explains the differences between a closure and an anonymous function.

Most references I have seen clearly specify that they are distinct "things" yet I can't seem to get my head around why.

Could someone please simplify it for me? What are the specific differences between these two language features? Which one is more appropriate in what scenarios?

Community
  • 1
  • 1
Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243

1 Answers1

110

An anonymous function is just a function that has no name; nothing more. A closure is a function that captures the state of the surrounding environment.

An anonymous function does not necessarily need to create a closure, and a closure is not created only for anonymous functions.

Consider this hypothetical counter-example. Consider a language Foo which does not support closures but supports anonymous functions. This language may either not compile or throw an error for the code below because "greeting" is not defined in the scope of the inner function. The fact that it is anonymous is irrelevant.

function outer() {
    var greeting = "hello ";

    (function(name) {
        alert(greeting + name);
    })("John Doe");
}

Let's consider an actual language now that does support closures - JavaScript. Taking the same example as above, but naming the inner function this time gives:

function outer() {
    var greeting = "hello ";

    (function inner(name) {
        alert(greeting + name);
    })("John Doe");
}

Although the inner function is not anonymous anymore, it still captures state from the surrounding environment.

Closures provide much needed convenience, as otherwise we would be passing every single dependency of the function as an argument.

function outer() {
    var greeting = "hello ";

    (function(name, greeting) {
        alert(greeting + name);
    })("John Doe", greeting);
}
Anurag
  • 140,337
  • 36
  • 221
  • 257
  • 2
    Can you cite some sources for the definitions used in your answer? – Pacerier Aug 27 '13 at 08:40
  • 2
    @Pacerier I haven't used any definitions other than what is already referenced in the question, and that's basically `closures`, and `anonymous functions`. I came up with the examples on my own, so can't provide external references for those. – Anurag Aug 27 '13 at 19:50
  • What I mean is referencing from a more definite source like from Wikipedia etc – Pacerier Aug 29 '13 at 03:31
  • Turtally agree Anurag. If you really don't believe him, @Pacerier ;) -> [Closures - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures) – Dominic May 03 '14 at 07:39
  • Great answer -- however your answer is slightly unclear in that it seems to suggest that JavaScript only creates closures for named functions, i.e. `(function inner(name) {...})("John Doe");`, rather than all functions (as it does). – Niels Abildgaard Sep 02 '14 at 07:54
  • 1
    To clarify, leaving out the parameter `greeting` from the anonymous function in your last example would still result in valid code, because of JavaScripts (highly unintuitive) scoping model. – Niels Abildgaard Sep 02 '14 at 07:55
  • 4
    @NielsAbildgaard - good point, I was just illustrating what it would look like in a language that does not have closures. It's JavaScript syntax and that makes it confusing but the idea was a generic language without closures. – Anurag Jan 16 '15 at 23:09
  • 4
    It seems that PHP's official docs use "closure" and "anonymous function" interchangeably, even though their closures don't capture surrounding state without using the 'use' language construct. "Anonymous functions, also known as closures, allow the creation of functions which have no specified name." http://php.net/manual/en/functions.anonymous.php – Buttle Butkus Apr 05 '16 at 23:12
  • The PHP definition of Closure is different. See http://php.net/manual/en/class.closure.php – PaulH Nov 02 '16 at 12:49