Results 1 to 11 of 11

Thread: compile error: implicit return at end of non-void function (warning)... SOLVED

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484

    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;
    }
    Last edited by iflash; Jan 2nd, 2002 at 11:57 PM.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    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
    Last edited by iflash; Jan 2nd, 2002 at 09:48 AM.

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827

    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.
    Harry.

    "From one thing, know ten thousand things."

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    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

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    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:

    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( "%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;
       
    }

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
    Code:
    float calculateCharges( float parked )
    {
       if ( parked <= 3 )
          return 2.00;
       else if ( parked == 24 )
          return 10.00;
       return ( parked - 3 ) * 0.50 + 2;
    }
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    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

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    There's two return statements - everything after the first one won't be executed.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    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

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    thnx a lot problem fixed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width