Online Book Reader

Home Category

Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [14]

By Root 472 0
frame is popped off the stack and destroyed.”

Let’s look more closely at how the stack works by putting showCookTimeForTurkey() into a hypothetical program:

void showCookTimeForTurkey(int pounds)

{

int necessaryMinutes = 15 + 15 * pounds;

printf("Cook for %d minutes.\n", necessaryMinutes);

}

int main(int argc, const char * argv[])

{

int totalWeight = 10;

int gibletsWeight = 1;

int turkeyWeight = totalWeight - gibletsWeight;

showCookTimeForTurkey(turkeyWeight);

return 0;

}

Recall that main() is always executed first. main() calls showCookTimeForTurkey(), which begins executing. What, then, does this program’s stack look like just after pounds is multiplied by 15?

Figure 5.4 Two frames on the stack

The stack is always last-in, first-out. That is, the showCookTimeForTurkey() will pop its frame off the stack before main() pops its frame off the stack.

Notice that pounds, the single parameter of showCookTimeForTurkey(), is part of the frame. Recall that a parameter is a local variable that has been assigned the value of the corresponding argument. For this example, the variable turkeyWeight with a value of 9 is passed as an argument to showCookTimeForTurkey(). Then that value is assigned to the parameter pounds and copied to the function’s frame.

Recursion


Can a function call itself? You bet! We call that recursion. There is a notoriously dull song called “99 Bottles of Beer.” Create a new C Command Line Tool named BeerSong. Open main.c and add a function to write out the words to this song and then kick it off in main():

#include

void singTheSong(int numberOfBottles)

{

if (numberOfBottles == 0) {

printf("There are simply no more bottles of beer on the wall.\n");

} else {

printf("%d bottles of beer on the wall. %d bottles of beer.\n",

numberOfBottles, numberOfBottles);

int oneFewer = numberOfBottles - 1;

printf("Take one down, pass it around, %d bottles of beer on the wall.\n",

oneFewer);

singTheSong(oneFewer); // This function calls itself!

printf("Put a bottle in the recycling, %d empty bottles in the bin.\n",

numberOfBottles);

}

}

int main(int argc, const char * argv[])

{

singTheSong(99);

return 0;

}

Build and run the program. The output looks like this:

99 bottles of beer on the wall. 99 bottles of beer.

Take one down, pass it around, 98 bottles of beer on the wall.

98 bottles of beer on the wall. 98 bottles of beer.

Take one down, pass it around, 97 bottles of beer on the wall.

97 bottles of beer on the wall. 97 bottles of beer.

1 bottles of beer on the wall. 1 bottles of beer.

Take one down, pass it around, 0 bottles of beer on the wall.

There are simply no more bottles of beer on the wall.

Put a bottle in the recycling, 1 empty bottles in the bin.

Put a bottle in the recycling, 2 empty bottles in the bin.

Put a bottle in the recycling, 98 empty bottles in the bin.

Put a bottle in the recycling, 99 empty bottles in the bin.

What does the stack look like when the last bottle is taken off the wall?

Figure 5.5 Frames on the stack for a recursive function

Discussing the frames and the stack is usually not covered in a beginning programming course, but I’ve found the ideas to be exceedingly useful to new programmers. First, it gives you a more concrete understanding of the answers to questions like “What happens to my local variables when the function finishes executing?” Second, it helps you understand the debugger. The debugger is a program that helps you understand what your program is actually doing, which, in turn, helps you find and fix “bugs” (problems in your code). When you build and run a program in Xcode, the debugger is attached to the program so that you can use it.

Looking at the frames in the debugger


You can use the debugger to browse the frames on the stack. To do this, however, you have to stop your program in mid-execution. Otherwise, main() will finish executing, and there won’t be any frames left to look at. To see as many frames as possible in our BeerSong program, we want to halt execution

Return Main Page Previous Page Next Page

®Online Book Reader