---
title: "Namespace with pass by reference program"
subject: "Object Oriented Programming"
chapter: 2
key: "semester_3/object_oriented_programming:namespace-pass-by-ref-program"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/namespace-pass-by-ref-program
site: BCT Engineering
---

# Namespace with pass by reference program

Object Oriented Programming · Chapter 2: Introduction to C++

- Asked in: 2070 Asar, 6 marks
- Video: [C++ pass by VALUE vs pass by REFERENCE? 📧](https://www.youtube.com/watch?v=ao0I1ViVV6U) — Bro Code
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/namespace-pass-by-ref-program
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-2

## Question

Write a program that uses pass by reference to change meter to centimeter using pass by reference along with the namespace.

## Short answer

The program defines namespace convert containing meterToCm(float &value) which multiplies value by 100, modifying the caller's variable via reference. In main(), input meters, call convert::meterToCm(length), output centimeters. E.g., 2.5 m becomes 250 cm.

## Answer

The namespace `convert` contains the function `meterToCm()`. It receives the value by reference and changes the original variable from meters to centimeters (1 m = 100 cm).

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

namespace convert {
    void meterToCm(float &value) {   // pass by reference
        value = value * 100;         // original variable is modified
    }
}

int main() {
    float length;
    cout << "Enter length in meters: ";
    cin >> length;
    convert::meterToCm(length);      // access using namespace name
    cout << "Length in centimeters = " << length << endl;
    return 0;
}
```

**Sample run**

```text
Enter length in meters: 2.5
Length in centimeters = 250
```

**Explanation:**
- `float &value` is a reference: it is another name for `length`, so no copy is made and the change is visible in `main()`.
- The function is reached by `convert::meterToCm`, using the scope resolution operator.

## Check yourself

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

1. What does float &value make value?
   A. A copy
   B. Another name (alias) for the caller's variable
   C. A pointer to float
   D. A constant
   Answer: B. It refers to length. (Key point: Reference parameter)

2. Why is length changed in main()?
   A. Function returns value
   B. Global variable
   C. Because no copy is made; the reference modifies the original
   D. Copy-back at end
   Answer: C. Pass by reference. (Key point: Effect)

3. How is the function called?
   A. meterToCm::convert(length);
   B. convert.meterToCm(length);
   C. convert->meterToCm(length);
   D. convert::meterToCm(length);
   Answer: D. Scope resolution. (Key point: Namespace access)

4. For 2.5 m?
   A. 250 cm
   B. 25 cm
   C. 0.025 cm
   D. 2.5 cm
   Answer: A. 2.5 x 100. (Key point: Result)

## Related in Object Oriented Programming

- [Program using static members](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astatic-program) — Object Oriented Programming, chapter 4
- [Relational operators comparing lengths](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoverload-relational-length) — Object Oriented Programming, chapter 5
- [mdistance and edistance addition via friend](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amdistance-edistance-friend) — Object Oriented Programming, chapter 4

## Related in other subjects

- [Pointers in pass by reference; 2D array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-passing-by-reference-2d) — Computer Programming, chapter 9
- [Uses of an all-pass filter](https://www.bctengineer.com/question.md?key=semester_5%2Ffilter_design%3Aallpass-filter-uses) — Filter Design, chapter 2
- [Call by value versus call by reference](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acall-by-value-vs-reference) — Computer Programming, chapter 6, asked 8×
- [CNC lathe part program with G and M codes](https://www.bctengineer.com/question.md?key=semester_4%2Fmanufacturing_and_production_processes%3Acnc-lathe-part-program-gcode) — Manufacturing and Production Processes, chapter 9
