154. Lambda Square Operation

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;

    auto square = [](int x) { return x * x; };

    cout << "Square=" << square(n);
    return 0;
}

Explanation & Logic Summary:

  • The lambda expression [](int x) { return x * x; } is assigned to the variable square
  • It behaves like an inline function that:
    • Accepts one integer
    • Returns the square of that integer
  • The lambda is defined inside main(), keeping the logic local and concise
  • The input range ensures that x * x remains within the valid bounds of a 32-bit int

👉 In simple words:
This is a small, inline function created exactly where it is needed, avoiding unnecessary global functions.

Firmware Relevance & Real-World Context:

In firmware and embedded C++:

  • Lambdas are useful for small, localized logic
  • Common use cases include:
    • Scaling sensor or ADC values
    • Lightweight callbacks
    • Short event-handling logic
  • Keeping such logic inline improves:
    • Readability
    • Maintainability
    • Scope safety in constrained systems

This problem reinforces safe arithmetic, modern C++ usage, and embedded-friendly coding practices.
 

 

 


 

Loading...

Input

5

Expected Output

Square=25