Load and examine the file sumsqres.c for an example of a C program with functions.
int sum; /* This is a global variable */
main( )
{
int index;
header(); /* This calls the function named header */
for (index = 1;index <= 7;index++)
square(index); /* This calls the square function */
ending(); /* This calls the ending function */
}
header() /* This is the function named header */
{
sum = 0; /* initialize the variable "sum" */
printf("This is the header for the square program\n\n");
}
square(number) /* This is the square function */
int number;
{
int numsq;
numsq = number * number; /* This produces the square */
sum += numsq;
printf("The square of %d is %d\n",number,numsq);
}
ending() /* This is the ending function */
{
printf("\nThe sum of the squares is %d\n",sum);
}
Actually this is not the first function we have encountered, because the "main" program we have been using all along is technically a function, as is the "printf" function. The "printf" function is a library function that was supplied with your compiler.
Notice the executable part of this program. It begins with a line that simply says "header()’, which is the way to call any function. The parentheses are required because the C compiler uses them to determine that it is a function call and not simply a misplaced variable. When the program comes to this line of code, the function named "header" is called, its statements are executed, and control returns to the statement following this call. Continuing on we come to a "for" loop which will be executed 7 times and which calls another function named "square" each time through the loop, and finally a function named "ending "will be called and executed. For
the moment ignore the "index" in the parentheses of the call to "square". We have seen that this program therefore calls a header, 7 square calls, and an ending. Now we need to define the functions
int sum; /* This is a global variable */
main( )
{
int index;
header(); /* This calls the function named header */
for (index = 1;index <= 7;index++)
square(index); /* This calls the square function */
ending(); /* This calls the ending function */
}
header() /* This is the function named header */
{
sum = 0; /* initialize the variable "sum" */
printf("This is the header for the square program\n\n");
}
square(number) /* This is the square function */
int number;
{
int numsq;
numsq = number * number; /* This produces the square */
sum += numsq;
printf("The square of %d is %d\n",number,numsq);
}
ending() /* This is the ending function */
{
printf("\nThe sum of the squares is %d\n",sum);
}
Actually this is not the first function we have encountered, because the "main" program we have been using all along is technically a function, as is the "printf" function. The "printf" function is a library function that was supplied with your compiler.
Notice the executable part of this program. It begins with a line that simply says "header()’, which is the way to call any function. The parentheses are required because the C compiler uses them to determine that it is a function call and not simply a misplaced variable. When the program comes to this line of code, the function named "header" is called, its statements are executed, and control returns to the statement following this call. Continuing on we come to a "for" loop which will be executed 7 times and which calls another function named "square" each time through the loop, and finally a function named "ending "will be called and executed. For
the moment ignore the "index" in the parentheses of the call to "square". We have seen that this program therefore calls a header, 7 square calls, and an ending. Now we need to define the functions

