Question:What do you mean by call by value and call by reference? Under what circumstances call by reference is to be preferred over call by value?

## Call by value

In call by value method of calling function, values of actual arguments are supplied to the called function. Any change in the value inside the function is for the function only. Such changes do not affect the original variables passed. To understand the concept of call by value , consider the following program.

## Program to explain passing parameters by value.

```#include <iostream>

using namespace std;

void sum (int x, int y);

int main()
{
int a,b;
a = 100;
b = 200;
cout <<" value before function call are  a ="<<a<<" b="<<b<<endl;
sum(a,b);
cout <<" value after function call are  a ="<<a<<" b="<<b<<endl;
return 1;
}
void sum (int x, int y)
{
x= x+10;
y= y+10 ;
cout<<" Value inside the function are  x ="<<x<<" y="<<y<<endl;
}
```

## Output

```Value before function  call are a = 100 b =200
Value inside the function are x = 110 y =210
Value after function call  are a = 100  b = 200
```

In the above example, the function sum ( ) is called from the main ( ) function by passing actual parameter a and b using call by value as formal parameters x and y. Four memory locations each of two bytes are allocated for actual parameters a, b and formal parameters x,y. The changes made in the formal parameters x and y does not change the original value of a and b. In the function sum ( ), the formal parameters values are increased by 10 and thus x and y are 110 and 210 respectively. After the execution of function sum ( ), the control is transferred to the main ( ) function, the next statement prints the original value of a and b as 100, 200 respectively because the memory locations of a and b are not affected by the changes done to x and y. Since in this type of parameters passing , only the values are passed from the calling function , the technique is accordingly called as call by value.

## Call by reference

If it is required that the changes made to the formal parameters be reflected on the corresponding actual parameters then call by reference method of parameters passing is used. In this technique the addresses of the actual parameters is passed to the called function. Thus in call by reference method, the actual and formal parameters share memory locations. To understand the concept of call by reference , consider the following program.

## Program to explain passing parameters by refrence.

```#include <iostream>

using namespace std;

void sum (int* x, int* y);

int main()
{
int a,b;
a = 100;
b = 200;
cout <<" value before function call are  a ="<<a<<" b="<<b<<endl;
sum(&a,&b);
cout <<" value after function call are  a ="<<a<<" b="<<b<<endl;
return 1;
}
void sum (int* x, int* y)
{
*x= *x+10;
*y= *y+10 ;
cout<<" Value inside the function are  x ="<<*x<<" y="<<*y<<endl;
}
```

## Output

```Value before function  call are a = 100 b =200
Value inside the function are x = 110 y =210
Value after function call  are a = 110  b = 210
```

In the above example , the function sum () is called from the main ( ) function by passing the addresses of a and b as parameters. Since the address are passed , the same memory locations are referenced by the pointers x and y. x and y has the starting addresses of a and b respectively. The changes made in the locations which are pointed by x and y will change the original value of a and b. After the execution of function sum ( ) , the control is transferred to main ( ) function, the next statement prints the value of a and b as 110 and 210 respectively. Call by reference is preferred over call by value when

• Changes to passed parameters are expected.

• The overhead of copying large amount of data is to be eliminated.

• Fast processing is required. 