53. Safe Utility Function

#include <iostream>
using namespace std;

int add(int a, int b) noexcept {
    return a + b;
}

int main() {
    int x, y;
    cin >> x >> y;

    cout << "Result: " << add(x, y) << "\n";

    return 0;
}

Solution Details

  • The keyword noexcept guarantees that the function will not throw exceptions.
     
  • Since integer addition cannot throw, it is safe to declare this function noexcept.
     
  • Compilers may optimize better when they know a function cannot throw.

     

👉 In simple words:
 This is just a safe “calculator button” for addition. By marking it noexcept, we promise it will never fail.
 

Significance for Embedded Developers

  • Many helper functions in firmware (arithmetic, bit operations, register manipulations) should never throw exceptions.
  • Declaring them noexcept makes code more predictable and sometimes faster.
  • Ensures critical low-level operations remain exception-free.

     
Loading...

Input

5 7

Expected Output

Result: 12