Programming in C

(Barry) #1

122 Chapter 8 Working with Functions


Program 8.2 Output
Programming is fun.
Programming is fun.

Execution of the preceding program starts at main, which contains two calls to the
printMessagefunction.When the first call to the function is executed, control is sent
directly to the printMessagefunction, which displays the message “Programming is
fun.” at the terminal and then returns to the mainroutine. Upon return, another call to
the printMessageroutine is encountered, which results in the execution of the same
function a second time. After the return is made from the printMessagefunction, exe-
cution is terminated.
As a final example of the printMessagefunction, try to predict the output from
Program 8.3

Program 8.3 More on Calling Functions
#include <stdio.h>

void printMessage (void)
{
printf ("Programming is fun.\n");
}

int main (void)
{
int i;

for ( i = 1; i <= 5; ++i )
printMessage ();

return 0;
}

Program 8.3 Output
Programming is fun.
Programming is fun.
Programming is fun.
Programming is fun.
Programming is fun.

Arguments and Local Variables


When the printffunction is called, you always supply one or more values to the func-
tion, the first value being the format string and the remaining values being the specific
Free download pdf