34. Namespace Variables Functions

#include <iostream>
using namespace std;

namespace MathOps {
    int x = 10;
    int square(int n) {
        return n * n;
    }
}

int main() {
    cout << "x=" << MathOps::x << "\n";
    cout << "Square=" << MathOps::square(MathOps::x) << "\n";
    return 0;
}

Explanation & Logic Summary:
Variables and functions defined inside a namespace are accessed using the scope resolution operator ::.

  • First, the value of x is printed.
  • Then, the function square(10) is called and the result is printed.

Layman’s Terms
A namespace is like a labeled box. To use something inside it, you must mention the box name first.

Firmware Relevance & Real-World Context:
In embedded C++, namespaces are commonly used to group drivers, math utilities, or configuration parameters while preventing name collisions across modules.

 

 

 

 

 

 

Loading...

Input

Expected Output

x=10 Square=100