Open In App

C++ Functions - Pass By Reference

Last Updated : 26 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, there are different ways to pass data (or variables) to a function, with two common methods being Passing by Value and Passing by Reference. Passing by Reference lets a function modify a variable directly, without creating a copy. The variable and parameter share the same memory location, so any changes to the parameter affect the original variable.

What is a Pass by Reference?

When a variable is passed as a reference to a function, the address of the variable is stored in a reference variable inside the function. Hence, the variable inside the function is an alias for the passed variable. Therefore, any operations performed on the variable inside the function will also be reflected in the calling function. 

  • This ability to reflect changes could return more than one value by a function. 
  • Also, a void function could technically return values using this method. 

The & (address of) operator denotes values passed by pass-by-reference in a function definition.

Syntax:

C++
int func(int& a, int& b){
    // function body
}

In the above statement, parameters of the function are reference variables to achieve pass by reference.

Example:

C++
#include <iostream>
using namespace std;

void func(int& x) {
  x--;
}

int main() {
  int a = 5;
  cout << a << endl;
  func(a);
  cout << a;
}

Output
5
4

In this program, the function func takes an integer reference as a parameter. When the function is called with a as the argument, it modifies the value of a directly by decrementing it using the statement x--. Since x is a reference to a, any changes made to x also affect a. Therefore, the value of a is decreased by 1, from 5 to 4. This demonstrates how a function can modify the value of a variable passed by reference.

Swap function using Pass-By-Reference

The swap function swaps the values of the two variables it receives as arguments. Below is the C++ program to swap the values of two variables using pass-by-reference.

C++
#include <bits/stdc++.h>
using namespace std;

// Swap function
void swap(int &a, int &b) {
    int temp;
    temp = b;
    b = a;
    a = temp;
}

int main() {
    int x = 3, y = 7;

    // Before swapping
    cout << "Before Swapping: " 
       << endl;
    cout << "x: " << x << " y: " 
       << y << endl;

    // Call the function
    swap(x, y);

    // After swapping
    cout << "After Swapping: " 
       << endl;
    cout << "x: " << x << " y: " 
       << y;
    return 0;
}

Output
Before Swapping: 
x: 3 y: 7
After Swapping: 
x: 7 y: 3

In this program, the swap function swaps the values of two variables by passing them as references. When the function is called, it modifies the values of a and b directly. When we print the value of x and y after calling swap function, x value is 7 and y value is 3.

Pass by Reference vs Pass by Value

The following table compares the key differences between Pass by Reference and Pass by value in C++:

Pass By Reference

Pass By Value

The actual reference (memory address) of the variable is passed.

A copy of the actual value is passed to the function.

The original variable is modified.

The original variable is not modified.

More memory efficient since no copy of the variable is made.

Requires more memory since a copy of the variable is created

Changes inside the function affect the original variable.

Changes inside the function do not affect the original variable.

Suitable when you want to modify the original data or avoid copying large data.

Suitable when you don’t want to alter the original data.


Next Article

Similar Reads