Chapter 6 Modular Programming 20061122 chap6
Functions with Simple Output Parameters In Chapter 3: functions. How to pass inputs into a function, and How to use the return statement to send back one result value from a function. How programmers use output parameters to return multiple results from a function. 20061122 chap6
Example: Function separate Separate a number into three parts. 20061122 chap6
20061122 chap6
Pointer A declaration of a simple output parameter such as char *signp tells the complier that signp will contain the address of a type char variable. The parameter signp is a pointer to a type char variable. Pointer: a memory cell whose content is the address of another memory cell. 20061122 chap6
Main function (Fig. 6.3) 20061122 chap6
separate(value, &sn, &whl, &fr) The use of the address-of operator & on the actual arguments sn, whl and fr is essential. If the operator & were omitted, we would be passing to separate the values of sn, whl and fr. The only way separate can store value is sn, whl and fr is if it knows where to find them in memory. e.g. scanf(“%d%lf”, &code, &amount); 20061122 chap6
Parameter Correspondence for separate 20061122 chap6
Multiple Calls to a Function with Input/Output Parameters The use of a single parameter both to bring a data value into a function and to carry a result value out of a function. How a function may be called more than once. Example: the Sort operation A rearrangement of data in a particular sequence (increasing or decreasing) 20061122 chap6
Ex: Sort Three Numbers (Fig. 6.6) 20061122 chap6
20061122 chap6
Scope of Names The region in a program where a particular meaning of a name is visible. The scope of the function subprogram begins with its prototype and continues to the end of the source file. All of the formal parameter and local variables are visible only from their declaration to the closing brace of the function in which they are declared. 20061122 chap6
Fig. 6.8 20061122 chap6
Formal Output Parameters as Actual Arguments Sometimes a function needs to pass its own output parameter as an argument when it calls another function. 20061122 chap6
20061122 chap6
Data Areas for scan_fraction and Its Caller 20061122 chap6
A Program with Multiple Functions Case Study: Arithmetic with Common Factions. 20061122 chap6
Show the program Fig. 6.12 20061122 chap6
Sample Run of a Partially Complete Program Containing Stubs 20061122 chap6
Debugging and Testing Top-Down Testing vs. Bottom-Up Testing Stubs: we can insert stubs in the program for functions that were not yet written. It provides a trace of the call sequence and allows the programmers to determine whether the flow of control within the program is correct. 20061122 chap6
Stub for multiply_fractions 20061122 chap6
Bottom-up Testing When a function is completed, we often perform a preliminary test of a new function. We perform such a unit test by writing a short driver function to call it. 20061122 chap6
Summary How programmers use output parameters to return multiple results from a function Pointer!!!!!! 20061122 chap6