Return test

This is a discussion on Return test within the C++ Programming forums, part of the General Programming Boards category; Whatever value i put in the if statement after return (0,1,-1) it comes back to main(). So why is everybody ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747

    Return test

    Whatever value i put in the if statement after return (0,1,-1) it comes back to
    main().
    So why is everybody putting all kind of values there?
    Is it a prank on noobs?
    Of course im not talking about values as a result of a calculation, that is different.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int If(void)
    {
        int a=2,b=2;
        if (a==b) {cout << "If...\n"  ; return 1;}
        else      {cout << "Else...\n"; return 0;}
    }
    int main()
    {
        If();
        cout << "Main...\n";
        return 0;
    }
    Compiler MSVC++ 2010 with Code::Blocks.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A function is supposed to return the value to the calling function. It's only return from main that exits the program (since there is no calling function above that).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    The main() return value indicates to calling envieronment whether program is terminated correctly, or something abnormal happened during the execution.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 10:06 AM
  3. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  4. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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