Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.

Similar presentations


Presentation on theme: "Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding."— Presentation transcript:

1 Computer Science 1620 Reference Parameters

2 Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding argument in the function call #include using namespace std; double factorial(int x) { double result = 1; for (int y = 1; y <= x; y++) result *= y; return result; } int main() { cout << "5! = " << factorial(5) << endl; return 0; } Before function starts to execute, x is assigned the value of its corresponding argument. It is as though an implicit x=5 exists in the code before the function starts

3 Parameters – Pass by Value what about when a variable is used e.g. what is the output of this code? #include using namespace std; void change(int x) { x = 2; } int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0; }

4 Parameters – Pass By Value why didn't function change y's value to 2 the parameter only receives a copy of x's value, not the actual value itself the same reason that this code outputs 4 #include using namespace std; int main() { int y = 4; int x = y; x = 2; cout << "y = " << y << endl; return 0; }

5 Program Memory: Program: Output: #include using namespace std; void change(int x) { x = 2; } int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0; }

6 Program Memory: Program: Output: #include using namespace std; void change(int x) { x = 2; } int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0; } int y = 4; main: y 4

7 Program Memory: Program: Output: #include using namespace std; void change(int x) { x = 2; } void main() { int y = 4; change(y); cout << "y = " << y << endl; return 0; } change(y); main: y 4 change: x 4

8 Program Memory: Program: Output: #include using namespace std; void change(int x) { x = 2; } int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0; } x = 2; main: y 4 change: x 42

9 Program Memory: Program: Output: #include using namespace std; void change(int x) { x = 2; } int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0; } main: y 4 change: x 42

10 Program Memory: Program: Output: #include using namespace std; void change(int x) { x = 2; } int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0; } main: y 4 change: x 42 cout << "y = " << y << endl; 4

11 Program Memory: Program: Output: #include using namespace std; void change(int x) { x = 2; } int main() { int y = 4; change(y); cout << "y = " << y << endl; return 0; } main: y 4 change: x 42 return 0; 4

12 Pass By Value when a parameter receives only a copy of its argument value, this is called pass by value all of our examples so far have followed this paradigm

13 Functions (using pass by value) three limitations 1) Cases exist where changing variable would be valuable 2) Making copies of data is sometimes expensive and unnecessary 3) We can currently only return one value from a function

14 Functions (using pass by value) Limitation 1: Cases exist where changing the value of the variable would be useful Example: write a function that swaps the values of two variables (very useful for sorting)

15 #include using namespace std; void swap(int a, int b) { int temp = b; b = a; a = temp; } int main() { int x = 3; int y = 4; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0; }

16 Functions (using pass by value) Limitation 2: Making copies of the data is unnecessary and sometimes expensive Example: write a function that takes a name, city, and province as inputs, and writes a short bio of the person.

17 #include using namespace std; void bio(string name, string city, string province) { cout << name << " currently lives in " << city << ", " << province << endl; } int main() { string name = "Kevin"; string city = "Lethbridge"; string province = "Alberta"; bio(name, city, province); return 0; } Each string gets two copies in memory one for main one for bio Two copies is unnecessary, since bio never changes these strings.

18 Functions (using pass by value) Limitation 3: Functions can only return one value Example: write a function that takes the radius of a circle as its input, and returns its area and its circumference

19 #include using namespace std; const double PI = 3.14159; ??? area_and_circumference(double radius) { double area = PI * radius * radius; double circumference = 2 * PI * radius; return ?????; }

20 What do we do when pass by value doesn't work? One possible solution: global variables e.g. consider our last problem (two return values) we could simply have two global variables to store the results of our computation

21 #include using namespace std; const double PI = 3.14159; double area; double circumference; void area_and_circumference(double radius) { area = PI * radius * radius; circumference = 2 * PI * radius; return ?????; } int main() { area_and_circumference(5); cout << "Area = " << area << endl; cout << "Circumference = " << circumference << endl; return 0; }

22 The previous slide works but we know that global variables are not recommended for passing information between functions Is there a way to avoid the limitations of pass-by-value, without resorting to global variables? Yes: pass-by-reference

23 Reference Variables similar to a standard variable shares its memory with another variable typename The left side of the assignment operator looks exactly like a normal variable declaration – with the inclusion of an & before the variable name. The reference variable is set to another variable. The variable var and the reference variable name now share the same memory. &= var;

