Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.

Similar presentations


Presentation on theme: "Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University."— Presentation transcript:

1 Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology

2 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-2 Functions with Simple Output Parameters When a function call executes, the computer allocates memory space in the function data area for each formal parameter. The value of each actual parameter is stored in the memory cell allocated to its corresponding formal parameter.

3 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-3 Figure 6.1 Function separate()

4 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-4 Figure 6.2 Diagram of Function separate() with Multiple Results

5 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-5 Functions with Simple Output Parameters (Cont’d) Asterisks –before the parameter names –in front of the parameter names in the assignment statements char *signp tells the compiler that signp will contain the address of a type char variable. pointer –a memory cell whose content is the address of another memory cell

6 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-6 Figure 6.3 Program That Calls a Function with Output Arguments (cont’d)

7 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-7 Figure 6.3 Program That Calls a Function with Output Arguments (cont’d)

8 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-8 Functions with Simple Output Parameters (Cont’d) The calling function must declare variables in which function separate can store the multiple results it computes. Changing of the values of memory cells in the data area of the calling function is considered a side effect of the call to function separate. The addresses of the arguments sn, whl, and fr are stored in the corresponding output parameters signp, wholep, and fracp.

9 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-9 Figure 6.4 Parameter Correspondence for separate(value, &sn, &whl, &fr);

10 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-10 Functions with Simple Output Parameters (Cont’d)

11 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-11 Figure 6.5 Comparison of Direct and Indirect Reference

12 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-12 Functions with Simple Output Parameters (Cont’d) The formal parameter is preceded by the indirection operator, unary *. –*signp = '+'; –*wholep = floor(magnitude); –*fracp = magnitude - *wholep;

13 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-13 Meanings of * Symbol Three distinct meanings of the symbol * –the binary operator meaning multiplication. –in the declarations, read as “pointer to” –the unary indirection operator, “follow the pointer”

14 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-14 Figure 6.6 Program to Sort Three Numbers

15 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-15 Figure 6.6 Program to Sort Three Numbers (cont’d)

16 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-16 Multiple Calls to a Function with Input/Output Parameters sort –a rearrangement of data in a particular sequence (increasing or decreasing) input/output parameters –the function uses the current actual argument values as inputs and may return new values

17 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-17 Figure 6.7 Data Areas After temp = *smp; During Call order(&num1, &num3);

18 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-18

19 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-19 Scope of Names scope of a name –the region in a program where a particular meaning of a name is visible All of the formal parameters and local variables are visible only from their declaration to the closing brace in which they are declared.

20 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-20 Figure 6.8 Outline of Program for Studying Scope of Names

21 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-21

22 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-22 Figure 6.9 Function scan_fraction (incomplete)

23 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-23 Figure 6.9 Function scan_fraction (incomplete) (cont’d)

24 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-24 Figure 6.10 Data Areas for scan_fraction and Its Caller

25 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-25 CASE STUDY Arithmetic with Common Fractions To perform computations with common fractions and get results that are common fractions in reduced form Add, subtract, multiply, and divide several pairs of common fractions.

26 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-26 CASE STUDY Arithmetic with Common Fractions (Cont’d) DATA REQUIREMENTS –Problem Inputs int n1, d1 /* numerator, denominator of first fraction */ int n2, d2 /* numerator, denominator of second fraction */ char op /* arithmetic operator + - * or / */ char again /* y or n depending on user's desire to continue */ –Problem Outputs int n_ans /* numerator of answer */ int d_ans /* denominator of answer */

27 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-27 CASE STUDY Arithmetic with Common Fractions (Cont’d) INITIAL ALGORITHM –1. Repeat as long as user wants to continue. –2. Get a fraction problem. –3. Compute the result. –4. Display problem and result. –5. Check if user wants to continue. Step 2 Refinement –2.1 Get first fraction. –2.2 Get operator. –2.3 Get second fraction.

28 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-28 CASE STUDY Arithmetic with Common Fractions (Cont’d) Step 3 Refinement –3.1 Select a task based on operator: –'+': Add the fractions. –‘-': Add the first fraction and the negation of the second. –'*': Multiply the fractions. –'/': Multiply the first fraction and the reciprocal of the second. –3.2 Put the result fraction in reduced form. Step 3.2 Refinement –3.2.1 Find the greatest common divisor (GCD) of the numerator and denominator. –3.2.2 Divide the numerator and denominator by the GCD.

29 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-29 Figure 6.11 Structure Chart for Common Fraction Problem

30 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-30 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions

31 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-31 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

32 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-32 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

33 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-33 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

34 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-34 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

35 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-35 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

36 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-36 Figure 6.13 Sample Run of a Partially Complete Program Containing Stubs

37 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-37 Debugging and Testing a Program System Not all functions will be ready at the same time use of stubs enables us to test and debug the main program flow and those functions that are available top-down testing –the process of testing flow of control between a main function and its subordinate functions

38 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-38 Debugging and Testing a Program System (Cont’d) stub –a skeleton function that consists of a header and statements that display trace messages and assign values to output parameters; enables testing of the flow of control among functions before this function is completed

39 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-39 Figure 6.14 Stub for Function multiply_fractions

40 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-40 Debugging and Testing a Program System (Cont’d) unit test –a test of an individual function –Perform a preliminary test of a new function –It is easier to locate and correct errors when dealing with a single function rather than with a complete program system. Perform a unit test by writing a short driver function to call it. Once a function works properly, substitute it for its stub

41 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-41 Figure 6.15 Driver for Function scan_fraction

42 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-42 Debugging and Testing a Program System (Cont’d) bottom-up testing –the process of separately testing individual functions of a program system system integration tests –testing a system after replacing all its stubs with functions that have been pretested

43 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-43 Debugging Tips for Program Systems A list of suggestions for debugging a program system follows. –Carefully document each function parameter and local variable using comments –Create a trace of execution by displaying the function name as you enter it –Trace or display the values of parameters upon entry to a function –Trace or display the values of all function outputs

44 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-44 Common Programming Errors An actual output argument must be of the same pointer data type as the corresponding formal parameter. The C scope rules


Download ppt "Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University."

Similar presentations


Ads by Google