-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_CallByValue_Reference.cpp
More file actions
66 lines (57 loc) · 1.46 KB
/
Copy path10_CallByValue_Reference.cpp
File metadata and controls
66 lines (57 loc) · 1.46 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
#include <iostream>
using namespace std;
// Call by Value Function
int SwapByValue(int a, int b)
{
int temp = a;
a = b;
b = temp;
return a, b;
}
// Call by Reference Functions
// 1. Using Pointer:
int SwapByPointer(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
return *a, *b;
}
// 2. Using Reference Variables:
int SwapByReference(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
return a, b;
}
int main()
{
cout << "Call by Value, and by reference\n";
/* When we call a function like func(*args) we call it by value, means we make a copy of them in new memory with
new name then proceed operations or whatever we want to do then function return same value at end if we return
both a and b. To override this copying we use referencing.
*/
int j, k;
cout << "Enter the value of j and k:\n";
cin >> j >> k;
int a = j;
int b = k;
cout << "Value Swaper\nWe will make a function which will swap value of two variables.\n\n";
cout << "Call by Value(This will not swap value):" << endl;
SwapByValue(j, k);
cout << "The value of j is " << j << endl;
cout << "The value of k is " << k << "\n\n";
cout << "Call by Reference:\n\n";
cout << "Swap by Pointer:\n";
SwapByPointer(&j, &k);
cout << "The value of j is " << j << endl;
cout << "The value of k is " << k << "\n\n";
j = a;
k = b;
cout << "Swap by Reference Variable:\n";
SwapByReference(j, k);
cout << "The value of j is " << j << endl;
cout << "The value of y is " << k << "\n\n";
return 0;
}