There are two ways of passing arguments/parameters to a function. One method is Call by Value and the other method is Call by Reference.
Call by Value: in the Call by Value the programming is sending the copy of an argument/parameter to a function. Programmer is not passing the actual variable. Since only the copy of the variable is passed to the function therefore any changes/modifications made within a function don't affect the variable and the variable value remains the same after the function call.
The following example will clarify it;
Suppose a = 2 before the function call
Void function (int a)
{
a = a + a;
a = a - 5;
}
Since only a copy of variable "a" is passed to the function that's why even after the function call value of a will remain 2.
Call by Reference: in the call by Reference the address of the argument/parameter is passed to the function. In simple words the actual variable or a pointer is passed to the function and variable value can be changed/modified within a function
For example; suppose a = 2 before function call
After passing this value to a function like
Void function(int &a)
{
*a = *a + *a;
*a = *a - 5;
}
Life is a Race
Thanks & Regards
By
Sabari Mahesh P M
Ramanagudlur, if this helps please login to Mark As Answer. | Alert Moderator