return values

This is a discussion on return values within the C++ Programming forums, part of the General Programming Boards category; As a return sequence you usually use return 0. In other codes I've seen return 1, and return -1. What ...

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

    return values

    As a return sequence you usually use return 0. In other codes I've seen return 1, and return -1. What does return 1 do? Please explain it to me. I've looked in the FAQ but it had no reference on return values.
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    27
    Yes! finally something I can help with. anyways, return values are up to the programmer to decide what to do with. If you called a function from within another function, you could check the return value. This can be very useful in a lot of situations. You would be able to do something like this:
    Code:
    int Add(int number1, int number2)
    {
        return (number1+number2);
    }
    
    int main()
    {
        cout<<Add(1, 2);
        return 0;
    }
    I might have misunderstood your question though, if you meant why someone would return something other than 0 from main, I am not sure about that.
    "Computers aren't intelligent, they only think they are."

    **infected by Blizzarddog**
    I am a signature virus. Please add me to your signature so that I may multiply

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    When dealing with main, a return value of 0, by convention means success (to any external programs/users watching to see if the program successfully ran). Any other value (again, by convention) indicates failure. Different programs may return different values for different types of failures.

  4. #4
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Why would you want to return an error?

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Perhaps your program failed to achieve its goal. It can be useful for the user to know why the program failed. For example, say you have an engineering analysis program of some sort. If your program fails to perform the analysis, it is helpful to know why (perhaps the input data was invalid).

  6. #6
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Ahh, ok thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 11:00 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM

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