Swapping by Call by Reference or Pointer using C++

Call by Reference or Pointer

This is a simple programming tutorial for swapping values of the two variables by passing parameters to a function using call by reference or call by pointer method in C++. The reference variables are represented by an ampersand symbol (&) before the variable name, and pointer variables are represented by an asterisk symbol (*) before the variable name. In this tutorial, I have used Turbo C++ v3.0 and GNU GCC v7.1.1 software for compiling the C++ program.

The values of the two variables can be swapped by initially assigning the value of a variable to a temporary variable in a cyclic order. For instance, a = 1 and b = 2, then t = a, a = b, and b = t.

Swapping by 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 is 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 instance, (a = 1) → 0x7fffffffeb58.

Source Code - Turbo C++

// Swapping two values using reference variable
#include <iostream.h>
#include <conio.h>
void swap(int &, int &);
int main()
{
  clrscr();
  int x, y;
  cout<<"Enter the value of x and y: ";
  cin>>x;
  cin>>y;
  cout<<"\nBefore Swapping\n\nx = "<<x<<"\ny = "<<y<<"\n";
  swap(x, y);
  cout<<"\nAfter Swapping\n\nx = "<<x<<"\ny = "<<y<<"\n";
  getch();
}
void swap(int &a, int &b)
// &a and &b are reference variables
{
  int temp;
  temp = a;
  a = b;
  b = temp;
}

Source Code - GNU GCC

// Swapping two values using reference variable
#include <iostream>
using namespace std;
void swap(int &, int &);
int main()
{
  int x, y;
  cout<<"Enter the value of x and y: ";
  cin>>x;
  cin>>y;
  cout<<"\nBefore Swapping\n\nx = "<<x<<"\ny = "<<y<<"\n";
  swap(x, y);
  cout<<"\nAfter Swapping\n\nx = "<<x<<"\ny = "<<y<<"\n";
}
void swap(int &a, int &b)
// &a and &b are reference variables
{
  int temp;
  temp = a;
  a = b;
  b = temp;
}

Swapping by Call by Pointer

The call by pointer method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address 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 pointer, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments.

Source Code - Turbo C++

// Swapping two values using pointer variable
#include <iostream.h>
#include <conio.h>
void swap(int*, int*);
int main()
{
  clrscr();
  int x, y;
  cout<<"Enter the value of x and y: ";
  cin>>x;
  cin>>y;
  cout<<"\nBefore Swapping\n\nx = "<<x<<"\ny = "<<y<<"\n";
  swap(&x, &y);
  cout<<"\nAfter Swapping\n\nx = "<<x<<"\ny = "<<y<<"\n";
  getch();
}
void swap(int *a, int *b)
// *a and *b are pointer variables
{
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
}

Source Code - GNU GCC

// Swapping two values using pointer variable
#include <iostream>
using namespace std;
void swap(int*, int*);
int main()
{
  int x, y;
  cout<<"Enter the value of x and y: ";
  cin>>x;
  cin>>y;
  cout<<"\nBefore Swapping\n\nx = "<<x<<"\ny = "<<y<<"\n";
  swap(&x, &y);
  cout<<"\nAfter Swapping\n\nx = "<<x<<"\ny = "<<y<<"\n";
}
void swap(int *a, int *b)
// *a and *b are pointer variables
{
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
}

Output - GNU GCC

Temperature Conersion Output

Comments

Most Popular Posts

TNEB Bill Calculator

TNEB Bill Calculator (New)

Technical Questions