-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtut16.cpp
More file actions
67 lines (59 loc) · 1.66 KB
/
tut16.cpp
File metadata and controls
67 lines (59 loc) · 1.66 KB
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
60
61
62
63
64
65
66
67
// #include <iostream>
// using namespace std;
// // ITs called call by refrence using refrence variable
// void swap(int *a, int *b) {
// int temp;
// temp = *a ;
// *a = *b ;
// *b = temp ;
// }
// int main()
// {
// int x=34 , y =99 ;
// cout<< "The value of x is: "<<x<<endl<<"The value of y is: "<<y<<endl;
// swap(x,y);
// cout<< "The value of x after swap is: "<<x<<endl<<"The value of y after swap is: "<<y<<endl;
// }
// Code with harry
#include <iostream>
using namespace std;
int sum(int a, int b)
{
int c = a + b;
return c;
}
// This will not swap a and b
void swap(int a, int b)
{ // temp a b
int temp = a; // 4 4 5
a = b; // 4 5 5
b = temp; // 4 5 4
}
// Call by reference using pointers
void swapPointer(int *a, int *b)
{ // temp a b
int temp = *a; // 4 4 5
*a = *b; // 4 5 5
*b = temp; // 4 5 4
}
// Call by reference using C++ reference Variables
// int &
void swapReferenceVar(int &a, int &b)
{ // temp a b
int temp = a; // 4 4 5
a = b; // 4 5 5
b = temp; // 4 5 4
// return a;
}
int main()
{
int x = 4, y = 5;
// cout<<"The sum of 4 and 5 is "<<sum(a, b);
cout << "The value of x is " << x << " and the 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 variables
// swapReferenceVar(x, y) = 766; //This will swap a and b using reference variables
cout << "The value of x is " << x << " and the value of y is " << y << endl;
return 0;
}