sum

Value and Reference Parameters
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
CSCE 106
2
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
CSCE 106
3
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
CSCE 106
4
Listing 6.2
CSCE 106
Functions main and test
5
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
CSCE 106
6
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 functions
exchange (s[3], s[5]);
CSCE 106
7
Flowchart Function Notation
A program module/function is represented in
flowcharts by the following special symbol.
CSCE 106
8
Flowchart Function Notation
(cont’d)
START
•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.
Read Input
Call calc_pay
function
Display
results
END
CSCE 106
9
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.
CSCE 106
10
main
Solution
START
computeAverage
INPUT
n1, n2
average =
computeAverage(n1, n2)
OUTPUT
average
START
return (n1 +n2)/2
STOP
STOP
CSCE 106
11
Figure 6.4
CSCE 106
Structure chart for general sum and
average problem
12
Listing 6.6
main function
// 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 data
float computeSum
(int);
// IN - number of data items
// Computes average of data
float computeAve
(int,
// IN - number of data items
float);
// IN - sum of data items
// Prints number of items, sum, and average
void printSumAve
(int,
// IN - number of data items
float,
// IN - sum of the data
float);
// IN - average of the data
CSCE 106
13
Listing 6.6
int main( )
{
int numItems;
float sum;
float average;
main function (continued)
// input - number of items to be added
// output - accumulated sum of the data
// 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;
}
CSCE 106
14
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
CSCE 106
15
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
CSCE 106
16
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,
float sum;
CSCE 106
// IN: number of data items
// IN: sum of data
17
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
CSCE 106
18
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,
float sum,
float average)
CSCE 106
// IN: number of data items
// IN: sum of the data
// IN: average of the data
19
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
CSCE 106
20
Exercise 2
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 2pr)
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 pr2)
Assume p=3.1416.
Please use a structure chart to help you analyse the given exercise.
CSCE 106
21
Next lecture will be about
Arrays as Function Parameters
CSCE 106
22