Differentiate between call by value and call by reference in c?

call by value: If we call a function in C by passing values of variables as the parameters/arguments to the function then such type of function call is known as Call by value.
call by reference: We know that whatever variable we are using in our program, ultimately it is getting stored somewhere in memory. So instead of passing values of variables as parameters to the function, if we pass address of those variables and somehow able to access data contained inside those addresses then we will be able to call functions.
 
Call by Value: when we use call by value in any function then we pass the value of variables to functions then we perform any action on that value. Means, In call by value function create a copy of that value then perform block of statements so the original value of the variable is remain unchanged.
Call by Reference: when we use call by reference then we pass the reference of that variable instead of passing value and then perform action on that. Means, In call by reference we pass the exact memory location of that variable and whatever the action we performed that is on location of that variable so the original value is being changed.
 
If we pass the value of the variable to function then that value is considered for the operation to be performed, this is called as the Call by Value.
On the other side in Call by reference we need to pass address or memory location of that variable to the function.
 
Call-by-value evaluation is the most common evaluation strategy, used in languages as different as C and Scheme. In call-by-value, the argument expression is evaluated, and the resulting value is bound to the corresponding variable in the function (frequently by copying the value into a new memory region). If the function or procedure is able to assign values to its parameters, only its local copy is assigned — that is, anything passed into a function call is unchanged in the caller's scope when the function returns.
 
Back
Top