-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16_Call_by_value_and_reference.cpp
More file actions
51 lines (36 loc) · 1.17 KB
/
Copy path16_Call_by_value_and_reference.cpp
File metadata and controls
51 lines (36 loc) · 1.17 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
#include<iostream>
using namespace std;
// This function won't work as we have given the copy of x and y...!!
// So to actually swap them we have to provide the addess of x and y, so that we can change the value directly from the address...!!
// #1 Call by Value
void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
// This function has a direct address of x and y. So by this change the value of x and y.
// #2 Call by Reference using Pointers
void swapPointer(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
// #3 Call by Reference using C++ Reference Variables...!!
void swapReferenceVar(int &a, int &b){
int temp = a;
a = b;
b = temp;
}
int main()
{
int x = 4, y = 5;
cout << "The value of x and y before swapping is: " << x << ", " << y << endl;
// ***************** Call by Value ****************************
// swap(x, y);
// *************** Call by Reference **************************
// swapPointer(&x, &y);
// *************** Call by Reference using C++ ****************
swapReferenceVar(x, y);
cout << "The value of x and y after swapping is: " << x << ", " << y << endl;
return 0;
}