unit 5

35
Unit 5 Modular Programming

Upload: idania

Post on 14-Jan-2016

45 views

Category:

Documents


0 download

DESCRIPTION

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: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Unit 5

Unit 5

Modular Programming

Page 2: Unit 5

Key Concepts

• Input parameters• Output parameters• Pointers• Multiple functions• Identifier scope• Types of testing

Page 3: Unit 5

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

Page 4: Unit 5

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

Page 5: Unit 5

Calling a Function with Output Parameters

• Use the address of operator (&) before the name of the variable.

• Example:separate(value, &sn, &whl, &fr);

Page 6: Unit 5

Figure 6.2

Diagram of Function separate with Multiple Results

Page 7: Unit 5

Figure 6.1

Function separate

Page 8: Unit 5

Figure 6.3

Program That Calls a Function with Output Arguments

Page 9: Unit 5

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

Page 10: Unit 5

Figure 6.3

Program That Calls a Function with Output Arguments (cont’d)

Page 11: Unit 5

Figure 6.4

Parameter Correspondence for separate(value, &sn, &whl, &fr);

Page 12: Unit 5

Effects of & Operator on the Data Type of a Reference

Table 6.1

Page 13: Unit 5

Figure 6.5

Comparison of Direct and Indirect Reference

Page 14: Unit 5

Figure 6.6 Program to Sort Three Numbers

Page 15: Unit 5

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

Page 16: Unit 5

Tracing the Sorting Program

Table 6.2

Page 17: Unit 5

Figure 6.7 Data Areas After temp = *smp;

During Call order(&num1, &num3);

Page 18: Unit 5

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

Page 19: Unit 5

Figure 6.8 Outline of Program for Studying Scope of Names

Page 20: Unit 5

Scope of Names in Figure 6.8Table 6.4

Page 21: Unit 5

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

}

Page 22: Unit 5

Figure 6.9

Function scan_fraction (incomplete)

Page 23: Unit 5

Figure 6.9

Function scan_fraction (incomplete) (cont’d)

Page 24: Unit 5

Figure 6.10

Data Areas for scan_fraction and Its Caller

Page 25: Unit 5

Figure 6.11

Structure Chart for Common Fraction Problem

Page 26: Unit 5

Figure 6.12

Program to Perform Arithmetic Operations on Common Fractions

Page 27: Unit 5

Figure 6.12

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

Page 28: Unit 5

Figure 6.12

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

Page 29: Unit 5

Figure 6.12

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

Page 30: Unit 5

Figure 6.12

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

Page 31: Unit 5

Figure 6.12

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

Page 32: Unit 5

Figure 6.13

Sample Run of a Partially Complete Program Containing Stubs

Page 33: Unit 5

Top-Down vs. Bottom-Up TestingTop-down testing• Create function stubs to

test the main program.• Add each function as it is

complete and retest.

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

Page 34: Unit 5

Figure 6.14

Stub for Function multiply_fractions

Page 35: Unit 5

Figure 6.15

Driver for Function scan_fraction