Thread: Returning wrong value in a Function

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    4

    Returning wrong value in a Function

    Hello,
    I'm trying to return a value in a function, but the value return by the function is exceedingly high and random. Is there a way to zero out the function variable so that when the function is called it starts at zero.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Variable = 0;

    Actually, it sounds like it has nothing to do with initializing the variable. Try posting some code (in code tags of course )

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Enoz28
    Hello,
    I'm trying to return a value in a function, but the value return by the function is exceedingly high and random. Is there a way to zero out the function variable so that when the function is called it starts at zero.
    It sounds like you're doing this:
    Code:
    #include <stdio.h>
    int foo( void )
    {
        int x;
    
        return x;
    }
    
    int main( void )
    {
        printf("%d\n", foo( ) );
        return 0;
    }
    Since x is not initialized, it will store some arbitrary value, and as such, will give you the results you've described. There are a few ways to fix this:

    1) Make the variable static:
    Code:
    int foo( void )
    {
        static int x;
    
        return x;
    }
    Static variables are automaticly initialized to zero.

    2) Initialize the variable yourself.
    Code:
    int foo( void )
    {
        int x = 0
    
        return x;
    }
    Since you're initializing it manually, and since this function does nothing more, it will simply return what you initialize it to.

    3) Make your function meaningful, so it actually does something with the variable, rather than just return some otherwise meaningless value. Personally, I'd go this route.
    Code:
    int foo( void )
    {
        int x;
    
        /* actually use x here and do something with it */
    
        return x;
    }
    That being said, the #2 option is also highly suggested. You should always initialize your variables to something before using them. Always. Thus, the final version would be something akin to this:
    Code:
    int foo( void )
    {
        int x = 0;
    
        /* actually use x here and do something with it */
    
        return x;
    }
    Now then, you could have used static also, however, this assumes you really want the full benifit of using the static keyword, and aren't just only using it for the lazy-man's initialization. (Since it has more than that effect.)

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM