Return value, need more understanding, pls help

Can someone just explain to me about all the return value in c++?

return -1; indicates an error
return 0; indicates success, and exit from program
return 1; do this return just int(value) of 1?
return ; What is the default value when return nothing?

WHEN do I know when to use which return value?

THANKS for helping a newbie.

Comments

  • : Can someone just explain to me about all the return value in c++?
    :
    : return -1; indicates an error
    : return 0; indicates success, and exit from program
    : return 1; do this return just int(value) of 1?
    : return ; What is the default value when return nothing?
    :
    : WHEN do I know when to use which return value?
    :
    : THANKS for helping a newbie.

    Ok, well, that is one scheme. The trick that they don't tell you about the above definitions is this: INSIDE the program execution it only matters in the context of what meaning you give to a returned value as it affects the processing in the function(s) that it returns to. Traditionally, 0 value returned indicates success, any non-zero is an error. In special cases where a function returns a pointer to a data structure in memory, a NULL may indicate failure and should be checked for before using the returned pointer.
    The ONLY time that the return is really an issue, is when the program terminates. It should return zero as an int type or a specific error level documented for conditions that may exist affecting the operating system's capability to take control of the computer after execution of your program. In most cases, when in doubt, main returns a zero. I hope that this helps you out.



  • : : Can someone just explain to me about all the return value in c++?
    : :
    : : return -1; indicates an error
    : : return 0; indicates success, and exit from program
    : : return 1; do this return just int(value) of 1?
    : : return ; What is the default value when return nothing?
    : :
    : : WHEN do I know when to use which return value?
    : :
    : : THANKS for helping a newbie.
    :
    : Ok, well, that is one scheme. The trick that they don't tell you about the above definitions is this: INSIDE the program execution it only matters in the context of what meaning you give to a returned value as it affects the processing in the function(s) that it returns to. Traditionally, 0 value returned indicates success, any non-zero is an error. In special cases where a function returns a pointer to a data structure in memory, a NULL may indicate failure and should be checked for before using the returned pointer.
    : The ONLY time that the return is really an issue, is when the program terminates. It should return zero as an int type or a specific error level documented for conditions that may exist affecting the operating system's capability to take control of the computer after execution of your program. In most cases, when in doubt, main returns a zero. I hope that this helps you out.
    :
    :
    :
    :
    [blue]Follow Windows in the 'return value' paradigm. In windows return TRUE (TRUE = 1) means success and return FALSE (FALSE = 0) means failure. When you use the opposite, which you call 'traditional' - the code becomes quite unreadable. You have to use the explicit comparison with TRUE or FALSE in the boolean expressions.

    Code #1:
    [code]
    int open_file ()
    {
    if (all OK) {
    return 0; // all good
    }
    else {
    return -1; // errors
    }
    }

    // using that function in expression:
    if (open_file (...) == 0) {
    // file opened, proceeed with processing
    }
    [/code]

    Code #2:
    [code]
    int open_file ()
    {
    if (all OK) {
    return 1; // all good
    }
    else {
    return 0; // errors
    }
    }

    // using that function in expression:
    if (open_file (...)) {
    // file opened, proceeed with processing
    }
    [/code]
    In my opinion the code #2 is more readable, however it may be just the personal preference. Windows follows the #2 convention.[/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories