Chapter 6 Modular Programming 20061122 chap 6 Functions

  • Slides: 23
Download presentation
Chapter 6 Modular Programming 20061122 chap 6

Chapter 6 Modular Programming 20061122 chap 6

Functions with Simple Output Parameters n In Chapter 3: functions. n n n How

Functions with Simple Output Parameters n In Chapter 3: functions. n n n 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 chap 6 2

Example: Function separate n Separate a number into three parts. 20061122 chap 6 3

Example: Function separate n Separate a number into three parts. 20061122 chap 6 3

20061122 chap 6 4

20061122 chap 6 4

Pointer n n n A declaration of a simple output parameter such as char

Pointer n n n 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 20061122 chap 6 5

Main function (Fig. 6. 3) 20061122 chap 6 6

Main function (Fig. 6. 3) 20061122 chap 6 6

separate(value, &sn, &whl, &fr) The use of the address-of operator & on the actual

separate(value, &sn, &whl, &fr) The use of the address-of operator & on the actual arguments sn, whl and fr is essential. n If the operator & were omitted, we would be passing to separate the values of sn, whl and fr. n The only way separate can store value is sn, whl and fr is if it knows where to find them in memory. n e. g. scanf(“%d%lf”, &code, &amount); 7 20061122 chap 6 n

Parameter Correspondence for separate 20061122 chap 6 8

Parameter Correspondence for separate 20061122 chap 6 8

Multiple Calls to a Function with Input/Output Parameters n n n The use of

Multiple Calls to a Function with Input/Output Parameters n n n 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 n 20061122 A rearrangement of data in a particular sequence (increasing or decreasing) chap 6 9

Ex: Sort Three Numbers (Fig. 6. 6) 20061122 chap 6 10

Ex: Sort Three Numbers (Fig. 6. 6) 20061122 chap 6 10

20061122 chap 6 11

20061122 chap 6 11

Scope of Names n n n The region in a program where a particular

Scope of Names n n n 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 chap 6 12

Fig. 6. 8 20061122 chap 6 13

Fig. 6. 8 20061122 chap 6 13

Formal Output Parameters as Actual Arguments n Sometimes a function needs to pass its

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

20061122 chap 6 15

20061122 chap 6 15

Data Areas for scan_fraction and Its Caller 20061122 chap 6 16

Data Areas for scan_fraction and Its Caller 20061122 chap 6 16

A Program with Multiple Functions n Case Study: Arithmetic with Common Factions. 20061122 chap

A Program with Multiple Functions n Case Study: Arithmetic with Common Factions. 20061122 chap 6 17

Show the program n Fig. 6. 12 20061122 chap 6 18

Show the program n Fig. 6. 12 20061122 chap 6 18

Sample Run of a Partially Complete Program Containing Stubs 20061122 chap 6 19

Sample Run of a Partially Complete Program Containing Stubs 20061122 chap 6 19

Debugging and Testing n n n Top-Down Testing vs. Bottom-Up Testing Stubs: we can

Debugging and Testing n n n 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 chap 6 20

Stub for multiply_fractions 20061122 chap 6 21

Stub for multiply_fractions 20061122 chap 6 21

Bottom-up Testing n n When a function is completed, we often perform a preliminary

Bottom-up Testing n n 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 chap 6 22

Summary n n How programmers use output parameters to return multiple results from a

Summary n n How programmers use output parameters to return multiple results from a function Pointer!!!!!! n 20061122 chap 6 23