Pass by Reference Slide 2 Review on Pass
Pass by Reference
Slide 2 Review on Pass by Value: #include <iostream> using namespace std; void Increment(int Number){ Number = Number + 1; cout << "The parameter Number: " << Number << endl; } void main(){ int I = 10; Increment(I); // parameter is a variable cout << "The variable I is: " << I << endl; } Note: replacing ‘I’ by ‘Number’ in the main does not change anything.
Slide 3 Pass by Reference: Example 1 * To show the function affects a variable which is used as a parameter: #include <iostream> using namespace std; void Increment(int& Number){ Number = Number + 1; cout << "The parameter Number: " << Number << endl; } void main(){ int I = 10; Increment(I); // parameter is a variable cout << "The variable I is: " << I << endl; }
Slide 4 Passing Parameters by Reference To have a function with multiple outputs, we have to use pass by reference. * Efficiency for large objects! * Reference (address) of parameter is passed to the function, instead of its value. * If the function changes the parameter value, the changes will be reflected in the program calling it. *
Slide 5 Specify the passing of a parameter by reference: T& l l l <type>& <variable>, … int& x, double& y, char& z The notation T& means a reference to T Very ‘interesting’ notation we will continue to see it later on … ‘reference’ does not exist in C, ramification of low-level pointer. l A reference is an alternative name for an object. int y=10; int& x = y; // x and y refer to the same int
Slide 6 Pass by value vs. by reference l l Pass by value: formal parameters and arguments are different variables. ideal desirable behavior (but not efficient some times) Pass by reference: they are the same variables, but different names! should carefully handled!
Slide 7 Example 2 *It is possible to use both pass by reference and pass by value parameters in the same function.
Slide 8 // Print the sum and average of two numbers // Input: two numbers x & y // Output: sum - the sum of x & y // average - the average of x & y #include <iostream> using namespace std; void Sum. Ave (double, double&, double&); void main ( ) { double x, y, sum, mean; cout << "Enter two numbers: "; cin >> x >> y; Sum. Ave (x, y, sum, mean); cout << "The sum is " << sum << endl; cout << "The average is " << mean << endl; } void Sum. Ave(double no 1, double no 2, double& sum, double& average) { sum = no 1 + no 2; average = sum / 2; }
Slide 9 * Data areas after call to Sum. Ave:
Slide 10 Example 3: sort 3 integers Input: three numbers First, Second, and Third Output: three numbers in order! Sort 3 numbers, F, S, and T Sort 2 numbers, F, S Sort 2 numbers, S, T Sort 2 numbers, F, S
Slide 11 Input: three numbers First, Second, and Third Output: three numbers in order! Sort F, S, and T Sort F, S Sort S, T Sort F, S if (first > second) swap (first, second);
Slide 12 // Compare and sort three integers #include <iostream> using namespace std; void swap (int&, int&); void main ( ) { int first, second, third; // input integers // Read in first, second and third. cout << "Enter three integers: "; cin >> first >> second >> third; if (first > second) swap (first, second); if (second > third) swap (second, third); if (first > second) swap (first, second); cout << "The sorted integers are " << first << " , " << second << " , " << third << endl; }
Slide 13 // Function for swapping two integers void swap (int& x, int& y) { int temp; temp = x; x = y; y = temp; }
Slide 14 Also possible … #include <iostream> using namespace std; void sort (int&, int&); Void swap (int&, int&); void main ( ) { int first, second, third; // input integers // Read in first, second and third. cout << "Enter three integers: "; cin >> first >> second >> third; sort(first, second); sort(second, third); sort(first, third); cout << "The sorted integers are " << first << , " << second << " , " << third << endl; } void sort(int& x, int& y) { if(x>y) swap(x, y); } void swap (int& x, int& y) { … } "
Slide 15 Summary int main() int max(int x, int y) { int m; if (x>y) m=x; else m=y; return m; } int xx, yy, mm; // OK with m cin >> xx >> yy; mm=max(xx, yy); // functional void max(int x, int y, int& m) { if (x>y) m=x; else m=y; } int xx, yy, mm; cin >> xx >> yy; max(xx, yy, mm); void minmax(int x, int y, int& min, int& max) { if (x>y) {min=y; max=x; } else (min=x; max=y; } } int xx, yy, mmin, mmax; cin >> xx >> yy; minmax(xx, yy, mmin, mmax);
Slide 16 Example 4: programme tracing // Pass-by-reference versus pass-by-value example #include <iostream> using namespace std; void One (int a, int b, int& c) { int d; a = 10; b = 11; c = 12; d = 13; cout << "The values of a, b, c, and d in One: "; cout << a << " " << b << " " << c << " " << d << endl; } void Two (int a, int b, int& d) { int c = 0; cout << "The values of a, b, c, and d in Two: "; cout << a << " " << b << " " << c << " " << d << endl; }
Slide 17 void main () { int a = 1, b = 2, c One(a, b, c); cout << "The values cout << a << " " << Two(a, b, d); cout << "The values cout << a << " " << } = 3, d = 4; of a, b, c, and d after One: "; b << " " << c << " " << d << endl; of a, b, c, and d after Two: "; b << " " << c << " " << d << endl;
- Slides: 17