Download presentation
Presentation is loading. Please wait.
Published byὈρφεύς Ράγκος Modified over 6 years ago
1
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Ask questions one by one and stopped at slide 18
2
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?
3
In, Out, InOut Parameters
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.
4
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
5
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
6
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.
7
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.
8
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)
9
Use Reference parameters only when necessary!
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 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) // Will compile and run. // Won’t get values in the calling function.
10
Value Parameter What can the actual parameter be? A VALUE!
Literal value (magic number) Variable (initialized!) Expression (with all variables initialized)
11
Function Calls // Function Prototype float sqrt(float x);
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
12
float GrossPay(float payRate, float hours);
cin >> hours >> rate; gross = GrossPay(rate, hours); // Valid function call? gross = GrossPay(rate, 35.5); // Holiday hours gross = GrossPay(2 * rate, hours);
13
Uninitialized Variable
float GrossPay(float payRate, float hours); float gross, hours, rate; // Call function with uninitialized variable gross = GrossPay(rate, hours); VS Error: Run-Time Check Failure #3 - The variable ‘hours' is being used without being initialized.
14
Reference Parameter What can the actual parameter be? AN ADDRESS!
Literal value: NO Expression : NO Variable : YES (initialized or uninitialized) void GetInput(float& rate, float& hours); float rate, hours; GetInput(rate, hours); // Valid function call? GetInput(rate, 35); GetInput(2 * rate, hours);
15
Actual Parameter Does the actual parameter change its value when the corresponding formal parameter changes its value? Value Parameter NO! Reference Parameter YES!
16
Function Calls void myFun(float num, int& count, float& total);
float score, sum = 0; int ScoreCount = 0; myFun(score, ScoreCount, sum); // Q: Valid function call? myFun(55.5, ScoreCount, sum); cin >> score; // for all following questions myFun(pow(score, 2), ScoreCount, sum); myFun(score, 2 * ScoreCount, sum); myFun(score, ScoreCount, 100); cout << myFun(score, ScoreCount, sum); // Q: Valid statement?
17
Function Calls void myFun(float num, int& count, float& total);
score = 55.0; sum = 100.5; ScoreCount = 5; myFun(score, ScoreCount, sum); // Q: What is the value after the function call? // score unknown // sum unknown // ScoreCount unknown
18
Function Calls char goodFun(float& num, char choice);
char input, output; float amount = 50.5; input = goodFun(amount, ‘A’); // Q: Valid function call? input = goodFun(amount, output); input = goodFun(100, ‘A’); cout << goodFun(amount, goodFun(amount, ‘A’)); // Q: Valid statement? goodFun(amount, ‘A’);
19
// Parameters: (?, ?) void WhoseFun(int param1, int& param2) { param2 = 3; param2 += param1; cin >> param1; return; } // param1: In, Out, InOut? // param2: In, Out, InOut?
20
void WhoseFun(int param1,
void main() { int num1; int num2; num1 = 5; // num1 = ? // num2 = ? WhoseFun(num1, num2); return; } // Parameters: (?, ?) void WhoseFun(int param1, int& param2) { param2 = 3; param2 += param1; cin >> param1; // 9 return; } // param1: In, Out, InOut // param2: In, Out, InOut
21
void WhoseFun(int param1,
void main() { float num1; int num2; num1 = 5; num2 = 10; // num2 has a value! WhoseFun(num1, num2); // num1 = ? // num2 = ? return; } // Parameters: (?, ?) void WhoseFun(int param1, int& param2) { param2 = 3; param2 += param1; cin >> param1; // 9 return; } // param1: In, Out, InOut // param2: In, Out, InOut
22
// Parameters: (?, ?) int WhichFun(int param1, int& param2) { if (param2 > 0) param1 = sqrt(param2); param2 = 3; param2 += param1; cin >> param1; return param1; } // param1: In, Out, InOut // param2: In, Out, InOut // Is it a good function? // NO: Return value and InOut parameter! // Programming Rule!
23
int main() { float score, highest, lowest; int scoreCount = 0; cin >> score; while (!cin.eof()) if (scoreCount == 0) highest = score; lowest = score; } else if (score > highest) if (score < lowest) scoreCount ++; // Display result return 0; // Task: Use a function to update highest and lowest.
24
Use a function to update highest and lowest
Function Prototype Name: UpdateMaxMin Type: void Parameters: How many? score In, Out, InOut? max min Pass by Value or Pass by Reference?
25
// -------------------------------------------------
// The function uses the value of num to update the // values of max and min. // Parameters: ( In , InOut , InOut ) void UpdateMaxMin(float num, float& max, float &min) { if (num > max) max = num; if (num < min) min = num; return; }
26
void UpdateMaxMin(float value, float& max, float& min);
int main() { float score, highest, lowest; int scoreCount = 0; cin >> score; while (!cin.eof()) if (scoreCount == 0) highest = score; lowest = score; } else UpdateMaxMin(score, highest, lowest); scoreCount ++; // Display result return 0;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.