Previous: 3.1 Designing the Algorithm with Functions
Up: 3 Designing Programs Top Down
Next: 3.3 Coding Programs for Readability
Previous Page: 3.1.1 Implementing the Program with Functions
Next Page: 3.2.1 Passing Data to and from Functions
A function is defined by writing the source code for it. Just as for main(), defining the function consists of giving a function header and a function body. The code for calc_pay() is shown in Figure 3.4. (It is included in the same source file as the code in Figure 3.2). Let us look at the function header first.
float calc_pay(float hours, float rate)
The header specifies that the name of the function is calc_pay, and that the function returns a float value. It also lists the parameters and their types, in this case there are two formal parameters, hours and rate, each of type float. Notice that the function header is very similar to the prototype statement for the function, with two notable exceptions. First, there is no semi-colon at the end, indicating that this is the definition of the function, not a declaration. Second, in the function header, the variable names in the parameter list are required, and this list is sometimes called the formal parameter list. These formal parameters act as variable declarations for the function with the additional feature that they receive initial values from the arguments when the function is called; the first parameter gets the value of the first argument, the second parameter the value of second argument, and so on. The formal parameters in a function definition behave in the same manner as automatic variables, and their scope is limited to the function itself. The names in this list are the names used within the function body to access these values.
The body of the function is defined, as with main(), within brackets, { and } and consists of the variable declarations for the block followed by the executable statements to perform the subtask of the function. In our case, we declare variables regular, overtime, and total which are called local variables because their scope is local, i.e. limited to within the function. We then calculate regular pay, overtime pay and total pay as before, but we use the formal parameter names and the names of the local variables in our computations. Finally, since a function can return only one value, we return only the value of total pay:
return total;The above return statement returns the value of the variable, total, to the calling function. In general, a return statement can be used to return the value of any expression. When the return statement is executed, the program control returns immediately to the calling function where the function call evaluates to the returned value.
When a function is first written, it is a good practice to include debug statements in the function definition showing the name of the function entered, the values of the parameters received, and the value returned by the function. When the program is run, these debug statements will produce a trace of all function calls and returns and as such are invaluable for debugging, particularly when a program uses many functions. We have included printf() statements for this purpose in the code for calc_pay() shown in the figure.
The above function, together with main() in the file pay5.c, forms a complete program which may be compiled and executed. A sample session shown below is similar to the one for pay4.c. The only change is that calc_pay() calculates and returns total pay, whereas in pay4.c total pay was calculated in main().