Creating Functions in Objective-C
So far, the code in the programs we’ve discussed has executed automatically when the programs start. But functions are different; you have to explicitly call a function by name in your code before its code will run. That means that using functions, you can divide your code into smaller parts: the divide and conquer technique.
In Objective-C, functions are a crucial stop on the way to building your own objects. Objects let you package both data and functions—called methods when they’re built into objects—together, as you’ll soon see.
Here is an example of how you might create a function named greeter():
#include <stdio.h> void greeter(void) { printf("Hello there."); } . . .
Note the function’s structure: In front of its name (here, greeter), you specify a return type, which indicates the type of the data item that the function can return. Since greeter() doesn’t return any data, the return type is void.
Then, in parentheses following the name comes a list of arguments; these are the data items you pass to the function to let it do its work. Since the greeter() function takes no arguments, it uses void for the argument list as well. Then comes the body of the function—the actual code that runs when you call the function—enclosed in curly braces: { and }. In this case, the greeter() function simply displays a message: “Hello there.”
The whole thing—the line that gives the function’s return type, name, and argument list, as well as the body of the function—is called the function’s definition.
You can call the greeter() function by name to run it from the code in main()—which itself is a function:
#include <stdio.h> void greeter() { printf("Hello there."); } int main() { greeter(); return 0; }
Now when your program runs, it will start automatically by calling the main() function. The code in the main() function includes a call to the greeter() function, which then will display its message. Nice.
You’ll get the full story on functions here: how to pass data to them, how to return data from them, how to pass pointers to them to access the data in the calling code directly, how to make them call themselves (a process called recursion), and how to set up pointers to them and then call them using those pointers.
Listing 4.1. Starting function.m.
#include <stdio.h> void greeter() { printf("Hello there."); } . . .
Listing 4.2. The function.m program.
#include <stdio.h> void greeter() { printf("Hello there."); } int main() { greeter(); return 0; }
Defining a Function
In this first task, we’ll put to work the example introduced in the chapter opener: the greeter() function.
To create a function:
- Create a new program named function.m.
- In function.m, enter the code shown in Listing 4.1.
This code creates the greeter() function.
- Add the code shown in Listing 4.2.
This code adds the main() function and the call to the greeter() function.
- Save function.m.
- Run the function.m program.
You should see the following:
Hello there.