Thread: what is the mess about return 0

  1. #1
    just me
    Guest

    what is the mess about return 0

    to whom it may concern and thanks in advance,
    what is the deal with the "return 0" statement.
    It kind of has me mixed up. and things get mixed up in my mind, like "if it is true it will return a zero number, if it is false it will return a nonzero number."(I may have it messed up)
    John

  2. #2
    Bios Raider biosninja's Avatar
    Join Date
    Jul 2002
    Location
    South Africa
    Posts
    765
    I'm not quite sure but here goes...

    If you use it in your main function like this :

    int main()
    {
    return 0;
    }

    it will tell the comiler/program that the program ended normally without any abnormalities.

  3. #3
    just me
    Guest
    I kind of have things messed up, like if you are testing whether something is a true statement or not you get number returns, and if it is zero it means it is true or some rule like that. i don't know what they are talking about.
    thanks though, I kinda get that part, it really isn't hard to get it, just to organize it in my mind with others stuff.

  4. #4
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    You may be wondering about a line like this one:
    You can have a variable, usually an integer and have a conditional where instead of a testing statement like if(var>5)
    the statement is if(var), which will test positive if there is ANY value of the variable(var). Since zero isn't always considered a value, it will be negative if the value is zero.

    It's a little long, but I hope it helps you.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what is the deal with the "return 0" statement.
    >It kind of has me mixed up. and things get mixed up in my mind,
    >like "if it is true it will return a zero number, if it is false it will return a nonzero number
    You are confusing boolean values with the return value of a function. A return value is arbitrary, if you write the function you determine what values mean what, if someone else has written the function then they decide. Boolean values are the result of boolean tests, a == b for example. For boolean values, zero is false and non-zero is true.

    -Prelude
    My best code is written with the delete key.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They're probably thinking in terms of 'strcmp' and the like, where a common test is:

    if( !strcmp( "dog", "dog ) ) ...match...

    This is common. They (the authors of many library functions) use this format so that all of their functions return the same value for "true".

    The reason they use for 'strcmp' is because strcmp returns three type of things:

    1 or -1 depending if the string comes before or after the other string.
    0 for perfect match

    So here, there cannot be just two values for strcmp, but there are only two values for truth checks.

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

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    int IsAlpha( char c )
     {
      if(  (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')  )
       {
         return 1;
       }
      else
       {
         return 0;
       } 
     }
    
    
    int AreEqual( int a, int b )
     {
       if( a > b)
        {
          return 1;
        }
       else
       if( a < b)
        {
          return -1;
        }
       else
        {
          return 0;
        } 
     }
    thus, in the above two examples, we see that true is interpreted differently for each function, because:


    if( IsAlpha('d') )

    is a true statement while

    if( AreEqual( 10, 10) )

    would evaluate to false!

    so to use the second properly, you would do:

    if( AreEqual(10, 10) == 0)

    to get the correct evaluation.

    To be sure, until you are completely familiar with the language, avoid boolean statements altogether, and instead always use value comparisons like:


    if( IsAlpha('d') == 1)
    and
    if( AreEqual(10, 10) == 0)
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM