-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtut16.cpp
59 lines (43 loc) · 1.12 KB
/
tut16.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
using namespace std;
// This will not swap a and b.
// void swap(int a, int b)
// {
// int temp = a;
// a = b;
// b = temp;
// }
// Call by reference using Pointers
void swapPointer(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// Call by reference using C++ Reference variables
// void swapReferenceVar(int &a, int &b)
// {
// int temp = a;
// a = b;
// b = temp;
// }
// return by reference
int &swapReferenceReturn(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
return a;
}
int main()
{
// Call by value and Call by reference.
int x = 5, y = 3;
cout << "value of x is " << x << " and value of y is " << y << endl;
// swap(x,y); // This will not swap a and b.
// swapPointer(&x, &y); // This will swap a and b using pointer reference.
// swapReferenceVar(x, y); // This will swap a and b using reference variable.
swapReferenceReturn(x, y) = 766; // this will change value of x as function return value(ie. a) is pointing x.
cout << "value of x is " << x << " and value of y is " << y << endl;
return 0;
}