modular programming chapter 6. 2 6.1 value and reference parameters computesumave (x, y, sum, mean)...

53
Modular Programming Chapter 6

Upload: solomon-dean

Post on 12-Jan-2016

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

Modular Programming

Chapter 6

Page 2: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

2

6.1 Value and Reference Parameters

computeSumAve (x, y, sum, mean)

ACTUAL FORMAL

x num1(input)

y num2(input)

sum sum(output)

mean average(output)

Page 3: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

3

computeSumAve.cpp

// File: computeSumAve.cpp

#include <iostream>

using namespace std;

void computeSumAve (float, float, float&,

float&);

int main ()

{

float x,

y,

sum,

mean;

Page 4: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

4

computeSumAve.cpp

cout << "Enter 2 numbers: ";

cin >> x >> y;

computeSumAve (x, y, sum, mean);

cout << " Sum is " << sum << endl;

cout << " Average is " << mean << endl;

return 0;

}

Page 5: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

5

computeSumAve.cpp

// COMPUTES THE SUM AND AVERAGE OF NUM1 AND NUM2

// Pre: num1 and num2 are assigned values.

// Post: The sum and average of num1 and num2

// are computed and returned as function outputs.

void computesumave(float num1, float num2,

float& sum,

float& average)

{

sum = num1 + num2;

average = sum / 2.0;

}

Page 6: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

6

Before Execution

Page 7: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

7

After Execution

Page 8: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

8

Call-by-Value and Call-by-Reference Parameters

Call by Value– Local function copy

Call by Reference (&)– Actual memory location

Page 9: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

9

Call-by-Value and Call-by- Reference

& Call by Reference– Formal Argument in function heading– Argument list in function proto-type

Used to modify values in a function– Input– Input/Output– Output

Page 10: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

10

Call by Value

Local copy of argument made at time of the function call

Local copy used in function Modified local copy not actual value When finished local copy destroyed Actual value not changed

Page 11: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

11

Call by Reference

Memory address of the actual argument is what is passed to the function

Because it is the address in memory of the actual argument you can modify its value

Data can flow into a function and out of the function

Page 12: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

12

Protection and Usage of Value and Reference Parameters

Value arguments not changeable Reference use could create a side effect If one return value is enough use value

arguments with a return If more than one return is needed use

reference arguments for the ones needing return values

Page 13: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

13

Protection and Usage of Value and Reference Parameters

Typically use reference arguments in getData() type functions

Value arguments used with printing type functions

When a function must return more than one value a reference argument must be used

Avoid using reference arguments because of side effects (Large Projects)

Page 14: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

14

Value and Reference Parameters

Expressions can be passed to functions– Always passed by value– Only variables can be passed by reference

Page 15: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

15

6.2 Functions with Output and Input Parameters

Now examine functions that have only output or inout (input/output) parameters.

testGetFrac.cpp– Data items are entered at the keyboard

sort3Numbers.cpp– Demonstrate multiple calls to a function with

inout parameters

Page 16: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

16

testGetFrac.cpp

// File: testGetFrac.cpp

// Tests the fractions.

#include <iostream>

using namespace std;

getFrac(int&, int& );

int main()

{

Page 17: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

17

testGetFrac.cpp

int num,denom;

cout << "Enter a common fraction "

<< "as 2 integers separated by a slash: ";

getFrac(num, denom);

cout << "Fraction read is " << num << " / " << denom << endl;

return 0;

}

Page 18: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

18

testGetFrac.cpp

// Reads a fraction.

// Pre: none

// Post: numerator returns fraction numerator,

// denominator returns fraction denominator

void getFrac(int& numerator, int& denominator)

{

char slash;

// Read the fraction

cin >> numerator >> slash >> denominator;

}

Page 19: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

19

testGetFrac.cpp

Program Output

Enter a fraction as 2 integers separated by a slash :

3 / 4

The Fraction is : 3 / 4

Page 20: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

20

sort3Numbers.cpp

// FILE: sort3Numbers.cpp

// READS THREE FLOATING POINT NUMBERS AND SORTS

// THEM IN ASCENDING ORDER

#include <iostream>

using namespace std;

// SORTS A PAIR OF NUMBERS

void order(float&, float&);

int main ()

