More on blocks

Here are some other things you can do with blocks.

Return values

The block that you created for VowelMovement does not have a return value, but many blocks will. When a block returns a value, you can get the return value by calling the block variable like a function.

Let’s look again at one of the sample blocks you saw at the beginning of the chapter:

^(double dividend, double divisor) {
    double quotient = dividend / divisor;
    return quotient;
}

This block takes two doubles and returns a double. To store this block in a variable, you would declare a variable of that type and assign the block to it:

// Declare divBlock variable double (^divBlock)(double,double); // Assign block to variable divBlock = ...

Get Objective-C Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.