I have a function that takes the exact same args, but sometimes I’d like for it to return a double and other times I’d like for it to return an int. What’s the proper way to do that?
I could do function overloading, but the declarations of overloaded functions must differ from each other by the types and/or the number of arguments in the argument list. These would be identical so function overloading would not apply (I don’t think).
double calc( int value, int add, double mult )
{
// Sometimes I want this to return int. Sometimes double.
return (value + add) * mult;
}
I’d rather not cast to int when that’s the type I expect or write two functions (one for ints, the other for doubles). Thanks for any advice.
You can create a second function:
Or you can use a template:
You can’t create a different function with the same name and arguments; the compiler wouldn’t know which one you wanted to invoke.