---
title: "Reference variable with example"
subject: "Object Oriented Programming"
chapter: 3
key: "semester_3/object_oriented_programming:reference-variable"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/reference-variable
site: BCT Engineering
---

# Reference variable with example

Object Oriented Programming · Chapter 3: C++ Language Constructs

- Asked in: 2072 Chaitra, 4 marks
- Video: [Reference Variables | C++ Tutorial](https://www.youtube.com/watch?v=e3DN1RaYVYQ) — Portfolio Courses
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/reference-variable
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-3

## Question

Explain the concept of reference variable with suitable example.

## Short answer

A reference variable is an alias for an existing variable, declared with & and must be initialized. It shares the same memory address and cannot be rebound. Example: int x=10; int &r=x; r+=5; changes x to 15. Used for call by reference and avoiding copies.

## Answer

A **reference variable** is an alias (another name) for an existing variable. It is declared with `&` and must be initialised when declared. After that, any operation on the reference works directly on the original variable.

#### Syntax

```cpp
data_type &ref_name = variable_name;
```

#### Example

```cpp
#include <iostream>
using namespace std;

int main()
{
    int x = 10;
    int &r = x;        // r is another name of x
    r = r + 5;         // changes x
    cout << x << " " << r << endl;       // 15 15
    cout << &x << " " << &r << endl;     // same address
    return 0;
}
```

#### Properties

- Must be initialised at declaration; `int &r;` is an error.
- Cannot be re-bound to another variable after initialisation.
- It has the same address as the variable; it does not take a separate copy.
- No null reference exists.

#### Uses

1. **Call by reference:** the function changes the caller's variables without using pointers.

```cpp
void swap(int &a, int &b)
{
    int t = a; a = b; b = t;
}
// swap(x, y) exchanges x and y in main
```

2. Avoids copying large objects (`const T &`).
3. Return by reference allows a function call on the left of `=`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. A reference is?
   A. A copy
   B. A new address
   C. An alias for an existing variable
   D. Null pointer
   Answer: C. Alias. (Key point: Definition)

2. int &r; is?
   A. Fine
   B. Null
   C. Valid
   D. Error; must be initialised
   Answer: D. Needs init. (Key point: Init)

3. Can reference be re-bound?
   A. No
   B. Yes
   C. Only const
   D. Only in function
   Answer: A. Fixed. (Key point: Rebinding)

4. &x vs &r?
   A. Different
   B. Same address
   C. r larger
   D. Undefined
   Answer: B. Alias. (Key point: Address)

## Related in other subjects

- [Variable oxidation states of 3d elements](https://www.bctengineer.com/question.md?key=semester_2%2Fengineering_chemistry%3Avariable-oxidation-3d) — Engineering Chemistry, chapter 5, asked 6×
