350 likes | 513 Views
Unit 5. Modular Programming. Key Concepts. Input parameters Output parameters Pointers Multiple functions Identifier scope Types of testing. Functions with Input Parameters. Data is passed into the function, but cannot be modified by the function. Example:
E N D
Unit 5 Modular Programming
Key Concepts • Input parameters • Output parameters • Pointers • Multiple functions • Identifier scope • Types of testing
Functions with Input Parameters • Data is passed into the function, but cannot be modified by the function. • Example: double getCircum(double radius) { return (2 * PI * radius); } int main(void) { double circum, r; r = 20; circum = getCircum(r); }
Output Parameters • Allow a function to modify the value of one or more address locations • Are parameters defined as pointers to the address location • Example: int *wholep; • Points to the location in memory where the function will store a value
Calling a Function with Output Parameters • Use the address of operator (&) before the name of the variable. • Example: separate(value, &sn, &whl, &fr);
Figure 6.2Diagram of Function separate with Multiple Results
Figure 6.3Program That Calls a Function with Output Arguments
Figure 6.3Program That Calls a Function with Output Arguments (cont’d)
Figure 6.3Program That Calls a Function with Output Arguments (cont’d)
Figure 6.4Parameter Correspondence for separate(value, &sn, &whl, &fr);
Effects of & Operator on the Data Type of a Reference Table 6.1
Tracing the Sorting Program Table 6.2
Figure 6.7Data Areas After temp = *smp; During Call order(&num1, &num3);
Scope of Names • Constant macros can be used from any line of code after they are referenced • Functions can be accessed any time after their prototype unless their name is "shadowed" by a function parameter with the same name • Formal parameters and variables defined in a function can only be accessed within that function
Scope of Names in Figure 6.8 Table 6.4
Passing an Output Parameter as an Argument • An output parameter is a pointer to a memory location • To pass it to a function as an output parameter, pass it without an operator: void scan_fraction (int *nump, int *dnomp) { char slash; slash = '/' scanf("%d %c%d", nump, &slash, dnomp); }
Figure 6.12Program to Perform Arithmetic Operations on Common Fractions
Figure 6.12Program to Perform Arithmetic Operations on Common Fractions (cont’d)
Figure 6.12Program to Perform Arithmetic Operations on Common Fractions (cont’d)
Figure 6.12Program to Perform Arithmetic Operations on Common Fractions (cont’d)
Figure 6.12Program to Perform Arithmetic Operations on Common Fractions (cont’d)
Figure 6.12Program to Perform Arithmetic Operations on Common Fractions (cont’d)
Figure 6.13Sample Run of a Partially Complete Program Containing Stubs
Top-Down vs. Bottom-Up Testing Top-down testing Bottom-up testing Separately test each function before integration. Create a driver to test the function. Unit testing – testing a function Integration testing – testing the complete program after all functions have been added • Create function stubs to test the main program. • Add each function as it is complete and retest.