24 Reference Variables once a reference variable is declared, you use it just like any other variable you only need the & at declaration that is, you can use it in an expression on the left side of assignment etc.

25 Example #include using namespace std; int main() { int y = 4; int x = y; x = 2; cout << "y = " << y << endl; return 0; } What happens if we make x a reference variable?

26 Example #include using namespace std; int main() { int y = 4; int &x = y; x = 2; cout << "y = " << y << endl; return 0; } What happens if we make x a reference variable?

27 Program Memory: Program: Output: #include using namespace std; int main() { int y = 4; int &x = y; x = 2; cout << "y = " << y << endl; return 0; }

28 Program Memory: Program: Output: #include using namespace std; int main() { int y = 4; int &x = y; x = 2; cout << "y = " << y << endl; return 0; } main: y 4 int y = 4;

29 Program Memory: Program: Output: #include using namespace std; int main() { int y = 4; int &x = y; x = 2; cout << "y = " << y << endl; return 0; } main: y 4 int &x = y; main: x Because x is a reference variable that is set equal to y, it now shares y's memory.

30 Program Memory: Program: Output: #include using namespace std; int main() { int y = 4; int &x = y; x = 2; cout << "y = " << y << endl; return 0; } main: y 2 x = 2; main: x

31 Program Memory: Program: Output: #include using namespace std; int main() { int y = 4; int &x = y; x = 2; cout << "y = " << y << endl; return 0; } main: y 2 cout << "y = " << y << endl; main: x 2

32 Reference Variables Some rules: when declaring a reference variable in a function, you must set it to another variable at declaration int &x; // this is a compiler error once a reference variable is set, you cannot set it to another variable int a, b; int &x = a; &x = b; // this is a compiler error

33 Reference Parameters in practice, reference variables are rarely declared inside a function instead, they are used as function parameters these are referred to as reference parameters reference parameters allow us to pass-by- reference

34 #include using namespace std; void swap(int a, int b) { int temp = b; b = a; a = temp; } int main() { int x = 3; int y = 4; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0; } Example: Write a function called swap that swaps the values of two variables. What happens if we make a and b reference parameters?

35 #include using namespace std; void swap(int &a, int &b) { int temp = b; b = a; a = temp; } int main() { int x = 3; int y = 4; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0; } Example: Write a function called swap that swaps the values of two variables. What happens if we make a and b reference parameters?

36 Program Memory: Program: Output: cout << "y = " << y << endl; void swap(int &a, int &b) { int temp = b; b = a; a = temp; } int main() { int x = 3; int y = 4; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0; }

37 Program Memory: Program: Output: cout << "y = " << y << endl; void swap(int &a, int &b) { int temp = b; b = a; a = temp; } int main() { int x = 3; int y = 4; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0; } int x = 3; main: x 3

38 Program Memory: Program: Output: cout << "y = " << y << endl; void swap(int &a, int &b) { int temp = b; b = a; a = temp; } int main() { int x = 3; int y = 4; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0; } int y = 4; main: x 3 main: y 4

39 Program Memory: Program: Output: cout << "y = " << y << endl; void swap(int &a, int &b) { int temp = b; b = a; a = temp; } int main() { int x = 3; int y = 4; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0; } cout << "x = " << x << ", y = " << y << endl; main: x 3 main: y 4 x = 3, y = 4

40 Program Memory: Program: Output: cout << "y = " << y << endl; void swap(int &a, int &b) { int temp = b; b = a; a = temp; } int main() { int x = 3; int y = 4; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0; } swap(x, y); main: x 3 main: y 4 x = 3, y = 4 swap: a swap: b

41 Program Memory: Program: Output: cout << "y = " << y << endl; void swap(int &a, int &b) { int temp = b; b = a; a = temp; } int main() { int x = 3; int y = 4; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0; } int temp = b; main: x 3 main: y 4 x = 3, y = 4 swap: a swap: b swap: temp 4

42 4 Program Memory: Program: Output: cout << "y = " << y << endl; void swap(int &a, int &b) { int temp = b; b = a; a = temp; } int main() { int x = 3; int y = 4; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0; } b = a; main: x 3 main: y x = 3, y = 4 swap: a swap: b swap: temp 4 3

