+ 1

Can anybody tell me the difference between call by reference and call by value?

call

26th Feb 2017, 2:48 AM
Bikash Sah
Bikash Sah - avatar
2 Answers
26th Feb 2017, 4:11 AM
Arshit patel
Arshit patel - avatar
+ 1
In call by value the parameters are passed duplicates or copies of it are created( formal parameters) so any change to these variables in the function wouldnt be reflected back where as in call by reference method you pass the variables addresses& so any change is made it happens in the passed address thus change in values of function reflected back. example: void sum(int a,int b) { a=a+b; b=a+b; cout<<a<<b;} void sumr(int &a,int &b) { a=a+b; b=a+b;} void main() { int x=1,y=2; sum(x,y); cout<<x<<y; // output 1 and 2 ( callbyvalue) sumr(x,y); // call by reference cout<<x<<y; // output 3 and 5 (changed)}
26th Feb 2017, 4:50 AM
Raj mandal
Raj mandal - avatar