Thread: Return???

  1. #1
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67

    Question Return???

    I'm just leaning how to use C++ and I'm confused on return integers and there purpose.
    ex:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
     cout<<"HEY, you, I'm alive!  Oh, and Hello World!\n";
    cin.get();
    
    return 1; <~~~WHAT!?? :confused: 
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Use this as a template for all your programs, and don't worry about that for now:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
         
          return 0;
    }
    Returning 0 is conceptually no different than returning 1, 100, or - 50: they are all integers. Another program can call main() to begin execution of your program, and inside main() you can return different values as a signal to the other program. For instance, if there is some error you can return 1 to alert the other program.
    Last edited by 7stud; 06-18-2005 at 12:43 PM.

  3. #3
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    I have a question to add, for I have always been a bit confused about this as well.

    Wouldn't the other program also have a main() function. Can it distinguish between the two? Or is that not how you would call the other program?
    Code:
    void function(void)
     {
      function();
     }

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    think of a function how you return something to main or to the
    same function again, think of the main in one program as being a function to another program.

  5. #5
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    Returning a value tells the computer what value that function found.

    For example, if you have a function in code:

    Code:
    int main()
    {
      int yup = 0;
      yup = otherfunction();
      return 0;
    }
    
    int otherfunction()
    {
      return 1;
    }
    The integer "yup" would equal 1 before the program closed.
    Last edited by gcn_zelda; 06-18-2005 at 06:15 PM. Reason: Forget a set of parentheses :-/

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    you will see some who will prototype main( ) to have no return value.. but do not do this, for it is not c++ standard compliant (you will be shunned by the programming community and/or little kids will laugh at you)

    Code:
    void main( )
    {
    
        do stuff
        do more stuff
        doing more stuff here
    }
    since main( ) has been prototyped as void, it is not required return a value.




    Here is the c++ standard way. It almost looks beautiful.. main( ) has been prototyped to return an integer to the operating system upon program completion.. I think I just shed a tear of joy :* )

    Code:
    int main( )
    {
    
        do stuff
        do more stuff
        doing more stuff here
    
       return 0;
    
    }
    A board search will also give conclusive evidence on why one method is better than the other.
    Last edited by The Brain; 06-19-2005 at 04:30 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    Code:
    return 0;
    this ends the program and returns control to your operating system. it aslo return the value 0 to the operating system. other values can be returned to indicate different end conditions for the program and can be used by the operating system to determine if the program executed successfully. typically, a zero indicates a normal end to a program and any nonzero value indicates an abnormal end. however, whether or not a nonzero return value can be acted upon will depend on the operating system concerned.
    nextus, the samurai warrior

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    17
    look at it like this...

    persay you have an interger that should not be less than 10. here is an example of using return 1; to figure it out...
    you have a function defintion
    definition of someFunction:

    Code:
    int someFunction(){
    if (someArg < 10){
    cout << "error" << endl;
    return 1;
    }
    else{ return 0; }
    }
    and here is the main()

    Code:
    someVar=someFunction();
    if (someVar==1){
    cout << "Error" << endl;
    }
    else{
    cout << "yay it worked!" << endl;
    }
    does that help at all ?

  9. #9
    ---
    Join Date
    May 2004
    Posts
    1,379
    You can also try and recurse main() but I have a feeling that it is against the standard

  10. #10
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Returning a value of 1 or 0 is a good if you want to perform a boolean test:
    Code:
    int someFunction(){
    if (someArg < 10){
    cout << "error" << endl;
    return 1;
    }
    else{ return 0; }
    }
    Which will allow you to do stuff like this:

    Code:
    if(someFunction())
    {
         //This block will only be called if someFunction() returns true  (returns 1) 
    }
    
    else
    {
         //Some function must have returned false (returned 0) in order to access this block
    }
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    this is my preferd coding standard. Although you may include.

    The proper form should be.
    Code:
    int void(int password, char** void)
    {
       int ON_EXIT = 0;
       return ON_EXIT;
    }

  12. #12
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    7stud

    Code:
    #include <iostream>
    #include <stl>
    #include <math>
    
    using std::cout;
    using stl::string;
    
    int main void(int number_of_arguments, char ** arguments)
    {
       return 0;
    }

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Disregard the last two posts. Neither will compile.

    Note to the posters: it's not a good idea to make jokes in a newbie's thread without explicitely marking them. If they weren't jokes ... then perhaps you shouldn't post answers at all.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The return value from main() is usually returned to the operating system. It is possible to test that value in batch files (.BAT extension) under windows or MSDOS or with shell scripts under unix. Such scripts can run multiple programs and do different things depending on the return values from programs (eg if the first value in a chain fails, don't pass it's output to a second program).

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. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM