File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+
4
+ int sum (int a, int b) {
5
+ int c = a + b;
6
+ return c;
7
+ };
8
+
9
+ // This will not swap a and b.
10
+ void swap (int a, int b) {
11
+ int temp = a;
12
+ a = b;
13
+ b = temp;
14
+ };
15
+
16
+ // Call by reference using pointers
17
+ // This will swap a and b. this will swap the value at address of a and b
18
+ void swapPointer (int * a, int * b) {
19
+ int temp = *a;
20
+ *a = *b;
21
+ *b = temp;
22
+ };
23
+
24
+ // Call by reference using C++ refrence variable
25
+ // This will swap a and b. this will swap the value at address of a and b
26
+ void swapReferenceVar (int &a, int &b) {
27
+ int temp = a;
28
+ a = b;
29
+ b = temp;
30
+ };
31
+
32
+ int main () {
33
+
34
+ int x = 4 , y = 3 ;
35
+ // cout<<"The sum of 4 and 3 is: "<<sum(x, y)<<endl;
36
+
37
+ // swapping variables
38
+ cout<<" Initially the value of x and y was: " <<x<<" and " <<y<<" respectively." <<endl;
39
+
40
+ // swap(x, y); // This will not swap a and b.
41
+
42
+ // swapPointer(&x, &y); // This will swap a and b using pointers.
43
+
44
+ swapReferenceVar (x, y); // This will swap a and b using reference variable.
45
+
46
+ cout<<" Finally the value of x and y is: " <<x<<" and " <<y<<" respectively." <<endl;
47
+
48
+
49
+ return 0 ;
50
+ };
You can’t perform that action at this time.
0 commit comments