views:

144

answers:

2

I know it's not executed immediatly, but then when?

+4  A: 

Yes, it needn't have a name at declaration time, but it can be bound to a name at runtime.

For example in Python:

def do_it(some_func):
    # call some_func
    result = some_func()
    # do something else

do_it(lambda: 42)

So the lambda just returns "42" and doesn't have a name. But when it's passed to the function do_it, it gets bound to the some_func parameter name. So there is a name to call, which is then called.

ars
+1  A: 

An anonymous function can also be executed as soon as defined, without binding it to any name at runtime. Javascript example:

var o = "hello world";
(function(msg) {
  alert(msg);
})(o);

This is commonly used to create scopes.

Mauricio Scheffer