Click to See Complete Forum and Search --> : compile error: implicit return at end of non-void function (warning)... SOLVED
iflash
Jan 2nd, 2002, 03:44 AM
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.
/* 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;
}
parksie
Jan 2nd, 2002, 08:19 AM
It's your calculateCharges function. The compiler isn't quite smart enough to realise those are the only ways out of it.
Just put a return 0.0f at the end :)
PS: Your == 24 bit will never be called...
iflash
Jan 2nd, 2002, 08:31 AM
oops i forgot to use == instead of =. I'm still learning u know, readin a book. But i dun understand what you're telling me to correct, it's a bit brief. WHat are the ways, only out of what? And the 0.0f bit, where can i put it?
thnx
HarryW
Jan 2nd, 2002, 09:33 AM
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:
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.
iflash
Jan 2nd, 2002, 09:59 AM
your code even confuse me more, how can i test the if 'parked' is 10 or 24, and there is no 'if' statement to test the line:
return (( parked - 3 ) * 0.50 + 2);
so that line will execute no matter what 'parked' is, sorry but i can not see any sign of it being exact to my previous code. Hope you can help me more, or anybody else?
thnx a lot anyway
iflash
Jan 2nd, 2002, 10:21 AM
wait, i edited my code, fixed a few more little bugs, then there's no compilation error. But the result turned out weird. The Output screen shot is attached. And the edited code is below:
/* 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( "%s%6s%6s", "Car", "Hours", "Charge" );
printf( "\n%d%6f%6.2f", 1, timeParked1, calculateCharges( timeParked1 ) );
printf( "\n%d%6f%6.2f", 2, timeParked2, calculateCharges( timeParked2 ) );
printf( "\n%d%6f%6.2f", 3, timeParked3, calculateCharges( timeParked3 ) );
return 0;
}
float calculateCharges( float parked )
{
float fee;
if ( parked <= 3 )
return 2.00;
else if ( parked > 3 ) {
fee = ( parked - 3 ) * 0.50 + 2;
return fee;
}
else if ( parked == 24 )
return 10.00;
else
return 0;
}
kedaman
Jan 2nd, 2002, 10:29 AM
What Harry and Parksie meant is that the case where parked is 24 will return ( parked - 3 ) * 0.50 + 2 because parked > 3
my suggestion is to swap them
float calculateCharges( float parked )
{
if ( parked <= 3 )
return 2.00;
else if ( parked == 24 )
return 10.00;
return ( parked - 3 ) * 0.50 + 2;
}
iflash
Jan 2nd, 2002, 10:58 AM
what i mean is the last line is not with an if statement, so it'll execute whatever the value parked is. DId you see my output ? I have to fix that problem.
thnx
parksie
Jan 2nd, 2002, 11:31 AM
There's two return statements - everything after the first one won't be executed.
iflash
Jan 2nd, 2002, 10:40 PM
ok, i get what you mean, but i fixed my function bit in the code and compile successfully, then the output turned really weird. I've attached the screenshot again, hope you can help.
thnx a lot guys
iflash
Jan 2nd, 2002, 10:56 PM
thnx a lot problem fixed.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.