Thread: Return void or success/fail?

  1. #1
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    Return void or success/fail?

    What would be the proper return type for few samples of functions below? "void" for nothing or "int" to indicate succes/fail operation?

    Code:
    returnType makeEmpty()
    returnType insert( ItemType newItem )
    
    ...and other similar functions
    *All* books that I've read would have "void" as a return type, but the....
    some people suggests to return operation success state. I believe that's the return type for functions in library, e.g
    Code:
    while ( getline( ... ) ) { ... }
    So...?
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    well if you want them to return a value upon success or failure i would go for a bool type. I hope this is what you meant
    Woop?

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    It could be "bool" instead of "int"
    Basically the question is to return VOID or OPERATION_STATE

    Can someone point me to a book that will suggest to return OPERATION_STATE, 'cause like I said I haven't encountered one?
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Quote Originally Posted by alphaoide
    It could be "bool" instead of "int"
    Basically the question is to return VOID or OPERATION_STATE

    Can someone point me to a book that will suggest to return OPERATION_STATE, 'cause like I said I haven't encountered one?
    The operation state would be true if the function completed its task successfuly and false otherwise
    Code:
    bool someFunction(int arg)
    {
         return true;  // operation state is true indicating successful operation
    }
    or you could have a more verbose operation state by returning an int with differing values representing the outcomes of your function return 0 being success
    Last edited by curlious; 08-30-2004 at 04:32 PM.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>>Can someone point me to a book that will suggest to return OPERATION_STATE, 'cause like I said I haven't encountered one?
    >The operation state would be true if the function completed its task successfuly and false otherwise



    I can't say I've encountered a book that would suggest a return value (or none I can think of), but then those are mostly beginner books that are just trying to demonstrate how the function would accomplish the said task (i.e. show how to insert). But any book that details error handling extensively would probably do so.

    Why are you looking for a book anyway? Are you actually going to buy one for the sake of it suggesting you to do something that you would already have done anyway, or is this just to prove a point, or are you thinking that a book that would make the suggestion would be a cut above the rest?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    I don't know of a book either....

    But, I did read an article (somewhere) about exceptions. The bottom line was to use exceptions where something can go wrong which is beyond your control. (Not to find coding errors.)

    The same concept seems to apply here. If your function allocates memory, performs an I/O operation, depends on user input, etc., then it's good practice to check for success or failure.

    Where I work, we have a custom library with a bunch of I/O functions. Most (if not all) of the functions return zero if sucessful, otherwise an error code. If the functions need to return other values, pointers are used.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I tend to almost always return status values, but the problem is I'm always to lazy to check them when I call the functions
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Finally I found one book that gives detailed explanation on this issue. For those who cares...
    McConnell, Steve. Code Complete. 2nd Ed. 2004: Microsoft Press.
    When to Use a Function and When to Use a Procedure
    Purist argue that a function should return only one value, just as a mathematical function does. This means that a function would take only input parameters and return its only value through the function itself. The function would always be named fo the value it returned, as sin(), CustomerID(), and ScreenHeight() are. A procedure, on the other hand could take input, modify, and output parameters-as many of each as it wanted to.

    A common programming practice is to have a function that operates as a procedure and returns a status value. Logically, it works as a procedure, but because it returns a value, it's officially a function. For example, you might have a routine called FormatOutput() used with a report object in statements like this one:
    Code:
    if ( report.FormatOutput( formattedReport ) = Succes ) then ...
    In this example, report.formatOutput() operates as a procedure in that it has an output parameter, formattedReport, but it is technically a function because the routine itself returns a value. Is this a valid way to use a function? In defense of this approach, you could maintain that the function return value has nothing to do with the main purpose of the routine, formatting output, or with the routine name, report.FormatOutput(). In that sense it operates more as a procedure does even if it is technically a function. The use of the return value to indicate the success or failure of the procedure is not confusing if the technique is used consistenly.

    The alternative is to create a procedure that has a status variable as an explicit paramater, which promotes code like this fragment:
    Code:
    report.FormatOutput( formattedReport, outputStatus )
    if ( outputStatus = Success ) then ...
    I prefer the second style style of coding, not because I'm hard-nosed about the difference between functions and procedures but because it makes a clear separation etween the routine call and the test of the status value. To combine the call and the test into one line of code increases the density of the statement and, correspondingly, its complexity. The following use of a function is fine too:
    Code:
    outputStatus = report.FormatOutput( formattedReport )
    if ( outputStatus = Success ) then ...
    In short, use a function if the primary purpose of the routine is to return the value indicated by the function name. Otherwise, use a procedure.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  9. #9
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Hmm. The book also has a typo in it:

    Code:
    if ( report.FormatOutput( formattedReport ) = Succes ) then ...
    I wonder how many people have noticed this.

    Hehe... when it comes to error handling, you could try to catch the error with try...catch blocks.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I did read something about whether or not to use exceptions to handle common errors. If I remeber correctly, the bottom line was no, although it could also have been depends. But the argument goes, exceptions should only be used when something truly exceptional happens. Actually, I don't think I've ever used exceptions yet.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by Hunter2
    I did read something about whether or not to use exceptions to handle common errors. If I remeber correctly, the bottom line was no, although it could also have been depends. But the argument goes, exceptions should only be used when something truly exceptional happens. Actually, I don't think I've ever used exceptions yet.
    Actually, I'm reading it right now
    Throw an exception only for conditions that are truly exceptional
    Exceptions should be reserved for conditions that are truly exceptional-in other words, for conditions that cannot be addressed by other conding paractices. Exceptions are used in similar circumstances to assertions-for events that are not just infrequent but for events that should never occur.
    McConnell, Steve. Code Complete. 2nd Ed. 2004: Microsoft Press.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Odd, actually I think I read it in Sam's Teach Yourself C(or ++) in 21 Days I guess it's just a popular maxim.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    Related to your discussion (I think)

    I have been told that it is better to use
    Code:
    int
    as the return type on
    Code:
    main(){}
    than using
    Code:
    void
    . I still use
    Code:
    void
    but haven't programmed anything really fantastic. I would like to know how important this is if it is true so that I will remember to do it should a fantastic idea arise.

    www.edwardtisdale.com

  14. #14
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    edwardtisdale:
    The main function is different, because it is expected to follow certain rules. If you want your program to be standards-conforming, then you always make main return an int. VC++ and other older compilers allow void main even though it is non-standard.

    In general, you would return void unless the function has a natural return value. The main function is a special case because it must have one of only a couple of potential declarations.

    ----------

    In my opinion on the original post, I would return void unless it is perfectly acceptable or allowable for your function to fail. For example, a makeEmpty() shouldn't really ever fail, so that function should return void and use exceptions for any possibly exceptional circumstances.

    On the other hand, insert might fail under normal circumstances depending on what it is meant to do, so if a failed insert was normaly acceptable, then I would return a bool giving that state. An example is the map and multimap of the STL. Since insertion into the map might fail under normal circumstances (the key already exists), they included a bool as part of the return value to indicate success or failure. In a multimap, inserts should always work because duplicate keys are allowed, so no bool is returned and successful insertion is assumed.

    I'm sure there are gray areas that can be hard to determine if failure is normal or exceptional, but that is the basic idea.

  15. #15
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    dang, jlou, your two cents is worth a million to me.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM