1 / 34

Chapter 6: Modular Programming By: Suraya Alias

Chapter 6: Modular Programming By: Suraya Alias. 6.1 Functions with simple output parameters. Argument list provide the communication links between the main function and its function subprograms. We can use output parameter to return multiple results from a function

yamal
Download Presentation

Chapter 6: Modular Programming By: Suraya Alias

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 By: Suraya Alias

  2. 6.1 Functions with simple output parameters • Argument list provide the communication links between the main function and its function subprograms. • We can use output parameter to return multiple results from a function • A function can send back multiple output to the function that calls it

  3. Figure 6.1 Function separate

  4. Figure 6.2 Diagram of Function separate with Multiple Results void separate(double num, char *signp, int *wholep, double *fracp) Explanation: double num /* input - value to be split */ char *signp, /* output - sign of num */ int *wholep, /* output - whole number magnitude of num */ double *fracp) /* output - fractional part of num */ • This function is void as it does not return any results, the function body also does not include return statement.

  5. Pointer • Pointer is a memory cell whose content is the address of another memory cell • A declaration to a parameter such as char *signp tells the compiler that signpcontain the address of a type char variable • So, parameter signp is a pointer to type char variable, • The name signp, comes from sign and pointer thus signp

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

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

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

  9. Figure 6.4 Parameter Correspondence for separate(value, &sn, &whl, &fr); • Address-of–operator (&) on the actual argument sn, whl and fr is compulsory

  10. Figure 6.5 Comparison of Direct and Indirect Reference • In front of each parameter is the indirect operator, unary * • When * applied it has the effect of following the pointer • referenced by its pointer

  11. 6.2 Multiple Calls to a function with input output parameter • This example demonstrates the use of single parameter to carry value and also result out of a function • Also, how a function may be called more than once • Sort – a rearrangement of data in a particular sequence (either decreasing or increasing)

  12. Figure 6.6 Program to Sort Three Numbers

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

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

  15. Trace of program sort

  16. 6.3 Scope of Names • Scope of name – the region in a program where a particular meaning of a name is visible

  17. 6.4 Formal output parameters as actual arguments • Sometimes a function needs to pass its own output parameter as an argument when it calls another function. • In the example, because nump and denomp store addresses, it can be used directly in the call to scanf: • scanf(“%d%c%d”, nump &slash,denomp); • Scanf will store the first number scanned in the variable whose address is in the nump, next the slash in local variable slash and the scanned number in the variable whose address is in denomp.

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

  19. Figure 6.9 Function scan_fraction (incomplete)

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

  21. Figure 6.10 Data Areas for scan_fraction and Its Caller

  22. 6.5 A program with multiple function Figure 6.11 Structure Chart for Common Fraction Problem

  23. Implementation • Stub • A skeleton function that consists of a header and statements that display trace messages and assign values to output parameters • It enables testing of the flow of control among functions before the function is completed.

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

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

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

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

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

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

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

  31. 6.6 Debugging and Testing a Program System • Top-down testing • The process of testing flow of control between a main function and its subordinate functions • Unit test • A test of an individual function • Bottom-up testing • The process of separately testing individual functions of a program system • System Integration testing • Testing a system after replacing all its stubs with functions that have been pretested

  32. Figure 6.14 Stub for Function multiply_fractions

  33. Figure 6.15 Driver for Function scan_fraction

More Related