Thursday, March 14, 2013

Program to implement Call by Reference (CBR)

Before understanding call by reference, you should have an idea of call by value.

When we passes reference of arguments to a function as actual argument and uses pointer in function definition as formal argument then it will be call by reference.

Pointer:  It is a type of variable which holds address/reference of another variable.

The terms call by value and call by reference are used together. The difference is that in call by value, a copy of actual arguments is passed whereas in call by reference, reference (address) of arguments is passed to the function.
As we know that reference is passed to the function so any changes made to formal arguments will directly affect the value of actual arguments.

Here is the program that will illustrate the concept of Call by Reference :

In this program we are passing address of "a" and "b" using address-of-operator (&). And two pointer variables "x" and "y" are used to hold these addresses.
Here values of "a" and "b" are interchanged after swapping.

Powered by Blogger.