Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [13]
sleep(2);
congratulateStudent("Bo", "Objective-C", 2);
sleep(2);
congratulateStudent("Mike", "PHP and PostgreSQL", 5);
sleep(2);
congratulateStudent("Ted", "iOS", 5);
return 0;
}
Build and run the program. (Ignore the warning about an implicit declaration for now.) You should see a 2-second pause between each message of congratulations. That’s because the main function stops running until the sleep function is done sleeping.
Notice that when you call a function, you use its name and a pair of parentheses for its arguments. Thus, when we talk about functions, we usually include a pair of empty parentheses. From now on, we will say main() when we talk about the main function.
Your computer came with many functions built-in. Actually, that is a little misleading – here is the truth: Before Mac OS X was installed on your computer, it was nothing but an expensive space heater. Among the things that were installed as part of Mac OS X were files containing a collection of precompiled functions. These collections are called the standard libraries. sleep() and printf() are included in these standard libraries.
At the top of main.c, you included the file stdio.h. This file contains a declaration of the function printf() and lets the compiler check to make sure that you are using it correctly. The function sleep() is declared in stdlib.h. Include that file, too, so that the compiler will stop complaining that sleep() is implicitly declared:
#include #include void congratulateStudent(char *student, char *course, int numDays) { … The standard libraries serve two functions: They represent big chunks of code that you don’t need to write and maintain. Thus, they empower you to build much bigger, better programs than you would be able to do otherwise. They ensure that most programs look and feel similar. Programmers spend a lot of time studying the standard libraries for the operating systems that they work on. Every company that creates an operating system also has documentation for the standard libraries that come with it. You’ll learn how to browse the documentation for iOS and Mac OS X in Chapter 16. Local variables, frames, and the stack void showCookTimeForTurkey(int pounds) { int necessaryMinutes = 15 + 15 * pounds; printf("Cook for %d minutes.\n", necessaryMinutes); } necessaryMinutes is a local variable. It came into existence when showCookTimeForTurkey() started to execute and will cease to exist once that function completes execution. The parameter of the function, pounds, is also a local variable. A parameter is a local variable that has been initialized to the value of the corresponding argument. A function can have many local variables, and all of them are stored in the frame for that function. Think of the frame as a blackboard that you can scribble on while the function is running. When the function is done executing, the blackboard is discarded. Imagine for a moment that you are working on the Baked Chicken recipe. In your kitchen, all recipes get their own blackboards, so you have a blackboard for the Baked Chicken recipe ready. Now, when you call the Seasoned Bread Crumbs recipe, you need a new blackboard. Where are you going to put it? Right on top of the blackboard for Baked Chicken. After all, you’ve suspended execution of Baked Chicken to make Seasoned Bread Crumbs. You won’t need the Baked Chicken frame until the Seasoned Bread Crumbs recipe is complete and its frame is discarded. What you have now is a stack of frames. Figure 5.3 Two blackboards in a stack Programmers say, “When a function is called, its frame is created on top of the stack. When the function finishes executing, its
Every function can have local variables. Local variables are variables declared inside a function. They exist only during the execution of that function and can only be accessed from within that function. For example, imagine that you were writing a function that computed how long to cook a turkey. It might look like this: