Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [12]
Let’s get specific. In the first call to congratulateStudent, you pass three arguments: "Mark", "Cocoa", 5.
congratulateStudent("Mark", "Cocoa", 5);
For now, we’ll focus on the third argument. When 5 is passed to congratulateStudent, it is assigned to the third parameter, numDays. Arguments and parameters are matched up in the order in which they appear. They must also be the same (or very close to the same) type. Here, 5 is an integer value, and the type of numDays is int. Good.
Now, when congratulateStudent uses, or references, the numDays variable within the function, its value will be 5. You can see numDays is referenced just before the semi-colon. Finally, you can prove that all of this worked by looking at the first line of the output, which correctly displays the number of days.
Look back to our first proposed version of ClassCertificates with all the repetitive typing. What’s the point of using a function instead? To save on the typing? Well, yes, but that’s definitely not all. It’s also about error-checking. The less you type and the more the computer crunches, the fewer chances for typos. Also if you do mistype a function name, Xcode will alert you, but Xcode has no idea if you’ve mistyped text.
Another benefit to writing functions is reusability. Now that you’ve written this handy function, you could use it in another program. Making changes is simpler, too. You only need to adjust the wording of the congratulatory phrase in one place for it to take effect everywhere.
The final benefit of functions is if there is a “bug,” you can fix that one function and suddenly everything that calls it will start working properly. Partitioning your code into functions makes it easier to understand and maintain.
Now back to that warning in your code. It is pretty common to declare a function in one place and define it in another. Declaring a function just warns the compiler that a function with a particular name is coming. Defining the function is where you describe the steps that should be executed. In this exercise, you actually declared and defined your function in the same place. Because this is uncommon, Xcode issues a warning if your function was not declared in advance.
It is OK to ignore this warning in any of the projects you build in this book. Or you can take the time to disable it. To do so, select the ClassCertificates target, which is the item at the top of the project navigator. In the editor pane, select All under the Build Settings tab. Scroll through the different build settings and find the Missing Function Prototypes setting. Change this setting to No.
Figure 5.1 Disabling Missing Function Prototypes warning
How functions work together
A program is a collection of functions. When you run a program, those functions are copied from the hard drive into memory, and the processor finds the function called “main” and executes it.
Remember that a function is like a recipe card. If I began to execute the “Baked Chicken” card, I would discover that the second instruction is “Make Seasoned Bread Crumbs,” which is explained on another card. A programmer would say “the Baked Chicken function calls the Seasoned Bread Crumbs function.”
Figure 5.2 Recipe cards
Similarly, the main function can call other functions. For example, your main function in ClassCertificates called the congratulateStudent function, which in turn called printf.
While you are preparing the seasoned bread crumbs, you stop executing the “Baked Chicken” card. When the bread crumbs are ready, you resume working through the “Baked Chicken” card.
Similarly, the main function stops executing and “blocks” until the function it calls is done executing. To see this happen, we’re going to call a sleep function that does nothing but wait a number of seconds. In your main function, add a call to sleep.
int main (int argc, const char * argv[])
{
congratulateStudent("Mark",