Chapter 7
Functions
As we mentioned earlier, C programs are made up entirely of functions.
We have already discussed the main function. Now we will discuss other functions.
As in most other programming languages, each function must have a heading.
The format for a function heading is:
<return type> function name( arguments ){body}
All functions (except the main function) must also have a FUNCTION PROTOTYPE.
A function prototype is simply a copy of the function's heading followed
by a semicolon. It tells the compiler what each function will look like
so that the compiler can look for it. See the following example.
#include <stdio.h>
/* function prototypes */
int test_function( int x, int y);
int test_function( int x, int y) {
int z; /* define a local variable */
z = x * y;
return z; /* return a value */
}
void main(void) {
int i;
i = test_function(2, 3);
printf(" %d", i);
}
The "test_function" in this program included a few elements
that require explanation. This was the first function in this tutorial to
return a value. Notice that to the keyword "return" was used to
return a value. The "return" keyword will terminate the current
function and return its argument to the calling program. If "return"
is used without an argument it will return a zero. |