From the docs:
int (^Multiply)(int, int) = ^(int num1, int num2) {
return num1 * num2;
};
int result = Multiply(7, 4); // result is 28
It only looks complicated – the same thing could be done with an function, or not? What’s the real point here in this example?
Blocks’ power is as lexical closures (also known as lambdas in languages such as Python or C#). Thus, you can do
You can then pass this block around and it will keep a (copy) of
myVar. Thus a closure is really code and context and therein lies the power.