To write the C++ program to swap two numbers, we need to use the concept of call by reference and call by address.

Basically in the call by reference, instead of passing the value of the variable, we pass its reference to another function.

This can be done in two ways

  1. Creating alias of the variable in another function (Can be called as call by Reference).
  2. By passing address or reference.

Swap two Numbers by Creating Alias

Swap two Numbers by Passing Address

In this method, we are using pointers because pointers can store the address of the variable and can be used in another function by dereferencing it.

Hope now you know, how to swap two numbers using reference and address in C++. If you have any doubt regarding the tutorial then comment below.

Leave a Reply