22
Value and Reference Parameters

Value and Reference Parameters. CSCE 1062 Outline Summary of value parameters Summary of reference parameters Argument/Parameter list correspondence

Embed Size (px)

Citation preview

Page 1: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

Value and Reference Parameters

Page 2: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 2

Outline Summary of value parameters Summary of reference parameters Argument/Parameter list correspondence

(NOT) Array elements as arguments (section 9.3) Flowchart notation for a user defined

function Structure charts

Page 3: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 3

Summary of Value Parameters Value of corresponding actual argument is

stored in the called function The function execution cannot change the

actual argument value Actual argument can be an expression,

variable, or constant Formal parameter type must be specified in

the formal parameter list Parameters are used to store the data

passed to a function

Page 4: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 4

Summary of Reference Parameters

Address of corresponding actual argument is stored in the called function

The function execution can change the actual argument value

Actual argument must be a variable only Formal parameter type must be followed by

& in the formal parameter list Parameters are used to return outputs from

the function or to change the value of a function argument

Page 5: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 5

Listing 6.2 Functions main and test

Page 6: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 6

Argument/Parameter List Correspondence (NOT)

Number: number of actual arguments used in function call must be the same as the number of formal parameters listed in the function header and prototype

Order: Order of arguments in the lists determines correspondence

Type: Each actual argument must be of a data type that can be assigned to the corresponding formal parameter with no unexpected loss of information

Page 7: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 7

Array Elements as Arguments Exercise: Create a C++ function called “exchange” to exchange the contents of two floating-point memory locations.

Can pass array elements to functionsexchange (s[3], s[5]);

Page 8: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 8

A program module/function is represented in flowcharts by the following special symbol.

Flowchart Function Notation

Page 9: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 9

•The position of the function/module symbol indicates the point the function is executed/called.

•A separate flowchart should be constructed for the function/module.

START

END

Read Input

Call calc_pay function

Flowchart Function Notation (cont’d)

Displayresults

Page 10: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 10

Exercise 1

Draw a flowchart for an algorithm that calculates the average of two numbers by the use of a value returning function (computeAverage). The main function should input the two numbers, as well as outputting the average.

Page 11: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 11

Solution

INPUTn1, n2

OUTPUTaverage

START

STOP

START

STOP

average = computeAverage(n1, n2)

return (n1 +n2)/2

computeAverage

main

Page 12: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 12

Figure 6.4 Structure chart for general sum and average problem

Page 13: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 13

// File: computeSumAve.cpp// Computes and prints the sum and average of a collection of data

#include <iostream>using namespace std;

// Functions used . . .// Computes sum of datafloat computeSum

(int); // IN - number of data items

// Computes average of datafloat computeAve

(int, // IN - number of data items float); // IN - sum of data items

// Prints number of items, sum, and averagevoid printSumAve

(int, // IN - number of data items float, // IN - sum of the data float); // IN - average of the data

Listing 6.6 main function

Page 14: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 14

int main( ){ int numItems; // input - number of items to be added float sum; // output - accumulated sum of the data float average; // output - average of data being processed

// Read the number of items to process. cout << “Enter the number of itesm to process: “; cin >> numItems;

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

Listing 6.6 main function (continued)

Page 15: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 15

Listing 6.7 Function computeSum

// 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) // IN: number of data items

{

// Local data . . .

float item; // input: contains current data item

float sum; // output: used to accumulate sum of data

// read in

Page 16: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 16

Listing 6.7 Function computeSum (continued)

// 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

return sum;

} // end computeSum

Page 17: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 17

Listing 6.8 Function computeAve

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

float computeAve

(int numItems, // IN: number of data items

float sum; // IN: sum of data

Page 18: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 18

Listing 6.8 Function computeAve (continued)

{

// Compute the average of the data.

if (numItems < 1) // test for invalid input

{

cout << “Invalid value for numItems = “ << numItems

<< endl;

cout << “Average not computed.” << endl;

return 0.0; // return for invalid input

}

return sum / numItems;

} // end computeAve

Page 19: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 19

Listing 6.9 Function printSumAve

// 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, // IN: number of data items

float sum, // IN: sum of the data

float average) // IN: average of the data

Page 20: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 20

Listing 6.9 Function printSumAve (continued)

{ // Display results is numItems is valid. if (numItems > 0) { 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; cout << “Sum and average are not defined.“ << endl; cout << “No printing done. Execution terminated.” << endl; } // end if} // end printSumAve

Page 21: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 21

The following formula gives the distance between two points (x1, y1) and (x2, y2) in the Cartesian plane:

(x2 – x1)2 + (y2 – y1)2)Given the centre and a point on the circle, you can use this formula to find the radius of the circle. Draw the flow charts and write a complete C++ program that prompts the user to enter the centre and a point on the circle. The program should then compute and output the circle's radius, diameter, circumference, and area. Your program must have at least the following user-defined functions:

Distance. This function takes as its parameters four numbers that represent two points in the plane and returns the distance between them.

Radius. This function takes as its parameters four numbers that represent the center and a point on the circle, calls the function Distance to find the radius of the circle, and returns the circle's radius.

Circumference. This function takes as its parameters a number that represents the radius of the circle and returns the circle's circumference. (If r is the radius, the circumference is 2r)

Area. This function takes as its parameters a number that represents the radius of the circle and returns the circle's area. ((If r is the radius, the area is r2)

Assume =3.1416.Please use a structure chart to help you analyse the given exercise.

Exercise 2

Page 22: Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence

CSCE 106 22

Next lecture will be about

Arrays as Function Parameters