Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.

Similar presentations


Presentation on theme: "Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005."— Presentation transcript:

1 Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

2 Pass by Value §What happens when a function is invoked using value parameters? a copy of the actual parameters' values is made this copy of the values is placed in the formal parameters of the function the function has no access to the original actual parameter variables. It cannot change them as it only has a copy of their values.

3 #include using namespace std; void change(float, float); void main() { float num1= 2.5, num2= 3.5; cout<<num1<<“ “<<num2; change(num1, num2); cout<<num1<<“ “<<num2; } void change (float x, float y) { cout <<x<<" " << y << endl; x = 10.5; y = 7.5; cout <<x<<" " << y << endl; } Memory 2.5 3.5 num1 num2 x y 2.5 3.57.5 10.5

4 Pass by Reference Passing parameters by reference gives the called function access to the actual memory locations of the passed variables. Use & (ampersand) & = “the address of …” Ex. int function1 (int &, float &, int)

5 Pass by Reference §Any changes made to the variable within the body of the called function are maintained when control returns to the calling function. §I.e. the values of the actual parameters will change!!!

6 Parameter List FunctionName (Datatype& VariableName, Datatype& VariableName,... )  When the & is omitted - the parameters are accepted as Pass By Value.  When the & is included - the parameters are accepted as Pass By Reference.

7 Function Prototype Pass by value: int function1 ( int, int, int); Pass by reference: int function2 (int &, int &, int &); Mixed function: int function3 ( int&, int, int&);

8 Function Call int a =1, b=2, c=3, result1, result2, result3; result1 = function1 (a, b, c); result2 = function2 (a, b, c); result3 = function3 (a, b, c);

9 Actual Parameters When the formal parameter is: Value  actual parameter may be:  variable i.e. function (a,b,c);  const i.e. function (1, 2, 5);  expression i.e. function (a+b, b+1, c*c); Reference  actual parameter must be:  variable only i.e. function (a, b, c);

10 #include using namespace std; void change(float &, float&); void main() { float num_1= 2.5, num_2= 3.5; cout<<num1<<“ “<<num2; change(num_1, num_2); cout<<num1<<“ “<<num2; } void change (float & x, float &y) { cout <<x<<" " << y << endl; x = 10.5; y = 7.5; cout <<x<<" " << y << endl; } Memory 2.5 num_1 num_2 x y 10.5 3.57.5

11 Pass by Reference §What happens when a function is invoked using reference parameters?  the location (memory address) of the actual parameter, not its value, is passed to the function  there is only one copy of the info and it is used by both the calling and the called function  the actual parameter and formal parameter become synonyms for the same location in memory  all changes made to the formal parameters in the called function have an effect on the actual parameters in the calling function.

12 #include using namespace std; int change (int&, int, int); void main() { int a = 2, b = 4, c = -1; cout << a << “ “ << b << “ “ << c << endl; c = change (b, a, c); cout << a << “ “ << b << “ “ << c << endl; } int change (int& x, int y, int z) { cout << x<< “ “ << y << “ “ << z << endl; x++; y--; z = x + y; cout << x<< “ “ << y << “ “ << z << endl; return z; } 2 4 -1 c = 6 4 2 -1 5 1 6 2 5 6


Download ppt "Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005."

Similar presentations


Ads by Google