-
Hajipur, Bihar, 844101
In C++, a reference is an alias for another variable. It does not create a new copy of the variable; instead, it provides a different name to access the same memory location.
References are one of the most powerful features in C++ because they make it easier to work with functions, parameters, and memory-efficient programming.
You can think of a reference as a nickname for a variable. Once a reference is created, it cannot refer to another variable.
A reference variable acts as an alternative name for an existing variable.
It must be initialized at the time of declaration, and after that, it refers permanently to the same variable.
dataType &referenceName = variableName;
int a = 10;
int &b = a; // b is a reference to a
Here, both a and b point to the same memory location.
If you change a, b also reflects the change and vice versa.
#include <iostream>
using namespace std;
int main() {
int x = 20;
int &y = x;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
y = 50;
cout << "After changing y, x = " << x;
return 0;
}
Output:
x = 20
y = 20
After changing y, x = 50
Here, updating y automatically updates x because both refer to the same memory.
A reference must be initialized at the time of declaration.
A reference cannot be reassigned to another variable.
References cannot be NULL.
A reference must always refer to a valid variable.
References are used for:
Function arguments (to avoid copying large data)
Returning values from functions
Simplifying pointer syntax
Efficient memory usage
One of the most common uses of references is passing variables to functions by reference instead of by value.
When you pass a variable by value, a copy is made.
Changes inside the function do not affect the original variable.
void changeValue(int n) {
n = 100;
}
When you pass a variable by reference, no copy is made.
The function works directly with the original variable.
void changeValue(int &n) {
n = 100;
}
#include <iostream>
using namespace std;
void changeValue(int &num) {
num = 50;
}
int main() {
int x = 10;
changeValue(x);
cout << "x after function call = " << x;
return 0;
}
Output:
x after function call = 50
Here, the function directly modifies the original variable x.
You can pass more than one variable by reference.
Example:
#include <iostream>
using namespace std;
void swapNumbers(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 5, y = 10;
swapNumbers(x, y);
cout << "x = " << x << ", y = " << y;
return 0;
}
Output:
x = 10, y = 5
Here, the swapNumbers() function swaps values without returning anything, thanks to references.
You can also return a variable by reference from a function.
This allows the function to provide direct access to the actual variable rather than a copy.
Example:
#include <iostream>
using namespace std;
int& getValue(int &x) {
x = x * 2;
return x;
}
int main() {
int num = 10;
int &result = getValue(num);
cout << "Result = " << result;
return 0;
}
Output:
Result = 20
Here, the function modifies and returns a reference to the same variable.
Although references and pointers look similar, they are not the same.
| Feature | Reference | Pointer |
|---|---|---|
| Declaration | int &ref = var; |
int *ptr = &var; |
| Null value | Cannot be null | Can be null |
| Reassignment | Cannot be changed | Can be reassigned |
| Dereferencing | Not required | Must use * |
| Initialization | Mandatory | Optional |
int a = 5;
int *p = &a;
int &r = a;
cout << *p; // 5
cout << r; // 5
References are simpler to use when you just need an alias, while pointers are more flexible when dealing with dynamic memory.
If you want to prevent modification of a referenced variable, you can declare a const reference.
Example:
#include <iostream>
using namespace std;
void display(const int &num) {
cout << "Value: " << num;
}
int main() {
int x = 100;
display(x);
return 0;
}
Here, the const keyword ensures that the function cannot change the value of x.
C++ does not allow references to references directly.
However, when you assign one reference to another, both refer to the same variable.
Example:
int a = 10;
int &b = a;
int &c = b;
All three (a, b, and c) refer to the same memory location.
You can also use references with arrays to manipulate their elements directly.
Example:
#include <iostream>
using namespace std;
void modifyArray(int (&arr)[3]) {
for (int i = 0; i < 3; i++) {
arr[i] += 10;
}
}
int main() {
int numbers[3] = {1, 2, 3};
modifyArray(numbers);
for (int n : numbers)
cout << n << " ";
return 0;
}
Output:
11 12 13
Here, the array is modified directly without using pointers.
Forgetting to initialize the reference during declaration.
Trying to make a reference refer to another variable later (not allowed).
Returning references to local variables (dangerous – causes undefined behavior).
A reference in C++ is a simple but powerful feature that allows two variables to share the same memory.
It improves performance by avoiding unnecessary copies and simplifies function parameter passing.
Key takeaways:
A reference must be initialized when declared.
It always refers to the same variable.
Use pass by reference to modify original data.
const references help protect variables from changes.
References are heavily used in modern C++ programming — especially in function calls, classes, and operator overloading.
Write a program to demonstrate how a reference variable reflects changes made to the original variable.
Create a function swapValues() that swaps two integers using pass by reference instead of using return values.
Write a program to take two numbers as input and use a reference-based function to return their sum and product.
Implement a function updateMarks(int &marks) that adds 10 bonus marks to a student’s score using references.
Create a program where a function doubleValue(int &n) doubles the value of a variable through reference.
Write a function compare(int &a, int &b) that returns the larger number using return by reference.
Demonstrate the use of constant references by writing a function that takes a const int & parameter and displays its value without modifying it.
Write a function incrementArray(int (&arr)[5]) that takes an array by reference and increases all its elements by 5.
Build a program that uses references in a for loop to modify the elements of an array directly.
Create a program that declares multiple references (int &ref1 = x; int &ref2 = ref1;) and prints how all refer to the same variable.