{

Page 21: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

21

sort3Numbers.cpp

// Local data ...

float num1, num2, num3;

// Read and sort numbers.

cout << "Enter 3 numbers to sort:";

cin >> num1 >> num2 >> num3;

order (num1, num2);

order (num1, num3);

order (num2, num3);

Page 22: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

22

sort3Numbers.cpp

// Display results.

cout << "The three numbers in order are:" <<

endl;

cout << num1 << " " << num2 << " " << num3 <<

endl;

return 0;

}

Page 23: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

23

sort3Numbers.cpp

// SORTS A PAIR OF NUMBERS REPRESENTED BY x AND y

void order(float& x, float& y)

// Pre: x and y are assigned values.

// Post: x is the smaller of the pair and y is

// the larger.

{

// Local data ...

float temp;

Page 24: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

24

sort3Numbers.cpp

// Compare x and y and exchange values if not

// properly ordered.

if (x > y)

{

temp = x;

x = y;

y = temp;

}

}

Page 25: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

25

sort3Numbers.cpp

Program Output

Enter 3 numbers to be sorted separated by spaces:

7.5 9.6 5.5

The three numbers in order are:

5.5 7.5 9.6

Page 26: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

26

Sort3Numbers.cpp

Page 27: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

27

6.3 Function Syntax & Arguments

Correspondence between actual and formal arguments is determined by position in their respective argument lists. These lists must be the same size. The names of corresponding actual and formal arguments may be different

Formal arguments and corresponding actual arguments should agree with respect to type

Page 28: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

28

Function Syntax & Arguments

For reference arguments, an actual argument must be a variable. For value arguments, an actual argument may be a variable, a constant or an expression

Page 29: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

29

6.3 Stepwise Design with Functions

Use functions as building blocks in design Start small and add functions compiling as

you go Case study the sum and average problem Classic Stepwise design steps

Page 30: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

30

Stepwise Design with Functions

Problem statement Problem analysis Program design Program implementation Test and verification

Page 31: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

31

Case Study Structure Chart

C a se S tu d y S truc tu re C h a rt

R e ad then u m b er o fd a ta i tem s

C o m p u teth e sum

C o m p u teth e a ve ra ge

P r in t th e suma n d thea ve rage

C o m p ute a n d p rin t th e suma n d a ve ra g e o f a co lle ctiono f f lo a tin g p o in t d a ta i te m s

ComputeSum ComputeAve PrintSumAve

Page 32: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

32

computeSumAve.cpp

// File: computeSumAve.cpp

// Computes and prints the sum and average of

// a collection of data.

// File: computeSumAveFunctions

// Computes the sum and average of a collection

// of data

#include <iostream>

using namespace std;

Page 33: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

33

computeSumAve.cpp

// Functions used ...

// Computes sum of data

float computeSum (int);

// Computes average of data

float computeAve (int, float);

// Prints number of items, sum, and average

void printSumAve (int, float, float);

Page 34: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

34

computeSumAve.cpp

int main()

{

// Local data . . .

int numItems;

float sum;

float average;

// Read the number of items to process.

cout << "Enter the number of items to

process:";

cin >> numItems;

Page 35: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

35

computeSumAve.cpp

// Compute the sum of the data.

sum = computeSum(numItems);

// Compute the average of the data.

average = computeAve(numItems, sum);

// Print the sum and the average.

printSumAve(numItems, sum, average);

return 0;

}

Page 36: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

36

computeSumAve.cpp

// Insert definitions for functions computeSum,

// computeAve, and printSumAve here.

// Computes sum of data.

// Pre: numItems is assigned a value.

// Post: numItems data items read; their sum

// is stored in sum.

// Returns: Sum of all data items read if

// numItems >= 1; otherwise, 0.

float computeSum (int numItems)

{

Page 37: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

37

computeSumAve.cpp

// Local data ...

float item;

float sum;

// Read each data item and accumulate it in

// sum.

sum = 0.0;

for (int count = 0; count < numItems; count++)

{

cout << "Enter a number to be added: ";

cin >> item;

sum += item;

} // end for

Page 38: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

38

computeSumAve.cpp

return sum;

} // end computeSum

// Computes average of data

// Pre: numItems and sum are defined; numItems

// must be greater than 0.

// Post: If numItems is positive, the average is

