
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a Reference Variable in C++
In C++, the reference variable is an alternate name of already existing variable. It cannot be changed to refer another variable and should be initialized at the time of declaration and cannot be NULL. The operator '&' is used to declare reference variable.
Reference variables are commonly used for parameter passing in functions, returning values, and aliasing existing variables.
Syntax
Following is the syntax for the reference variable in c++:
datatype &refer_var = variable_name;
Here,
- datatype ? The datatype of variable like int, char, float etc.
- variable_name ? This is the name of variable given by user.
- refer_var ? The name of reference variable.
Below are the examples that demonstrate how reference variables work in C++.
Basic Reference Variable
A reference is declared using the '&' symbol. It acts as another name for the original variable.
Syntax
The following is the syntax of reference variable.
datatype variable_name; // variable declaration datatype& refer_var = variable_name; // reference variable
Example
In the example, we can analyse that a variable of integer type is declared and initialized with a value. So, we can see that the variable b is declared which is referring variable a.
#include<iostream> using namespace std; int main() { int a = 8; int& b = a; b = 20; cout << "The variable a : " << a; cout << "\nThe reference variable r : " << b; return 0; }
Output
Following is the output to the above program:
The variable a : 20 The reference variable r : 20
Function Parameter Using Reference
Passing arguments by reference allows functions to modify the original variable.
Syntax
Following is the syntax for Function Parameter Using Reference.
void func(int &refVar);
Example
In this example, we initialize a number and add 5 to it using a function, and that displays the updated result.
#include<iostream> using namespace std; void update(int &n) { n += 5; } int main() { int num = 10; update(num); cout << "Updated number: " << num << endl; return 0; }
Output
Following is the output to the above program:
Updated number: 15
Return by Reference
We can return a reference from a function to allow chaining or direct modification.
Syntax
Following is the syntax for return by reference.
int& func();
Example
This program modifies the global variable 'x' to 50 by using a function that returns its reference.
#include<iostream> using namespace std; int x = 10; int& getX() { return x; } int main() { getX() = 50; cout << "x = " << x << endl; return 0; }
Output
Following is the output to the above program:
x = 50
Constant Reference
We can use constant to prevent modifications through the reference.
Syntax
Following is the syntax for a Constant Reference
const int & ref = var;
Example
In the following example, we pass a number by constant reference to a function, which display it without modifying the original value.
#include<iostream> using namespace std; void show(const int &num) { cout << "Number: " << num << endl; } int main() { int value = 100; show(value); return 0; }
Output
Following is the output to the above program:
Number: 100
Note: The Reference variables need to be initialized at the same time of declaration, but later on cannot be changed to reference another variable.