im having a simple question about function types.
aren't function type chosen to reflect return type?
Functions aren't types. Functions do have a return value, sometimes. What is far more important in function design is what the function is supposed to do. Let's go to your code.
Code:
int bottles_of_beer ()
{
int bottle_amount = 99;
while (bottle_amount > 0)
{
cout<< bottle_amount << " bottles of beer on the wall, " << bottle_amount << " bottles of beer!\n";
cout<< "take one down, pass it around! " << --bottle_amount << " bottles of beer on the wall!\n\n";
}
}
int main()
{
return bottles_of_beer();
}
Your bottles_of_beer() function supplies the return variable of main(). This is actually probably the worst option because the return variable of main() means something.
If main() returns 0, everything is OK.
If main() returns any other number, it implies an error in program termination.
So it's actually quite restrictive what you're doing.
Now the other thing is, if you do return a value, you should plan on using it. A far more simple function like this:
Code:
double fahrenheit_to_c (double degree) {
return 5.0 * (degree - 32.0) / 9.0 ;
}
That has a useful return value, but sometimes there just isn't one. Other than exiting main(), there is no reason for bottles_of_beer to return anything. That is the fix I recommend.
But say that there was a reason you should return more than one thing, you actually have a lot of options available to you. Non-const references, if you know what those are, allow you to change a thing in a function pretty safely. You could also return a pair, or a tuple, if there were 2 or 3 things you needed to return, respectively. Better still is incorporating these separate values into a user-defined data type.