functions pass by reference alina solovyova-vincent department of computer science & engineering...
TRANSCRIPT
Functions
Pass by Reference
Alina Solovyova-Vincent
Department of Computer Science & Engineering
University of Nevada, Reno
Fall 2005
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.
#include <iostream>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.53.5
num1 num2
x
y2.5
3.57.5
10.5
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)
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!!!
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.
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&);
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);
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);
#include <iostream>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.53.57.5
Pass by ReferenceWhat 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.
#include <iostream>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 -1c = 6
4 2 -1
5 1 6
2 5 6