Hi, I really cant see what I have done wrong here. I have never used default arguments before and I am wondering what I am doing wrong in this program.
My C compiler is complaining that my function prototype is wrong. Here is the error:
ERR1: conflicting types for 'volume'
ERR2:"an argument type that has a default promotion can't match an empty parameter name list declaration"
To be fair I have never seen that error message before and I am confused by the meaning of it.
Here is the code in question:
float volume ( float height = 10.12, float width = 10.12, float length = 10.12 );
/*main function - begins program execution -----------------------------------*/
int main ( void )
printf("Default volume is %.2f", volume());
printf("\nAltered height rest default volume is: %.2f", volume ( 12.10 ));
printf("\nAltered height and width volume is: %.2f", volume ( 12.10, 14.20 ));
printf("\nAll default values altered volume is: %.2f", volume ( 12.10, 14.20, 16.11));
getchar(); /*freeze console output window*/
return 0; /*return value from int main*/
/*function to calulate volume of cube*/
float volume ( float h, float w, float l )
return h * w * l;
C allows default arguments? :)
Ah, perhaps that is why, is this one those little differences from C and C++? I know they CAN be used in C++, but I wonder if you are correct that C does not allow it?
It was a shock for me too.
Firstly, the code is C++. If your compiler is recognising default arguments, it is a C++ compiler.
Second, the reason for the error message is that volume() is a function that accepts three float arguments, and returns float. However, the default values you are supplying for those arguments (and, also, the values you pass when you use volume() within main()) are of type double. The easiest fix would be to replace all usages of the keyword "float" with "double" in your code.
C doesn't allows default arguments