Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and.

Similar presentations


Presentation on theme: "Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and."— Presentation transcript:

1 Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and update the value of the actual parameter. Value Parameters (No &) The formal parameter receives (a copy of) the value of the actual parameter, and the function can use the value, but can NOT change the value of the actual parameter. How to pass the parameters? 1

2 In, Out, InOut Parameters In The function uses the value of the actual parameter. Out The calling function gets the new or updated value of the formal parameter. (a function can only return one value using the return statement) InOut Both In and Out Why do we pass parameters? Nothing to do with the return statement. 2

3 Function Parameters For each parameter 1. Figure out In, Out or InOut 2. Pass by value or Pass by reference In Pass by value (value parameter) (Does not let the function modify the values without &) Out Pass by reference (reference parameter with &) InOut Pass by reference (reference parameter with &) 3

4 In, Out, InOut Parameters Passing parameters at function call Nothing to do with what the function does. In Parameters Not input inside the function Out Parameter Not output inside the function 4

5 In, Out, InOut Parameters // ----------------------------------------------------------- // The function inputs payRate and hoursWorked and // pass both values back to calling function // Parameters: (out, out) // ----------------------------------------------------------- void GetInput(float& payRate, float& hoursWorked) { cout << “Enter the pay rate: ”; cin >> payRate; cout << “Enter the hours: ”; cin >> hoursWorked; } The function does input. The parameters are OUT. 5

6 In, Out, InOut Parameters // ------------------------------------------------------------ // The function displays avg, max and min in required format. // Parameters: (In, In, In) // ------------------------------------------------------------ void DisplayResult(float avg, float max, float min) { cout << fixed << showpoint << setprecision(2); cout << endl << endl << "The average score: " << setw(8) << avg << “.\n” << "The highest score: " << setw(8) << max << “.\n” << "The lowest score : " << setw(8) << min; } The function does output. The parameters are IN. 6

7 Programming Rules Function description Specify IN, OUT, or INOUT parameters // ----------------------------------------------------------- // The function computes and returns the gross pay based on // the pay rate and hours. Hours over will be paid 1.5 times // the regular pay rate. // Parameters: (in, in) // ----------------------------------------------------------- float GrossPay(float payRate, float hours) { // Function body } // ----------------------------------------------------------- // The function inputs payRate and hoursWorked and // pass both values back to calling function // Parameters: (out, out) // ----------------------------------------------------------- void GetInput(float& payRate, float& hoursWorked) { // Function body } 7

8 Programming Rules Use Reference parameters only when necessary! // ----------------------------------------------------------- // The function computes and returns the gross pay // based on the pay rate and hours. Hours over 40 will be paid // 1.5 times the regular pay rate. // Parameters: (in, in) // ----------------------------------------------------------- float GrossPay(float& payRate, float& hours) { // Function body } // It will it work. // Lose points! // ----------------------------------------------------------- // The function inputs payRate and hoursWorked and // pass both values back to calling function. // Parameters: (out, out) // ----------------------------------------------------------- void GetInput(float payRate, float hoursWorked) { // Function body } // Will compile and run. // Won’t get values in the calling function. 8

9 Value Parameter What can the actual parameter be? A VALUE! Literal value (magic number) Variable (initialized!) Expression (with all variables initialized) 9

10 10 Function Calls // Function Prototype float sqrt(float x); // Function Calls root1 = (-coefB + sqrt(delta)) / (2 * coefA); // Parameter is variable delta with a value cout << “The square root of 10 is “ << sqrt(10); // Parameter is literal value 10 Value = sqrt (coefB * coefB - 4 * coefA * coefC); // Parameter is an expression // with coefA, coefB and coefC initialized

11 float GrossPay(float payRate, float hours); cin >> hours >> rate; gross = GrossPay(rate, hours); // Valid function call? gross = GrossPay(rate, 35.5); // Valid function call? // Holiday hours gross = GrossPay(2 * rate, hours); // Valid function call? 11

12 Uninitialized Variable float GrossPay(float payRate, float hours); // Call function with uninitialized variable float gross, hours, rate; gross = GrossPay(rate, hours); HiC Error: HicUndefException: Accessing uninitialized value from rate on line 28 -- ABNORMAL TERMINATION. 12

13 Reference Parameter What can the actual parameter be? AN ADDRESS! Literal value: NO Expression : NO Variable : YES initialized or uninitialized float rate, hours; GetInput(rate, hours); // Valid function call? GetInput(rate, 35); // Valid function call? GetInput(2 * rate, hours); // Valid function call? 13

14 Prog3 Must use functions! 14

15 Schedule Quiz4-3: Due 9:30 PM today Must use input file! Thursday Lab5: 5 points Prog3 Progress: 2 points Friday Discuss Prog3 Answer all questions Monday, March 21 Drop your Prog3.cpp Wednesday, March 23 Prog3 is due 15

16 16 Loading an Input File in HiC Save the input file on your drive Click the RUN menu Select option "Set Input File …" Click “Load Input” Browse to the file and open it Select the Input (Interactive) radio button Click OK Use input file for Prog3!

17 CS 1430: Programming in C++ QuizProg3 17


Download ppt "Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and."

Similar presentations


Ads by Google