What is the difference between call by reference and call by the value mechanisms?

There are two basic methods to pass arguments in functions. They are:

1. Function arguments passed by value.
2. Function arguments passed by reference.

When arguments are passed by value, the called function creates a new variable of same type as the argument and copies the arguments value in it. The function cannot access the original variable in the calling program. It can access only the copy it created. Passing arguments by value is useful when the function need not modify the original variable in the calling program.

Passing arguments by reference uses a different mechanism. Instead of a value being passed to the function, a reference to the original variable in the calling program is passed. It is the memory address of the variable that is passed. The advantage of passing by reference is that the function can access the actual variables in the calling program. Using this method, more than one value can be passed to the function. For passing objects call by reference is used.

Post a Comment