1 / 23

Chapter 6

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.

hansel
Download Presentation

Chapter 6

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. Chapter 6 Modular Programming

  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.

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

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

  5. Main function (Fig. 6.3)

  6. 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);

  7. Parameter Correspondence for separate

  8. 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)

  9. Ex: Sort Three Numbers (Fig. 6.6)

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

  11. Fig. 6.8

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

  13. Data Areas for scan_fraction and Its Caller

  14. A Program with Multiple Functions • Case Study: Arithmetic with Common Factions.

  15. Show the program • Fig. 6.12

  16. Sample Run of a Partially Complete Program Containing Stubs

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

  18. Stub for multiply_fractions

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

  20. Summary • How programmers use output parameters to return multiple results from a function • Pointer!!!!!!

More Related