+ 1
Can anybody tell me the difference between call by reference and call by value?
call
2 Answers
+ 1
+ 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)}