43 4 Program Memory: Program: Output: cout << "y = " << y << endl; void swap(int &a, int &b) { int temp = b; b = a; a = temp; } int main() { int x = 3; int y = 4; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0; } a = temp; main: x 3 main: y x = 3, y = 4 swap: a swap: b swap: temp 4 3 4

44 4 Program Memory: Program: Output: cout << "y = " << y << endl; void swap(int &a, int &b) { int temp = b; b = a; a = temp; } int main() { int x = 3; int y = 4; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0; } main: x 3 main: y x = 3, y = 4 swap: a swap: b swap: temp 4 3 4

45 4 Program Memory: Program: Output: cout << "y = " << y << endl; void swap(int &a, int &b) { int temp = b; b = a; a = temp; } int main() { int x = 3; int y = 4; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "x = " << x << ", y = " << y << endl; return 0; } main: x 3 main: y x = 3, y = 4 4 3 4 cout << "x = " << x << ", y = " << y << endl; x = 4, y = 3

46 Reference Parameters when a variable is passed by reference, changes to the value of the parameter affect the corresponding argument variable e.g. last example what else do reference parameters give us? more efficient parameter passing

47 #include using namespace std; void bio(string name, string city, string province) { cout << name << " currently lives in " << city << ", " << province << endl; } int main() { string name = "Kevin"; string city = "Lethbridge"; string province = "Alberta"; bio(name, city, province); return 0; } write a function that takes a name, city, and province as inputs, and writes a short bio of the person. Each string gets two copies in memory one for main one for bio What if we made the parameters reference parameters?

48 #include using namespace std; void bio(string &name, string &city, string &province) { cout << name << " currently lives in " << city << ", " << province << endl; } int main() { string name = "Kevin"; string city = "Lethbridge"; string province = "Alberta"; bio(name, city, province); return 0; } write a function that takes a name, city, and province as inputs, and writes a short bio of the person. Now each string only gets one copy in memory the parameter and argument variable refer to the same memory

49 Receiving multiple values from a function reference parameters can be used to "receive" more than one value from a function Steps: 1. for each value to be returned, the function will declare a reference parameter 2. the calling function will send a variable to each of these reference parameters 3. the called function will assign each return value to one of these reference parameters 4. when the function ends, the return values will be in the variables that were sent

50 #include using namespace std; const double PI = 3.14159; area_and_circumference(double radius) { } int main() { double r = 5.0; // radius of circle return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle. 1. for each value to be returned, the function will declare a reference parameter 2. the calling function will send a variable to each of these reference parameters 3. the called function will assign each return value to one of these reference parameters 4. when the function ends, the return values will be in the variables that were sent

51 #include using namespace std; const double PI = 3.14159; area_and_circumference(double radius) { } int main() { double r = 5.0; // radius of circle return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle. 1. for each value to be returned, the function will declare a reference parameter 2. the calling function will send a variable to each of these reference parameters 3. the called function will assign each return value to one of these reference parameters 4. when the function ends, the return values will be in the variables that were sent

52 #include using namespace std; const double PI = 3.14159; area_and_circumference(double radius, double &area, double &circumference) { } int main() { double r = 5.0; // radius of circle return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle. 1. for each value to be returned, the function will declare a reference parameter 2. the calling function will send a variable to each of these reference parameters 3. the called function will assign each return value to one of these reference parameters 4. when the function ends, the return values will be in the variables that were sent

53 #include using namespace std; const double PI = 3.14159; area_and_circumference(double radius, double &area, double &circumference) { } int main() { double r = 5.0; // radius of circle return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle. 1. for each value to be returned, the function will declare a reference parameter 2. the calling function will send a variable to each of these reference parameters 3. the called function will assign each return value to one of these reference parameters 4. when the function ends, the return values will be in the variables that were sent

54 #include using namespace std; const double PI = 3.14159; area_and_circumference(double radius, double &area, double &circumference) { } int main() { double r = 5.0; // radius of circle double a, c; area_and_circumference(r, a, c); return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle. 1. for each value to be returned, the function will declare a reference parameter 2. the calling function will send a variable to each of these reference parameters 3. the called function will assign each return value to one of these reference parameters 4. when the function ends, the return values will be in the variables that were sent

55 #include using namespace std; const double PI = 3.14159; area_and_circumference(double radius, double &area, double &circumference) { } int main() { double r = 5.0; // radius of circle double a, c; area_and_circumference(r, a, c); return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle. 1. for each value to be returned, the function will declare a reference parameter 2. the calling function will send a variable to each of these reference parameters 3. the called function will assign each return value to one of these reference parameters 4. when the function ends, the return values will be in the variables that were sent

