Graphics Programs Reference
In-Depth Information
Blocks and Block Syntax
In the C programming language, we separate code into functions. A function is defined as
its own entity...
int adder(int a, int b)
{
return a + b;
}
and can be called anywhere. A function can take arguments (input) and return a value (out-
put).
In Objective-C, we separate code into classes and then further into methods that a class im-
plements. The syntax of defining and calling a method is different than a function, but
really, they are the same thing: chunks of executable code that take arguments and return
single values.
- (int)addValue:(int)a andValue:(int)b
{
return a + b;
}
The big difference between a function and a method is that a method can only be executed
by an object or class: this is the conceptual difference between procedural programming
and object-oriented programming.
Blocks are an interesting combination of object-oriented and procedural programming. A
block, like a method or function, is a chunk of executable code that can take arguments and
return a value. But a block is also an object. This odd combination gives us new design pat-
terns that weren't easily achievable when limited to functions and objects and their meth-
ods. Like the hurdle you had to jump to first understand object-oriented programming,
blocks require a similar (although less intense) leap in your understanding of programming.
Let's create a new project to learn more about blocks and their syntax. Create a new iOS
Empty Application template named Blocky and configure it as shown in Figure 27.1 .
Figure 27.1 Configuring Blocky
 
Search WWH ::




Custom Search