functions pass by reference alina solovyova-vincent department of computer science & engineering...

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

Upload: amberlynn-bennett

Post on 01-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

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

Functions

Pass by Reference

Alina Solovyova-Vincent

Department of Computer Science & Engineering

University of Nevada, Reno

Fall 2005

Page 2: 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.

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

#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

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

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)

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

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!!!

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

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.

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

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&);

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

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);

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

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);

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

#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

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

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.

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

#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