0

Whats the difference between call by value and call by reference ?? [In detail]

Can you even provide an example !!!

11th Jan 2017, 10:34 AM
Siddharth Naithani
Siddharth Naithani - avatar
1 Respuesta
+ 9
* In call by value , copies of values of actual parameters are passed, where as in call by reference method, address of actual parameters are passed(using & sign). *In call by value actual parameters do not participate in performing operations, hence changes are not reflected back in main. On the other hand in call by reference formal parameters are actually the other names of actual parameters so operations take place on actual parameters only, hence changes are reflected back in main. The simplest example is the swapping one: Call by value:- #include<iostream> using namespace std; void swap(int a, int b) {a=a+b; b=a-b; a=a-b; } int main() {void swap(int,int) int a,b; cout<<"Enter nos. to be swapped:"; cin>>a>>b; swap(a,b); cout<<"\n a="<<a<<"b=<<b; //nos won't swap return 0; } Call by reference:- #include<iostream> using namespace std; void swap(int &a, int &b) {//same as call by value } int main() {void swap(int &, int &) //same as call by value //here u will see that the nos are swapped } This is too descriptive as u asked. Hope it helped u
11th Jan 2017, 12:24 PM
‎‏‎‏‎Kueen
‎‏‎‏‎Kueen - avatar