This chapter is from the book

This chapter is from the book

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():

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:

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.