Value and Reference Parameters CSCE 106 Outline Summary

  • Slides: 22
Download presentation
Value and Reference Parameters CSCE 106

Value and Reference Parameters CSCE 106

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

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

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

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

Listing 6. 2 CSCE 106 Functions main and test 5

Argument/Parameter List Correspondence (NOT) § Number: number of actual arguments used in function call

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

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

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

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

Exercise 1 Draw a flowchart for an algorithm that calculates the average of two numbers by the use of a value returning function (compute. Average). The main function should input the two numbers, as well as outputting the average. CSCE 106 10

main Solution START compute. Average INPUT n 1, n 2 average = compute. Average(n

main Solution START compute. Average INPUT n 1, n 2 average = compute. Average(n 1, n 2) OUTPUT average START return (n 1 +n 2)/2 STOP CSCE 106 11

Figure 6. 4 CSCE 106 Structure chart for general sum and average problem 12

Figure 6. 4 CSCE 106 Structure chart for general sum and average problem 12

Listing 6. 6 main function // File: compute. Sum. Ave. cpp // Computes and

Listing 6. 6 main function // File: compute. Sum. Ave. 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 compute. Sum (int); // IN - number of data items // Computes average of data float compute. Ave (int, // IN - number of data items float); // IN - sum of data items // Prints number of items, sum, and average void print. Sum. Ave (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 num. Items; float sum; float average;

Listing 6. 6 int main( ) { int num. Items; 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 >> num. Items; // Compute the sum of the data. sum = compute. Sum(num. Items); // Compute the average of the data. average = compute. Ave(num. Items, sum); // Print the sum and the average. print. Sum. Ave(num. Items, sum, average); return 0; } CSCE 106 14

Listing 6. 7 // // // Function compute. Sum Computes sum of data. Pre:

Listing 6. 7 // // // Function compute. Sum Computes sum of data. Pre: num. Items is assigned a value. Post: num. Items data items read; their sum is stored in sum Returns: Sum of all data items read if num. Items >= 1; otherwise, 0; float compute. Sum (int num. Items) // 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 compute. Sum (continued) // Read each data item and accumulate

Listing 6. 7 Function compute. Sum (continued) // Read each data item and accumulate it in sum = 0. 0; for (int count = 0; count < num. Items; count++) { cout << “Enter a number to be added: “; cin >> item; sum += item; } // end for return sum; } // end compute. Sum CSCE 106 16

Listing 6. 8 // // Function compute. Ave Computes average of data Pre: num.

Listing 6. 8 // // Function compute. Ave Computes average of data Pre: num. Items and sum are defined; num. Items must be greater than 0. Post: If num. Items is positive, the average is computed as sum / num. Items. Returns: The average if num. Items is positive; otherwise, 0; float compute. Ave (int num. Items, float sum; CSCE 106 // IN: number of data items // IN: sum of data 17

Listing 6. 8 Function compute. Ave (continued) { // Compute the average of the

Listing 6. 8 Function compute. Ave (continued) { // Compute the average of the data. if (num. Items < 1) // test for invalid input { cout << “Invalid value for num. Items = “ << num. Items << endl; cout << “Average not computed. ” << endl; return 0. 0; // return for invalid input } return sum / num. Items; } // end compute. Ave CSCE 106 18

Listing 6. 9 Function print. Sum. Ave // Prints number of items, sum, and

Listing 6. 9 Function print. Sum. Ave // Prints number of items, sum, and average of data. // Pre: num. Items, sum, and average are defined. // Post: displays num. Items, sum and average if num. Items > 0 void print. Sum. Ave (int num. Items, 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 print. Sum. Ave (continued) { // Display results is num.

Listing 6. 9 Function print. Sum. Ave (continued) { // Display results is num. Items is valid. if (num. Items > 0) { cout << “The number of items is “ << num. Items << 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 = “ << num. Items << endl; cout << “Sum and average are not defined. “ << endl; cout << “No printing done. Execution terminated. ” << endl; } // end if } // end print. Sum. Ave CSCE 106 20

Exercise 2 The following formula gives the distance between two points (x 1, y

Exercise 2 The following formula gives the distance between two points (x 1, y 1) and (x 2, y 2) in the Cartesian plane: (x 2 – x 1)2 + (y 2 – y 1)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. u 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. u 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 pr) u 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 pr 2) u Assume p=3. 1416. u 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

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