1 / 35

Unit 5

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:

idania
Télécharger la présentation

Unit 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit 5 Modular Programming

  2. Key Concepts • Input parameters • Output parameters • Pointers • Multiple functions • Identifier scope • Types of testing

  3. 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); }

  4. 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

  5. Calling a Function with Output Parameters • Use the address of operator (&) before the name of the variable. • Example: separate(value, &sn, &whl, &fr);

  6. Figure 6.2Diagram of Function separate with Multiple Results

  7. Figure 6.1Function separate

  8. Figure 6.3Program That Calls a Function with Output Arguments

  9. Figure 6.3Program That Calls a Function with Output Arguments (cont’d)

  10. Figure 6.3Program That Calls a Function with Output Arguments (cont’d)

  11. Figure 6.4Parameter Correspondence for separate(value, &sn, &whl, &fr);

  12. Effects of & Operator on the Data Type of a Reference Table 6.1

  13. Figure 6.5Comparison of Direct and Indirect Reference

  14. Figure 6.6Program to Sort Three Numbers

  15. Figure 6.6Program to Sort Three Numbers (cont’d)

  16. Tracing the Sorting Program Table 6.2

  17. Figure 6.7Data Areas After temp = *smp; During Call order(&num1, &num3);

  18. 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

  19. Figure 6.8Outline of Program for Studying Scope of Names

  20. Scope of Names in Figure 6.8 Table 6.4

  21. 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); }

  22. Figure 6.9Function scan_fraction (incomplete)

  23. Figure 6.9Function scan_fraction (incomplete) (cont’d)

  24. Figure 6.10Data Areas for scan_fraction and Its Caller

  25. Figure 6.11Structure Chart for Common Fraction Problem

  26. Figure 6.12Program to Perform Arithmetic Operations on Common Fractions

  27. Figure 6.12Program to Perform Arithmetic Operations on Common Fractions (cont’d)

  28. Figure 6.12Program to Perform Arithmetic Operations on Common Fractions (cont’d)

  29. Figure 6.12Program to Perform Arithmetic Operations on Common Fractions (cont’d)

  30. Figure 6.12Program to Perform Arithmetic Operations on Common Fractions (cont’d)

  31. Figure 6.12Program to Perform Arithmetic Operations on Common Fractions (cont’d)

  32. Figure 6.13Sample Run of a Partially Complete Program Containing Stubs

  33. 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.

  34. Figure 6.14Stub for Function multiply_fractions

  35. Figure 6.15Driver for Function scan_fraction

More Related