Thread: return

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    35

    return

    Here is the code:
    Code:
    #include <stdio.h>
    
    main ()
    {
        float fahr, celsius;
        int lower, upper, step;
    
    
        lower = 0;
        upper = 300;
        step = 20;
    
    
    
    
        fahr = lower;
        while (fahr <= upper) {
            printf ("%3.0f %6.1f\n", fahr, celsius(fahr));
            fahr = fahr + step;
        }
    
    
    }
        float celsius (float fahr)
        {
            return (5.0/9.0) * (fahr - 32.0);
        }
    Now can anyone explain to me why do i get an error.I mean it compiles,but it doesen't do what its meant for.This is an experiment program by the way.To check out the return function....
    Any help would be nice,thanks!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I get an error when I compile (after fixing your main() and returning 0)

    Code:
    gcc -Wall -pedantic -o f2c f2c.c
    f2c.c: In function ‘main’:
    f2c.c:18: error: called object ‘celsius’ is not a function
    You have an unused variable named celsius and a function named celsius. I'm guessing you added the variable because you were getting an error about celsius being undefined.

    If you're using functions, their prototype must be declared before you use them. A prototype looks like this

    Code:
    int myFunction(int myArgument1, char myArgument2);
    Then, after your main (like you've done), you define the function:

    Code:
    int myFunction(int myArgument1, char myArgument2)
    {
        printf("Arg1: %d, Argc2: %c\n", myArgument1, myArgument2);
    }

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    35
    Thanks alot ,il look into it more deeply.
    And your right ,about the celsius part.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A function with (visibility of) a variable named celcius cannot (directly) call a function named celcius(). That has nothing to do with a return statement, BTW.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    also for loop would be more suitable for this code
    Code:
        for(fahr = lower; fahr <= upper; fahr += step) 
        {
            printf ("%3.0f %6.1f\n", fahr, celsius(fahr));
        }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Fixing my error above:

    Code:
    int myFunction(int myArgument1, char myArgument2)
    {
        return printf("Arg1: %d, Argc2: %c\n", myArgument1, myArgument2);
    }
    Forgot the return from my own function! O_O

  7. #7
    Registered User
    Join Date
    Mar 2013
    Location
    /Earth/India/Pune
    Posts
    12
    Or if you don't want to declare the prototype of your function explicitly then just define your celsius function before the main function. Though I reckon declaring the custom functions before main is always a neat and good idea.

    Code:
    #include <stdio.h>
     
    float celsius (float fahr)
    {
        return (5.0/9.0) * (fahr - 32.0);
    }
    
    main ()
    {
        float fahr;
        int lower, upper, step;
             
        lower = 0;
        upper = 300;
        step = 20;
        fahr = lower;
        
        while (fahr <= upper) 
        {
            printf ("%3.0f %6.1f\n", fahr, celsius(fahr));
            fahr = fahr + step;
        }                                        
    }

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    35
    Not sure thats a good idea...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 07-04-2007, 12:20 AM
  2. Replies: 12
    Last Post: 04-07-2007, 11:11 AM
  3. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  4. Replies: 4
    Last Post: 07-15-2005, 04:10 PM
  5. Return Return Error
    By javacvb in forum C++ Programming
    Replies: 8
    Last Post: 12-16-2003, 04:17 PM