Previous: 3.2 Defining Functions
Up: 3.2 Defining Functions
Next: 3.2.2 Call by Value and Local Variables
Previous Page: 3.2 Defining Functions
Next Page: 3.2.2 Call by Value and Local Variables

3.2.1 Passing Data to and from Functions

As we can see from the above description, and also in Figure 3.5, information is passed to a function as arguments specified in the calling expression. This information is received by the function in the cells reserved for the formal parameters. In our case, the values of hours_worked and rate_of_pay (the arguments of the call) are copied to the cells called hours and rate within the function calc_pay(). Remember, these names are only known internally to the function. All that main() sees of the function is a black box as was shown in Figure 3.3.

The names of the formal parameters are arbitrary. For example, calc_pay() may be defined with any names for formal arguments:

float calc_pay(float x, float y)
     {
          if (x > REG_LIMIT) ...
     }
or,
float calc_pay(float hours_worked, float rate_of_pay)
     {
          if (hours_worked > REG_LIMIT) ...
     }
As long as the function uses the formal parameters names internally for computations, the function definitions behave the same. In the last case, even though the formal parameters have the same names as variables defined in main(), they represent distinctly different variables, as shown in Figure 3.5. In summary, the scope of automatic variables defined in a block is local to that block, i.e. the objects can be directly accessed by name only within that block and in blocks nested within it.

As we stated earlier, the arguments in a function call can be any valid expressions. Only the values of the argument expressions are passed to the called function. For example, these are valid function calls:

printf("Pay = %f\n", hours_worked * rate_of_pay);
     printf("Pay = %f\n", calc_pay(hours_worked, rate_of_pay));
     calc_pay(hours_worked, rate_of_pay * 1.10);
The argument in the first printf() call is a product expression. The result of evaluating that expression is passed to printf(). The second statement uses an argument that is itself a function call. The function call evaluates to a value which is then passed to printf(). The second argument in the last statement is an expression whose value is passed to calc_pay().

Information is returned from a function using the return statement which can also return the value of any valid expression. The syntax of the return statement is:

For example, we could have combined the last two statements in the function definition of calc_pay():
return regular + overtime;
where calc_pay() would then return the value of the expression .

When writing functions, tools such as shown in Figure 3.5 can be very useful in tracing the behavior of the function. Another way to check a function for bugs is to manually trace its execution with representative values for the formal parameters. Figure 3.6 shows such a trace for calc_pay(). Note: the variables hours and rate (the formal parameters) receive values during the function calls. Other local variables get values as the function is executed.

In our payroll program, the overall logic can be made even more apparent if functions are used to get the input data and to print the results. The driver, i.e. main(), can then follow the overall logic and use function call statements to get the data, calculate the pay, and print the results. A function that prints data is simple to write. Writing a function that reads data is somewhat more involved. We will delay writing such functions until Chapter .



Previous: 3.2 Defining Functions
Up: 3.2 Defining Functions
Next: 3.2.2 Call by Value and Local Variables
Previous Page: 3.2 Defining Functions
Next Page: 3.2.2 Call by Value and Local Variables

tep@wiliki.eng.hawaii.edu
Wed Aug 17 08:21:42 HST 1994