39. Print Sensor Value

#include <iostream>
using namespace std;

void printValue(int x) {
    cout << "Value=" << x;
}

void printValue(float x) {
    cout << "Value=" << x;
}

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

    if (val == (int)val) {
        printValue((int)val);
    } else {
        printValue(val);
    }

    return 0;
}

Solution Details

  • The two printValue functions have the same name but different parameter types.
     
  • The compiler automatically decides which one to call.
     
  • main() is already handling the logic of choosing between integer and float.
     

👉 In simple words:
 You only write the two functions. The program will figure out which one to use.

Significance for Embedded Developers

  • This mirrors how APIs for sensors may need to work with both raw integer counts (from ADC) and converted floating values (volts, °C).
     
  • Function overloading provides a clean way to handle both without writing different function names.
     
Loading...

Input

42

Expected Output

Value=42