56 #include using namespace std; const double PI = 3.14159; area_and_circumference(double radius, double &area, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius; } int main() { double r = 5.0; // radius of circle double a, c; area_and_circumference(r, a, c); return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle. 1. for each value to be returned, the function will declare a reference parameter 2. the calling function will send a variable to each of these reference parameters 3. the called function will assign each return value to one of these reference parameters 4. when the function ends, the return values will be in the variables that were sent

57 #include using namespace std; const double PI = 3.14159; area_and_circumference(double radius, double &area, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius; } int main() { double r = 5.0; // radius of circle double a, c; area_and_circumference(r, a, c); return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle. 1. for each value to be returned, the function will declare a reference parameter 2. the calling function will send a variable to each of these reference parameters 3. the called function will assign each return value to one of these reference parameters 4. when the function ends, the return values will be in the variables that were sent

58 #include using namespace std; const double PI = 3.14159; area_and_circumference(double radius, double &area, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius; } int main() { double r = 5.0; // radius of circle double a, c; area_and_circumference(r, a, c); cout << "Area = " << a << endl; cout << "Circumference = " << c << endl; return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle. 1. for each value to be returned, the function will declare a reference parameter 2. the calling function will send a variable to each of these reference parameters 3. the called function will assign each return value to one of these reference parameters 4. when the function ends, the return values will be in the variables that were sent

59 Reference Parameters what is the return type of the previous program? since no value is being "returned" through a return statement, it's return type is void

60 #include using namespace std; const double PI = 3.14159; void area_and_circumference(double radius, double &area, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius; } int main() { double r = 5.0; // radius of circle double a, c; area_and_circumference(r, a, c); cout << "Area = " << a << endl; cout << "Circumference = " << c << endl; return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

61 Reference Parameters and Return Values if your function returns more than one value, you can use both the return value and reference parameters return one value through a return statement return the rest of the values through reference parameters e.g. rewrite the previous program, but return the area through a return statement, and the circumference through a reference parameter

62 #include using namespace std; const double PI = 3.14159; void area_and_circumference(double radius, double &area, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius; } int main() { double r = 5.0; // radius of circle double a, c; area_and_circumference(r, a, c); cout << "Area = " << a << endl; cout << "Circumference = " << c << endl; return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

63 #include using namespace std; const double PI = 3.14159; void area_and_circumference(double radius, double &area, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius; } int main() { double r = 5.0; // radius of circle double a, c; area_and_circumference(r, a, c); cout << "Area = " << a << endl; cout << "Circumference = " << c << endl; return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle. Since area will be returned using a return statement, we do not need to use a reference parameter for this value.

64 #include using namespace std; const double PI = 3.14159; void area_and_circumference(double radius, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius; } int main() { double r = 5.0; // radius of circle double a, c; area_and_circumference(r, a, c); cout << "Area = " << a << endl; cout << "Circumference = " << c << endl; return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

65 #include using namespace std; const double PI = 3.14159; void area_and_circumference(double radius, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius; } int main() { double r = 5.0; // radius of circle double a, c; area_and_circumference(r, a, c); cout << "Area = " << a << endl; cout << "Circumference = " << c << endl; return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle. Since area will be returned using a return statement, we need to declare what type this return value will be.

66 #include using namespace std; const double PI = 3.14159; double area_and_circumference(double radius, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius; } int main() { double r = 5.0; // radius of circle double a, c; area_and_circumference(r, a, c); cout << "Area = " << a << endl; cout << "Circumference = " << c << endl; return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

67 #include using namespace std; const double PI = 3.14159; double area_and_circumference(double radius, double &circumference) { area = PI * radius * radius; circumference = 2 * PI * radius; } int main() { double r = 5.0; // radius of circle double a, c; area_and_circumference(r, a, c); cout << "Area = " << a << endl; cout << "Circumference = " << c << endl; return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle. This value should be returned, not stored in a variable.

68 #include using namespace std; const double PI = 3.14159; double area_and_circumference(double radius, double &circumference) { circumference = 2 * PI * radius; return PI * radius * radius; } int main() { double r = 5.0; // radius of circle double a, c; area_and_circumference(r, a, c); cout << "Area = " << a << endl; cout << "Circumference = " << c << endl; return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

