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: }
This is a discussion on Return??? within the C++ Programming forums, part of the General Programming Boards category; I'm just leaning how to use C++ and I'm confused on return integers and there purpose. ex: Code: #include <iostream> ...
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: }
Use this as a template for all your programs, and don't worry about that for now:
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.Code:#include <iostream> using namespace std; int main() { return 0; }
Last edited by 7stud; 06-18-2005 at 12:43 PM.
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(); }
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.
Returning a value tells the computer what value that function found.
For example, if you have a function in code:
The integer "yup" would equal 1 before the program closed.Code:int main() { int yup = 0; yup = otherfunction(); return 0; } int otherfunction() { return 1; }
Last edited by gcn_zelda; 06-18-2005 at 06:15 PM. Reason: Forget a set of parentheses :-/
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)
since main( ) has been prototyped as void, it is not required return a value.Code:void main( ) { do stuff do more stuff doing more stuff here }
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 :* )
A board search will also give conclusive evidence on why one method is better than the other.Code:int main( ) { do stuff do more stuff doing more stuff here return 0; }
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
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.Code:return 0;
nextus, the samurai warrior
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:
and here is the main()Code:int someFunction(){ if (someArg < 10){ cout << "error" << endl; return 1; } else{ return 0; } }
does that help at all ?Code:someVar=someFunction(); if (someVar==1){ cout << "Error" << endl; } else{ cout << "yay it worked!" << endl; }
You can also try and recurse main() but I have a feeling that it is against the standard![]()
Returning a value of 1 or 0 is a good if you want to perform a boolean test:
Which will allow you to do stuff like this:Code:int someFunction(){ if (someArg < 10){ cout << "error" << endl; return 1; } else{ return 0; } }
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
The proper form should be.
Code:int void(int password, char** void) { int ON_EXIT = 0; return ON_EXIT; }
Code:#include <iostream> #include <stl> #include <math> using std::cout; using stl::string; int main void(int number_of_arguments, char ** arguments) { return 0; }
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
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).