Parameter Passing References in C++ Functions

Parameter Passing References in C++ Functions: The parameter passing references in C++ can be passed into and out of the functions. There are two different types of parameters in which function accepts the parameters i.e. formal parameters and actual parameters.



  • Formal Parameters: If a function is ready to use the arguments and it must declare the variables that accept the value of the arguments. These variables are called “formal parameters” of the function.
  • Actual Parameters: The variable (or) expression corresponding to a formal parameter that appears in the function (or) method call known as “Actual Parameters“.

Modes: There are three different m0des. They are as follows:

  • IN: It passes information from the caller to caller.
  • OUT: The callee writes values in the caller.
  • IN/OUT: The caller tells the callee value

Parameter Passing Methods in C++

There are different types of parameter passing methods in C++. They are described as follows:

1) Call by Value (Pass by value)

2) Call by Address (Pass by address)

3) Call by Reference (Pass by reference)

let us know in detail regarding the passing methods.

1) Call by Value (Pass by Value) in C++

The call by value can be defined as the values of the actual arguments are passed to the formal arguments. And this operation is done in the formal arguments. Any change in the formal arguments does not effect the actual arguments because formal arguments are the copies of the actual arguments.

The changes made in the formal arguments are local to the block of the called function. If the control returns back then the changes will be erased. The main advantage of this call by value is the actual parameters are fully protected because their values are not changed when the control is returned to the calling function.

Example:

#include <iostream>
using namespace std;
 
// function declaration
void swap(int x, int y);
 
int main () {
   // local variable declaration:
   int a = 100;
   int b = 200;
 
   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;
 
   // calling a function to swap the values.
   swap(a, b);
 
   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;
 
   return 0;
}

Output:

Variables BEFORE Swapping
————————–
int a = 100 int b = 200

Variables AFTER Swapping
————————–
int X = 200 int Y = 100

2) Call By Address (Pass By Address) in C++

The call by address in C++ can be defined as instead of passing values, address of actual parameters are passed to functions by using pointers. The function operates on the address rather than values. Here, the formal arguments are pointers to actual arguments. So because of this when the values of formal arguments are changed, then the values of actual parameters also change.

Example:

#include <iostream>
using namespace std;

// function declaration
void swap(int *x, int *y);

int main () {
   // local variable declaration:
   int a = 100;
   int b = 200;
 
   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;

   /* calling a function to swap the values.
      * &a indicates pointer to a ie. address of variable a and 
      * &b indicates pointer to b ie. address of variable b.
   */
   swap(&a, &b);

   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;
 
   return 0;
}

Output:

Before swap, value of a: 100
Before swap, value of b: 200
After swap, value of a: 200
After swap, value of b: 100

3) Call By Reference (Pass by reference) in C++

The call by reference in C++ can be possible to pass arguments by value, address and reference. In C++ reference types are declared with ‘&’ operator is nearly identical but not same as pointers. They will declare the aliases for object variables and allows the programmer to pass arguments by reference to functions.

Example:

#include <iostream>
using namespace std;

// function declaration
void swap(int &x, int &y);

int main () {
   // local variable declaration:
   int a = 10;
   int b = 20;
 
   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;

   /* calling a function to swap the values using variable reference.*/
   swap(a, b);

   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;
 
   return 0;
}

Output:

Before swap, value of a: 10
Before swap, value of b: 20
After swap, value of a: 20
After swap, value of b: 10