Thread: return statement

  1. #1
    vatos
    Guest

    return statement

    Hello..


    Sorry if this sounds a bit dumb but can somebody explain to me what the return statement means? I'm getting confused with all the diff types i see. I'm still new to this by the way.

    eg

    return 0;
    retrurn EXIT_SUCCESS;
    return EXIT_FAILURE;
    return 1;
    return ;

    oh...and also EXIT (0); ??

    thanx a lot.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Okay, well this is pretty simple. First off let me address the EXIT_SUCCESS/EXIT_FAILURE thing. In stdlib.h you will find macros that define these as 0 and 1, respectively. In other words:

    Code:
    return 0;
    
    // and
    
    return EXIT_SUCCESS;
    Mean the same thing. The exit() function is the standard c way of stopping execution of your program. A return itself is the value that a function returns.

    Example
    Code:
    int func1(void) {
      return 0;
    }
    
    float func2(void) {
      return 81.33;
    }
    
    char *func3(void) {
      return malloc(32);
    }

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>In stdlib.h you will find macros that define these as 0 and 1, respectively.
    Maybe, but you can't be sure that the literals behind the macros are 0 and 1 and relying on that means you lose portability.

    >>return malloc(32);
    And if malloc fails? :-)
    *Cela*

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Cela
    >>return malloc(32);
    And if malloc fails? :-)
    Then it simply returns what malloc returns on failure. NULL. Not a difficult concept.

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

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Not a difficult concept.
    Except that the most common use for a function that returns a block of memory is to assume it's valid and use it immediately. Even most of the calls to malloc I've seen assume everything is kosher. So it's better to protect obviously less than stellar programmers against themselves because they might be working next to you :-)
    *Cela*

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Except that the most common use for a function that returns a block of memory is to assume it's valid and use it immediately
    Not in my experience. That philosphy doesn't exist in any of the code I write/maintain.

    >>So it's better to protect obviously less than stellar programmers against themselves
    Which is why you should always use well written comments in your code. Returning NULL from a function because something failed is common practice, and is easy to understand, especially if it is documented at the top of the function.

    Using the malloc example, remember that if malloc fails in "your" function, it doesn't mean the caller wants to be restricted to your error handling.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Well, I've been suitably chided in this thread, my work here is done :-)
    *Cela*

  8. #8
    vatos
    Guest

    another one

    thanx guys...


    and what about this?

    return ( (b1 < b2) ? b1 : b2 );

  9. #9
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: another one

    Originally posted by vatos
    thanx guys...


    and what about this?

    return ( (b1 < b2) ? b1 : b2 );
    if b1 is less than b2 it returns b1 or else it returns b2.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM