Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closure vs Anonymous function (difference?) [duplicate]

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?

like image 495
Maxim Gershkovich Avatar asked Feb 06 '11 07:02

Maxim Gershkovich


People also ask

What is the difference between closures and functions?

Difference between Function and ClosureFunction is declared using func keyword whereas Closure doesn't have func keyword. Function has always name but Closure doesn't have. Function doesn't have in keyword but closure has in the keyword.

What is the difference between anonymous function and normal function?

In JavaScript, an anonymous function is something that is declared without an identification. It's the distinction between a regular and an anonymous function. An anonymous function cannot be accessed after it is created; it can only be retrieved by a variable in which it is stored as a function value.

Are closures the same as lambdas?

A lambda expression is an anonymous function and can be defined as a parameter. The Closures are like code fragments or code blocks that can be used without being a method or a class. It means that Closures can access variables not defined in its parameter list and also assign it to a variable.

What is difference between callback and closure?

a callback is executable code that is passed as an argument to other code. a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables. a callback is executable code that is passed as an argument to other code.


1 Answers

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); } 
like image 170
Anurag Avatar answered Oct 04 '22 12:10

Anurag