Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pass by Reference.

Similar presentations


Presentation on theme: "Pass by Reference."— Presentation transcript:

1 Pass by Reference

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.

3 Pass by Reference: Example 1
To show how 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;

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.

5 Specify the passing of a parameter by reference: T&
<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. A reference is an alternative name for an object. int y=10; int& x = y; // x and y refer to the same int

6 Pass by value vs. by reference
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!

7 Example 2 It is possible to use both pass by reference and pass by value parameters in the same function.

8 cout << "Enter two numbers: "; cin >> x >> y;
// 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 SumAve (double, double, double&, double&); void main ( ) { double x, y, sum, mean; cout << "Enter two numbers: "; cin >> x >> y; SumAve (x, y, sum, mean); cout << "The sum is " << sum << endl; cout << "The average is " << mean << endl; } void SumAve(double no1, double no2, double& sum, double& average) { sum = no1 + no2; average = sum / 2;

9 Data areas after call to SumAve:

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

11 Input: three numbers First, Second, and Third
Output: three numbers in order! Sort F, S, and T Sort F,S Sort S,T if (first > second) swap (first, second);

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); cout << "The sorted integers are " << first << " , " << second << " , " << third << endl; }

13 // Function for swapping two integers
void swap (int& x, int& y) { int temp; temp = x; x = y; y = temp; }

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

15 Summary int main() int max(int x, int y) {
int xx,yy,mm; // OK with m, x, and y cin >> xx >> yy; mm=max(xx,yy); // functional programming style int xx,yy,mm; max(xx,yy,mm); int xx,yy,mmin,mmax; minmax(xx,yy,mmin,mmax); int max(int x, int y) { int m; if (x>y) m=x; else m=y; return m; } void max(int x, int y, int& m) { void minmax(int x, int y, int& min, int& max) { if (x>y) {min=y; max=x;} else (min=x; max=y;}

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: ";

17 void main () { int a = 1, b = 2, c = 3, d = 4; One(a, b, c); cout << "The values of a, b, c, and d after One: "; cout << a << " " << b << " " << c << " " << d << endl; Two(a, b, d); cout << "The values of a, b, c, and d after Two: "; }


Download ppt "Pass by Reference."

Similar presentations


Ads by Google