Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Modular Programming 20061122 chap6.

Similar presentations


Presentation on theme: "Chapter 6 Modular Programming 20061122 chap6."— Presentation transcript:

1 Chapter 6 Modular Programming chap6

2 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. chap6

3 Example: Function separate
Separate a number into three parts. chap6

4 chap6

5 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. chap6

6 Main function (Fig. 6.3) chap6

7 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); chap6

8 Parameter Correspondence for separate
chap6

9 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) chap6

10 Ex: Sort Three Numbers (Fig. 6.6)
chap6

11 chap6

12 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. chap6

13 Fig. 6.8 chap6

14 Formal Output Parameters as Actual Arguments
Sometimes a function needs to pass its own output parameter as an argument when it calls another function. chap6

15 chap6

16 Data Areas for scan_fraction and Its Caller
chap6

17 A Program with Multiple Functions
Case Study: Arithmetic with Common Factions. chap6

18 Show the program Fig. 6.12 chap6

19 Sample Run of a Partially Complete Program Containing Stubs
chap6

20 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. chap6

21 Stub for multiply_fractions
chap6

22 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. chap6

23 Summary How programmers use output parameters to return multiple results from a function Pointer!!!!!! chap6


Download ppt "Chapter 6 Modular Programming 20061122 chap6."

Similar presentations


Ads by Google