Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence."— Presentation transcript:

1 Value and Reference Parameters

2 CSCE 1062 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

3 CSCE 1063 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

4 CSCE 1064 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

5 CSCE 1065 Listing 6.2 Functions main and test

6 CSCE 1066 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

7 CSCE 1067 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]);

8 CSCE 1068 A program module/function is represented in flowcharts by the following special symbol. Flowchart Function Notation

9 CSCE 1069 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) Display results

10 CSCE 10610 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.

11 CSCE 10611 Solution INPUT n1, n2 OUTPUT average START STOP START STOP average = computeAverage(n1, n2) return (n1 +n2)/2 computeAverage main

12 CSCE 10612 Figure 6.4 Structure chart for general sum and average problem

13 CSCE 10613 // File: computeSumAve.cpp // Computes and prints the sum and average of a collection of data #include 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 Listing 6.6 main function

14 CSCE 10614 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)

15 CSCE 10615 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

16 CSCE 10616 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

17 CSCE 10617 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

18 CSCE 10618 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

19 CSCE 10619 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

20 CSCE 10620 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

21 CSCE 10621 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 2  r)  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  r 2 )  Assume  =3.1416. Please use a structure chart to help you analyse the given exercise. Exercise 2

22 CSCE 10622 Next lecture will be about Arrays as Function Parameters


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

Similar presentations


Ads by Google