Objective-C supports blocks, which are similar to anonymous functions and can form closures. Blocks are useful when you want to define a function inline without having to name it.
To run the program, compile it with a command like:
In Objective-C, blocks serve a similar purpose to closures in other languages. They allow you to create inline, anonymous functions that can capture and store references to variables from the surrounding scope.
The __block keyword is used to allow the captured variable i to be modified within the block. Without this, i would be captured as a const copy.
The syntax int (^blockName)(void) declares a block that takes no parameters and returns an int. This is equivalent to the func() int signature in the original example.
Note that Objective-C uses NSLog for console output, which includes additional information like timestamp and process ID in its default format.
The last feature of functions we’ll look at for now is recursion, which is also supported in Objective-C.