Call by Value
- The call by value method copies the values of actual parameters into formal parameters , that is the function creates its own copy of argument values and then uses them.
- The main benefit of call by value method is that you cannot alter the variables that are used to call the function . The original copy of the function remains intact.
following program illustrates the working of call by value method
int main()
{
int cube(int); //function declaration
int vol,side=7;
vol=cube(side); //function invoked
cout<<vol;
return 0;
}
int cube(int a) //function definition
{
return a*a*a;
}
Call by Reference
- The call by reference method uses a different mechanism. In place of passing a value to the function being called , a reference to the original variable is passed.
- In call by reference method , the called function does not create its own copy of original values , rather it refers to the original values only by different names (references).
- The call function works with the original data and any change in the values get reflected back to the original data.
following program illustrates the working of call by reference method.
int main()
{
int cube(int &); //function declaration
int vol,side=7;
vol=cube(side); //function invoked
cout<<vol;
return 0;
}
int cube(int &a) //function definition
{
return a*a*a;
}
ย