C++ function call by reference

The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.

To pass the value by reference, argument reference are passed to the functions just like any other value. So accordingly you need to declare the function parameters as reference types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments.


For now, let us call the function swap() by passing values by reference as in the following example:

When the above code is put together in a file, compiled and executed, it produces following result:

Mohit Arora
Follow me