compile error: implicit return at end of non-void function (warning)... SOLVED
Hi,
I am using pacific c compiler and get that error when compiling. I also used DEVC++ compiler (which can also compile c programs ) and get the error of illegal operation of program.
Here is the code, pls help out, and tell me where i went wrong, thnx in advance.
Code:
/* Calculates and prints parking charges. */
#include <stdio.h>
float calculateCharges( float );
int main()
{
float timeParked1, timeParked2, timeParked3;
printf( "Car %2d - hours parked: ", 1 ); /* Car 1 */
scanf( "%f", timeParked1 );
printf( "Car %2d - hours parked: ", 2 ); /* Car 2 */
scanf( "%f", timeParked2 );
printf( "Car %2d - hours parked: ", 3 ); /* Car 3 */
scanf( "%f", timeParked3 );
printf( "%2s%6s%6s", "Car", "Hours", "Charge" );
printf( "\n%2d%6.2f%6.2f", 1, timeParked1, calculateCharges( timeParked1 ) );
printf( "\n%2d%6.2f%6.2f", 2, timeParked2, calculateCharges( timeParked2 ) );
printf( "\n%2d%6.2f%6.2f", 3, timeParked3, calculateCharges( timeParked3 ) );
return 0;
}
float calculateCharges( float parked )
{
float fee;
if ( parked <= 3 )
return 2.00;
if ( parked > 3 ) {
fee = ( parked - 3 ) * 0.50 + 2;
return fee;
}
if ( parked = 24 )
return 10.00;
}
Re: compile error: implicit return at end of non-void function (warning)...
Code:
float calculateCharges( float parked )
{
float fee;
if ( parked <= 3 )
return 2.00;
if ( parked > 3 ) {
fee = ( parked - 3 ) * 0.50 + 2;
return fee;
}
if ( parked = 24 )
return 10.00;
}
This is the function that's causing the error. What Parksie was saying was that although you have accounted for all the possible values of 'parked' and given an exit point for each of them (an exit point is a line after which the function returns), the compiler doesn't realise this when it's looking through your code looking for syntax errors. It might sound like that's a bad thing for the compiler to do but in fact it is pointing out an opportunity to make your code simpler and more efficient.
In fact, this code would do the exact same thing and is okay as far as your compiler is concerned:
Code:
float calculateCharges( float parked )
{
if ( parked <= 3 )
return 2.00;
return (( parked - 3 ) * 0.50 + 2);
}
If you have any trouble seeing why that's the same, just say so.