-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCallByValueAndRef.cpp
49 lines (33 loc) · 1.22 KB
/
CallByValueAndRef.cpp
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
#include<iostream>
//example to represent call by value and call by reference of function calling
using namespace std;
//In call by value, a copy of actual(args which are passed when func is called)
// arguments is passed to formal(args when defined a func) arguments of the called function and any change made to the
//formal arguments in the called function have no effect on the values of actual arguments in the calling function.
int SwapByVal(int a, int b) {
int temp=a;
a=b;
b=temp;
return (a,b);
}
//in call by reference we pass the location(address) of the actual arguments as formal arguments when calling function. So when a function is defined
// we pass pointers as formal arguments as using the pointers we can easily indirectly manipulte the values of the actual(passed) arguments from within the func using their address
int SwapByRef(int *a, int *b) {
int temp = *a;
*a=*b;
*b=temp;
return (*a,*b);
}
int main () {
int a=10,b=20;
SwapByVal(a,b);
cout<<"a is : "<<a<<endl;
cout<<"b is: "<<b<<endl; //values of a and b unchanged
cout<<"\n";
cout<<"When swap function is called by reference"<<endl;
cout<<"\n";
SwapByRef(&a,&b);
cout<<"a is : "<<a<<endl;
cout<<"b is: "<<b<<endl;
return 0;
}