25. Namespace with Variables and 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;
}

 

Solution Explanation

  • Variables and functions inside MathOps are accessed with MathOps::.
  • First x=10 is printed.
  • Then square(10) is called and prints 100.

     

Layman’s Terms

The namespace is like a toolbox. You take out a variable (x) and a tool (function square) from it to use.

 

Significance for Embedded Developers

In embedded C++, namespaces are often used to group math helpers, driver utilities, or configuration sets without risking name collisions.

Loading...

Input

Expected Output

x=10 Square=100