Download presentation
Presentation is loading. Please wait.
Published byAlexis Ramsey Modified over 8 years ago
1
LECTURE 3 PASS BY REFERENCE
2
METHODS OF PASSING There are 3 primary methods of passing arguments to functions: pass by value, pass by reference, and pass by address. ( will be addressed later)
3
PASS BY VALUE When passing arguments by value, the only way to return a value back to the caller is via the function’s return value. By default, arguments in C++ are passed by value. When arguments are passed by value, a copy of the argument is passed to the function. Because a copy of the argument is passed to the function, the original argument can not be modified by the function.
4
PASS BY VALUE When arguments are passed by value, the called function creates a new variable of the same type as the argument and copies the argument’s value into it. As we noted, the function cannot access the original variable in the calling program, only the copy it created. Passing arguments by value is useful when the function does not need to modify the original variable in the calling program. In fact, it offers insurance that the function cannot harm the original variable.
5
ADV. AND DISADV. OF PASSING BY VALUE ADVANTAGES Arguments passed by value can be: variables (eg. x), literals (eg. 6), or expressions (eg. x+1) or (eg foo()). Arguments are never changed by the function being called, which prevents side effects. DISADVANTAGES Copying large structs or classes can take a lot of time to copy, and this can cause a performance penalty, especially if the function is called many times.
6
PASS BY REFERENCE Passing arguments by reference uses a different mechanism. Instead of a value being passed to the function, a reference to the original variable, in the calling program, is passed. An important advantage of passing by reference is that the function can access the actual variables in the calling program. This provides a mechanism for passing more than one value from the function back to the calling program.
7
In pass by reference, we declare the function parameters as references rather than normal variables: #include Using namespace std ; Void duplicate (int& a, int& b, int & c); Int main () { Int x=1,y=3,z=7; Duplicate (x,y,z); Cout << “x=“<<x<<“, y=“<<y<<“,z=“<<z; Return 0; } Void duplicate (int& a, int& b, int & c) {a*=2; b*=2; c*=2; } PASS BY REFERENCE
8
PASSING BY REFERENCE Reference arguments are indicated by the ampersand (&) following the data type: int& a When the function is called, a will become a reference to the argument. Since a reference to a variable is treated exactly the same as the variable itself, then any changes made to the reference are passed through to the argument!
9
PASSING ARGUMENTS BY REFERENCE Sometimes we need a function to return multiple values. However, functions can only have one return value. One way to return multiple values is using reference parameters. Advantages of Pass By Reference: It allows us to have the function change the value of the argument, which is sometimes useful. Because a copy of the argument is not made, it is fast, even when used with large structs or classes. We can return multiple values from a function.
10
EXAMPLE 1 void foo(int &y) // y is now a reference { cout << "y = " << y << endl; y = 6; cout << "y = " << y << endl; } // y is destroyed here int main() { int x = 5; cout << "x = " << x << endl; foo(x); cout << "x = " << x << endl; return 0; }
11
EXAMPLE 2 void AddOne(int &y) { y++; } int main() { int x = 5; cout << "x = " << x << endl; AddOne(x); cout << "x = " << x << endl; return 0; }
12
EXAMPLE 3 void swap(float &x, float &y); int main() { float a, b; cout << "Enter 2 numbers: " << endl; cin >> a >> b; if(a>b) swap(a,b); cout << "Sorted numbers: "; cout << a << " " << b << endl; return 0; } void swap(float &x, float &y) { float temp; temp = x; x = y; y = temp; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.