Two related calculations can share a function name when their parameter lists differ. This is function overloading. The compiler selects a matching function from the arguments in the call.
#include <iostream>
int triple(int value) {
return value * 3;
}
double triple(double value) {
return value * 3;
}
int main() {
std::cout << triple(4) << '\n';
std::cout << triple(1.5) << '\n';
return 0;
}
4 is an int, so the integer version is an exact match. 1.5 is a double, so the decimal version is an exact match. The results are 12 and 4.5. Each definition has its own body and parameter.
Overloads must differ in their parameter types, their number of parameters, or both. Changing only the return type is not enough: the call's arguments would not tell the compiler which version you intended. For now, use calls with clear exact type matches rather than relying on conversions between competing overloads.
Your program has two bodies named doubleAmount. Read the parameter type to tell them apart: the int body handles whole-number amounts, and the double body handles decimal amounts. Both currently return a temporary zero. Finish the integer body first and run the program. The Whole line should change while Decimal stays unfinished, because the call with 2.25 uses the other body.
This is different from a default argument. A default fills an omitted argument for a function; overloading selects among distinct function definitions. Both can improve an interface, but simple, unambiguous calls are easiest to understand.
int overload to return twice its parameter. Expect Whole: 14.double overload to return twice its parameter. Expect Decimal: 4.5.Edit main.cpp, press Run to inspect the Console, then Submit to check your work.