Online Book Reader

Home Category

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

By Root 487 0
on the line that prints “There are simply no more bottles of beer on the wall.”

How do we do this? In main.c, find the line

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

There are two shaded columns to the left of your code. Click on the wider, lefthand column next to this line of code.

Figure 5.6 Setting a breakpoint

The blue indicator shows that you’ve set a breakpoint. A breakpoint is a location in code where you want the debugger to pause the execution of your program. Run the program again. It will start and then stop right before it executes the line where you set the breakpoint.

Now your program is temporarily frozen in time, and you can examine it more closely. In the navigator area, click the icon to open the debug navigator. This navigator shows all the frames currently on the stack, also called a stack trace.

In the stack trace, frames are identified by the name of their function. Given your program consists almost entirely of a recursive function, these frames have the same name and you must distinguish them by the value of oneFewer that gets passed to them. At the bottom of the stack, you will, of course, find the frame for main().

You can select a frame to see the variables in that frame and the source code for the line of code that is currently being executed. Select the frame for the first time singTheSong is called.

Figure 5.7 Frames on the stack for a recursive function

You can see this frame’s variables and their values on the bottom left of the window. To the right, you can also see the output in an area called the console. (If you don’t see the console, find the buttons at the right of the screen towards the bottom half. Click the middle button to reveal the console.) In the console, you can see the effect of your breakpoint: the program stopped before reaching the line that ends the song.

Now we need to remove the breakpoint so that the program will run normally. You can simply drag the blue indicator off the gutter. Or click the icon at the top of the navigator area to reveal the breakpoint navigator and see all the breakpoints in a project. From there, you can select your breakpoint and delete it.

To resume execution of your program, click the button on the debugger bar between the editor and the variables view.

We just took a quick look at the debugger here to demonstrate how frames work. However, using the debugger to set breakpoints and browse the frames in a program’s stack will be helpful when your program is not doing what you expect and you need to look at what is really happening.

return


Many functions return a value when they complete execution. You know what type of data a function will return by the type that precedes the function name. (If a function doesn’t return anything, its return type is void.)

Create a new C Command Line Tool named Degrees. In main.c, add a function before main() that converts a temperature from Celsius to Fahrenheit. Then update main() to call the new function.

#include

float fahrenheitFromCelsius(float cel)

{

float fahr = cel * 1.8 + 32.0;

printf("%f Celsius is %f Fahrenheit\n", cel, fahr);

return fahr;

}

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

{

float freezeInC = 0;

float freezeInF = fahrenheitFromCelsius(freezeInC);

printf("Water freezes at %f degrees Fahrenheit\n", freezeInF);

return 0;

}

See how we took the return value of fahrenheitFromCelsius() and assigned it to the freezeInF variable of type float? Pretty slick, huh?

The execution of a function stops when it returns. For example, imagine that you had this function:

float average(float a, float b)

{

return (a + b)/2.0;

printf("The mean justifies the end\n");

}

If you called this function, the printf() call would never get executed.

A natural question, then, is “Why do we always return 0 from main()?” When you return 0 to the system, you are saying “Everything went OK.” If you are terminating the program because something has gone wrong, you’ll return 1.

This may seem contradictory

Return Main Page Previous Page Next Page

®Online Book Reader