C++ Functions - Pass By Reference
Last Updated :
26 May, 2025
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;
}
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;
}
OutputBefore 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. |
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are
10 min read