230 likes | 337 Views
Chapter 6. Modular Programming. 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.
E N D
Chapter 6 Modular Programming
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.
Example: Function separate • Separate a number into three parts.
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.
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 valuesof 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);
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)
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.
Formal Output Parameters as Actual Arguments • Sometimes a function needs to pass its own output parameter as an argument when it calls another function.
A Program with Multiple Functions • Case Study: Arithmetic with Common Factions.
Show the program • Fig. 6.12
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.
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.
Summary • How programmers use output parameters to return multiple results from a function • Pointer!!!!!!