40. Set Threshold

#include <iostream>
using namespace std;

void setThreshold(int x) {
    cout << "Threshold set to " << x;
}

void setThreshold(double x) {
    cout << "Threshold set to " << x;
}

int main() {
    double val;
    cin >> val;

    if (val == (int)val) {
        setThreshold((int)val); // integer overload
    } else {
        setThreshold(val);      // double overload
    }

    return 0;
}

 

Solution Details

  • Two functions with the same name setThreshold but different parameter types (int and double).
     
  • The compiler decides which function to use based on the type of argument.
     
  • main() handles whether the value is whole (int) or decimal (double).
     

👉 In simple words:
 You only write the two versions of setThreshold. The program chooses which one to call depending on whether the input has decimals.

Significance for Embedded Developers

  • Thresholds can be set in raw counts (e.g., ADC = 1023) or in real units (e.g., volts = 3.75).
     
  • Function overloading allows both cases to be handled naturally, without creating separate function names.
     
Loading...

Input

50

Expected Output

Threshold set to 50