69 #include using namespace std; const double PI = 3.14159; double area_and_circumference(double radius, double &circumference) { circumference = 2 * PI * radius; return PI * radius * radius; } int main() { double r = 5.0; // radius of circle double a, c; area_and_circumference(r, a, c); cout << "Area = " << a << endl; cout << "Circumference = " << c << endl; return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle. The area of the circle is not returned through a reference parameter, but is the value of the function call.

70 #include using namespace std; const double PI = 3.14159; double area_and_circumference(double radius, double &circumference) { circumference = 2 * PI * radius; return PI * radius * radius; } int main() { double r = 5.0; // radius of circle double a, c; a = area_and_circumference(r, c); cout << "Area = " << a << endl; cout << "Circumference = " << c << endl; return 0; } Write a function that takes a radius of a circle, and returns the area and circumference of that circle.

71 Reference Parameters and Return Values When my function returns a value, should I use a return statement, or reference parameters? if your function returns only one value, a return statement is typically used if your function returns more than one value, you need to use a reference parameter if you wish, you can use both

72 Another Example write a function that receives two numbers A and B, and returns A / B in division format. That is, it returns the quotient and the remainder. e.g., if A = 5, and B = 2, the function should return 2 as the quotient, and 1 as the remainder

73 Design (function): Step 1: Compute the quotient of A / B Step 2: Compute the remainder of A / B Step 3: Return these two values

74 divide(int A, int B, ) { Step 1: Compute the quotient of A / B Step 2: Compute the remainder of A / B Step 3: Return these two values }

75 divide(int A, int B, ) { Step 1: Compute the quotient of A / B Step 2: Compute the remainder of A / B Step 3: Return these two values }

76 divide(int A, int B, ) { int quotient = A / B; Step 2: Compute the remainder of A / B Step 3: Return these two values }

77 divide(int A, int B, ) { int quotient = A / B; Step 2: Compute the remainder of A / B Step 3: Return these two values }

78 divide(int A, int B, ) { int quotient = A / B; int remainder = A % B; Step 3: Return these two values }

79 divide(int A, int B, ) { int quotient = A / B; int remainder = A % B; Step 3: Return these two values }

80 divide(int A, int B, ) { int quotient = A / B; int remainder = A % B; Step 3.1: Return quotient Step 3.2: Return remainder }

81 How do we return these two values: two choices Choice 1: return them both as reference parameters Choice 2: return one as the return value, and one as a reference parameter

82 void divide(int A, int B, ) { int quotient = A / B; int remainder = A % B; Step 3.1: Return quotient as ref. param Step 3.2: Return remainder as ref. param }

83 void divide(int A, int B,int &qparam ){ int quotient = A / B; int remainder = A % B; qparam = quotient; Step 3.2: Return remainder as ref. param }

84 void divide(int A, int B, int &qparam ){ int quotient = A / B; int remainder = A % B; qparam = quotient; Step 3.2: Return remainder as ref. param }

85 void divide(int A, int B, int &qparam, int &rparam){ int quotient = A / B; int remainder = A % B; qparam = quotient; rparam = remainder; }

86 void divide(int A, int B, int &qparam, int &rparam){ int qparam = A / B; int rparam = A % B; }

87 How do we return these two values: two choices Choice 1: return them both as reference parameters Choice 2: return one as the return value, and one as a reference parameter

88 divide(int A, int B, ) { int quotient = A / B; int remainder = A % B; Step 3.1: Return quotient as ref. param Step 3.2: Return remainder as ret. value }

89 divide(int A, int B, int &qparam) { int quotient = A / B; int remainder = A % B; qparam = quotient; Step 3.2: Return remainder as ret. value }

90 divide(int A, int B, int &qparam) { int quotient = A / B; int remainder = A % B; qparam = quotient; Step 3.2: Return remainder as ret. value }

91 int divide(int A, int B, int &qparam) { int quotient = A / B; int remainder = A % B; qparam = quotient; return remainder; }

92 int divide(int A, int B, int &qparam) { qparam = A / B; return A % B; }

93 Summary reference variables are variables that share memory with another variable that's already declared Advantages of reference parameters: allow a function to change the state of a variable outside of that function improve efficiency by avoiding duplication allow multiple return values from a function


Download ppt "Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding."

Similar presentations


Ads by Google