return value

This is a discussion on return value within the C Programming forums, part of the General Programming Boards category; What does a return 0 or 1 means and where and to whom does it return it to?...

  1. #1
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    return value

    What does a return 0 or 1 means and where and to whom does it return it to?

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    'return 0' return's 0 and 'return 1' return's 1 to the calling function/process. The choice of 0 or 1 (or whatever) is purely with the user.
    Ex:
    Code:
    int fn2(void)
    {
         return(0);
    }
    
    void fn1(void)
    {
         printf(" %d\n", fn1());
    }
    In this example, a value of 0 is returned to the the calling function - fn1(), by the called function fn2().
    Similarly;
    Code:
    int  main(void)
    {
         return(1);
    }
    A value of 1 is returned the the parent of this process (usually the shell).

  3. #3
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    Usually a function or program returns a 0 on success and an error code on failure. The main deviation from this is when a function is supposed to return a pointer. When the function is not successful, it should return the NULL pointer (0).

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by shaik786
    Code:
    void fn1(void)
    {
         printf(" %d\n", fn1());
    }
    Small typo..., it must be:
    Code:
    printf(" %d\n", f2());

  5. #5
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    So if I return a value any value to a calling function in the main program what happen to that value...can it be used in the main program for other purposes

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >So if I return a value any value to a calling function in the main program what happen to that value...
    It kind of depends on the type of the value and how it was created or used in the function. But simply, you can use a return value in the calling function for whatever you need. Usually the return value is a flag indicating whether or not the function terminated successfully:
    Code:
    int func ( void )
    {
      /*
      ** Do stuff.
      */
      if ( stuff == GOOD )
        return 0;
      else
        return 1;
    }
    
    void calling_function ( void )
    {
      int success_flag;
      success_flag = func();
      if ( success_flag == 0 ) {
        /*
        ** All is well
        */
      }
      else {
        /*
        ** Handle the error.
        */
      }
    }
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    5

    Red face return -1

    And by the way, I wonder what the mean by return -1.

  8. #8
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    It's usually 'error'.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: return -1

    Originally posted by June
    And by the way, I wonder what the mean by return -1.
    It means whatever the original programmer intended it to mean. Yes, there are plenty of standard functions that return -1 on error, but that isn't always guaranteed to be the case. You need to read up on each function you use to understand what it will return under all conditions.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 01:39 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 12:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21