// computed as sum / numItems;

// Returns: The average if numItems is positive;

// otherwise, 0.

Page 39: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

39

computeSumAve.cpp

float computeAve (int numItems, float sum)

{

// Compute the average of the data.

if (numItems < 1)

{

cout << "Invalid value for numItems = " <<

numItems << endl;

cout << "Average not computed." << endl;

return 0.0;

} // end if

return sum / numItems;

} // end computeAve

Page 40: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

40

computeSumAve.cpp

// Prints number of items, sum, and average of

// data

// Pre: numItems, sum, and average are defined.

// Post: Displays numItems, sum and average if

// numItems > 0.

void printSumAve (int numItems, float sum,

float average)

{

// Display results if numItems is valid.

if (numItems > 0)

{

Page 41: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

41

computeSumAve.cpp

cout << "The number of items is " <<

numItems << endl;

cout << "The sum of the data is " <<

sum << endl;

cout << "The average of the data is " <<

average << endl;

}

else

{

cout << "Invalid number of items = " <<

numItems << endl;

Page 42: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

42

computeSumAve.cpp

cout <<

"Sum and average are not defined." << endl;

cout <<

"No printing done. Execution terminated." <<

endl;

} // end if

} // end printSumAve

Page 43: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

43

computeSumAve.cpp

Program OutputEnter the number of items to be processed: 3

Enter a number to be added: 5

Enter a number to be added: 6

Enter a number to be added: 17

The number of items is 3

The sum of the data is 28.00

The average of the data is 9.3333

Page 44: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

44

6.4 Using Objects with Functions

Two ways to use functions to process objects Member function modifies the objects

attributes– testString.remove (0, 5);

Pass object as a function argument– Passing string object in function doReplace.cpp

Page 45: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

45

moneyToNumberTest.cpp

// File: MoneyToNumberTest.cpp

// Tests function moneyToNumberString

#include <string>

#include <iostream>

using namespace std;

// Function prototype

void moneyToNumberString(string&);

Page 46: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

46

moneyToNumberTest.cpp

int main()

{

string mString;

cout << "Enter a dollar amount with $

and commas: ";

cin >> mString;

moneyToNumberString(mString);

cout << "The dollar amount as a number is " <<

mString << endl;

Page 47: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

47

moneyToNumberTest.cpp

return 0;

}

// Removes the $ and commas from a money string.

// Pre: moneyString is defined and may contain

// commas and begin with $ or -$.

// Post: $ and all commas are removed from

// moneyString.

void moneyToNumberString (string& moneyString)

{

Page 48: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

48

moneyToNumberTest.cpp

{

// Local data . . .

int posComma; // position of next comma

// Remove $ from moneyString

if (moneyString.at(0) == '$')

moneyString.erase(0, 1);

else if (moneyString.find("-$") == 0)

moneyString.erase(1, 1);

// Remove all commas

posComma = moneyString.find(",");

Page 49: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

49

moneyToNumberTest.cpp

while (posComma >= 0 && posComma <

moneyString.length())

{

moneyString.erase(posComma, 1);

posComma = moneyString.find(",");

}

} // end moneyToNumberString

Page 50: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

50

6.5 Debugging and Testing a Program System

Top-Down testing and use of Stubs– Large projects– Stubs for all functions not finished (substitute

for a specific function) just a heading without any details other than some type of message

Bottom-Up testing and use of Drivers– Driver used by developer to test full

functionality of their function

Page 51: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

51

Debugging and Testing a Program System

Debugging Tips for Program Systems– Carefully document each function parameter and

local variable using comments as you write the code. Also describe the function’s purpose using comments.

– Create a trace of execution by displaying the function name as you enter it.

– Trace or display the values of all input and input/output parameters upon entry to a function. Check that these values make sense.

Page 52: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

52

Debugging and Testing a Program System

Debugging Tips for Program Systems– Make sure that the function stub assigns a value to

each output parameter.

Identifier Scope and Watch Window Variables

Black-Box Versus White-Box Testing

Page 53: Modular Programming Chapter 6. 2 6.1 Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)

53

6.7 Common Programming Errors

Argument inconsistencies with Call by Reference Arguments– Side effects

Forgetting & Argument type mismatch Argument positional errors or missing

arguments