-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path39_Passing_by_Reference.cpp
More file actions
44 lines (36 loc) · 1.03 KB
/
39_Passing_by_Reference.cpp
File metadata and controls
44 lines (36 loc) · 1.03 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
#include <iostream>
using namespace std;
/*
In functions chapter, we learned how to pass arguments to a function
There is another way of passing arguments called pass by reference (passing the references of actual parameters rather than actual value)
Consider the following example:
void func1 (int val){
...
}
void func2 (int& ref){
...
}
int main(){
int var = x;
func1(var); //pass by actual value
func2(var); //pass by reference
}
*/
void swapVals(int& val1, int& val2){
int temp;
temp = val1;
val1 = val2;
val2 = temp;
}
int main(){
int n1 = 0, n2 = 1;
cout << n1 << ", " << n2 << endl;
swapVals(n1, n2); //Swap the addresses
cout << n1 << ", " << n2 << endl;
/*
The above task can be done using the pointers (pass by pointers)
void swapVals(int* val1, int* val2)
A dereference operator (*) must be used every time to access the values, so generally pass by reference is easier
Then pass by pointers is discouraged.
*/
}