-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtut16.cpp
More file actions
37 lines (34 loc) · 1.29 KB
/
Copy pathtut16.cpp
File metadata and controls
37 lines (34 loc) · 1.29 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
#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
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
}
int main(){
int a = 4; int b = 5;
cout<<"The value of 4 and 5 is "<<sum(a,b)<<endl;
cout<<"The value of a is "<<a<<" and the value of b is "<<b<<endl;
//swap(a, b); //This will not swap a and b
//swapPointer(&a, &b); //This will swap a and b using pointers reference
swapReferenceVar(a, b); //This will swap a and b using reference variables
cout<<"The value of a is "<<a<<" and the value of b is "<<b<<endl;
return 0;
}