39
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ. http:// ant.comm.ccu.edu.tw [email protected]

Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ. [email protected]

  • View
    221

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

Chapter 6Modular Programming

Dr. J.-Y. Pan Dept. Comm. Eng.Nat. Chung Cheng Univ.http://[email protected]

Page 2: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-2中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

本章重點

PointerName scopeTesting skill

Page 3: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-3中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

6.1 Function With Output Parameter

Argument lists provide the communication links between the main function and its function subprograms.

Arguments enable a function to manipulate different data each time it is called.

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.

Page 4: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-4中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 6.1 Function separate

Page 5: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-5中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Pointer A memory cell whose content is the address of another memory cell

Fig 1.4Fig 3.15

Actual & Formal

Page 6: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-6中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

If a reference x is of type “whatever-type”, the reference &x is of type “pointer to whatever-type,” that is, “whatever-type *.”

Page 7: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-7中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 6.5 Comparison of Direct and Indirect Reference

Page 8: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-8中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

6.1 (cont) Meaning of * Symbol

Three distinct meanings of the symbol * Multiplication

3 * x + 4 (expression)

Part of the names of the parameters’ data typespointer tochar *signp; (declaration)

Unary indirection operatorfollow the pointer*signp = ‘+’; (execution statement)

Page 9: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-9中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

6.2 Multiple Calls to a Function with Input/Output Parameters

sort A rearrangement of data in a particular

sequence (increasing or decreasing)

Example 6.2 The main function in Fig. 6.6 gets three data

values and rearranges the data in increasing sequence.

Page 10: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-10中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 6.6 Program to Sort Three Numbers

Page 11: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-11中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Comparing to p.164, Fig 4.5

Page 12: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-12中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

6.2 (cont) Trace of Program to Sort Three Numbers

Statement num1 num2 num3 Effect

scanf(“...”, &num1, &num2, &num3);

7.5 9.6 5.5 Enter data

Order(&num1, &num2); No change

Order(&num1, &num3); 5.5 9.6 7.5 Switch

Order(&num2, &num3); 5.5 7.5 9.6 Switch

printf(“...”, num1, num2, num3)

Display

Page 13: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-13中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 14: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-14中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Program Style

Functions that return a single value are the easiest functions for a program reader to deal with.

If a function subprogram has a meaningful name, the reader can often get a good idea of what is happening in the calling function.

Page 15: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-15中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

6.3 Scope of Names

The scope of a name refers to the region of a program where a particular meaning of a name is visible or can be referenced.

Name is defined as constant macros and their scope begins at their definition and continues to the end of the source file.

Page 16: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-16中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 6.8 Outline of Program for Studying Scope of Names

Page 17: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-17中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Scope of Names Use in Fig. 6.8

Page 18: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-18中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

6.4 Formal Output Parameters as Actual Arguments

Fig. 6.9 shows a function that scans a data line representing a common fraction of the form numerator / denominator Numerator is an integer Denominator is a positive integer

Figure 6.10 shows the data areas for scan_fraction and the function calling it.

Page 19: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-19中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 6.9 Function scan_fraction

nump &slash denomp

Driver: Fig 6.15

Page 20: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-20中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 6.10 Data Areas for scan_fraction and Its Caller

scanf()

Page 21: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-21中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

6.5 A Program with Multiple Functions CASE STUDY Arithmetic with Common Fractions

Page 22: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-22中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Page 23: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-23中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

6.5 (cont) Case Study: Arithmetic with Common Fractions

Design Initial Algorithm

1.Repeat as log as user wants to continue2. Get a fraction problem3. Compute the result4. Display problem and result5. Check if user wants to continue

Refinement2.1 Get first fraction2.2 Get operator2.3 Get second fraction

Page 24: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-24中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

6.5 (cont) Case Study: Arithmetic with Common Fractions

Design Refinement

3.1 Select a task based on operator3.1.2 Add the fractions3.1.3 Add the first fraction and the negation of the second3.1.4 Multiply the fractions3.1.5 Multiply the first fraction and the reciprocal of the

second

3.2 Put the result fraction in reduced form

Page 25: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-25中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

6.5 (cont) Case Study: Arithmetic with Common Fractions

Design Refinement of 3.2

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

Fig. 6.11 shows the data flow among the steps

Page 26: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-26中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 6.11 Structure Chart for Common Fraction Problem

Fig 6.9

HW5

Page 27: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-27中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

6.5 (cont) Case Study: Arithmetic with Common Fractions

Implementation (Figure 6.12) 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

multiply_fractions and find_gcd use stubs

Page 28: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-28中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions

直接看程式…

Page 29: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-29中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 30: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-30中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 31: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-31中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 32: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-32中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 33: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-33中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

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

Page 34: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-34中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 6.13 Sample Run of a Partially Complete Program Containing Stubs

Page 35: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-35中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Testing (Figure 6.13) Insert a stub for each function not yet completed Each stub prints an identification message and

assigns values to its output parameters

Page 36: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-36中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

6.6 Debugging and Testing a Program SystemThe number of statements in a program system

grows, the possibility of error also increases. keep each function to a manageable size

Top-down testing the process of testing flow of control between a main

function and its subordinate functionsBottom-up testing

The process of separately testing individual functions of a program system

Unit test A test of an individual function

System integration tests Testing a system after replacing all its stubs with

functions that have been pretested

Page 37: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-37中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

Figure 6.15 Driver for Function scan_fractionDriver

A short function written to test another function by defining its arguments, calling it, and displaying its result.

Page 38: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-38中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

6.6 (cont) Debugging Tips for Program Systems

Carefully document each function parameter and local variable using comments.

Create a trace of execution by displaying namesTrace or display the values of all input and

input/output parametersTrace or display the values of all function outputs

after returning from a functionMake sure that a function stub assigns a value to

the variable pointed to by each output parameter Use “step into” and “step over” in debugger

Page 39: Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.  jypan@comm.ccu.edu.tw

3-39中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab

6.7 Common Programming Errors

N O TBeware of name scope and life time

Is there any identifier referenced outside its scope?

Does the identifier refer to what you want to? …