>Do you need to put the ; after the last }?
No.
>Do you need to put a return statement before the last }?
No, but it's a good idea.
>If so, what number do you put in the (), i.e. return (0), return (1), etc.
Use 0, EXIT_SUCCESS, or EXIT_FAILURE to be portable. Use whatever you like when you can answer the next question.
>What does the number mean?
It is the value returned to the operating system. This may be useful to other programs. For example, a batch file under Windows runs your executable which returns EXIT_FAILURE; after doing so, ERRORLEVEL is checked to see whether the batch file should continue.
>Which one is the correct or the proper way to create a main function?
I'd pick this one:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
cout<<"test"<<endl;
system("pause");
return 0;
}
>And why?
The semicolon is just a null statement. You could just as well litter your code with other useless declarations.
#include <iostream>
using namespace std;
;
;;
int main()
{
cout<<"test"<<endl;
system("pause");
return 0;
};
;
;
Returning 0 means normal program termination. The parentheses are not necessary.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>Do you need to put the ; after the last }?
No.
>Do you need to put a return statement before the last }?
In C, yes. In pre-standard C++, yes. In standard C++, no, 0 will be returned automagically. Whether you choose to or not for standard C++ is a matter of style and consistency. My style is to not explicitly return a value unless I return for failure elsewhere:
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello, world!"<<endl;
// No return
}
#include <cstdlib> // For EXIT_SUCCESS and EXIT_FAILURE
#include <iostream>
using namespace std;
int main()
{
if (some failure condition) {
cerr<<"Error"<<endl;
return EXIT_FAILURE;
}
cout<<"Hello, world!"<<endl;
return EXIT_SUCCESS; // Return success for symmetry, 0 works too
}
>what number do you put in the (), i.e. return (0), return (1), etc
return is not a function, so you don't need to use parentheses if you don't want to. Any integer is a valid return value, but the only portable values are 0 and two macros defined in cstdlib, EXIT_FAILURE and EXIT_SUCCESS.
>What does the number mean?
It's an exit code. If your program returns 0 then that tells any calling process that it terminated successfully. Any other value is treated as unsuccessful termination and the actual value gives more detail. For example, 1 could mean that a file failed to open, 2 could mean that invalid user input was read, etc...
>Which one is the correct or the proper way to create a main function? And why?
They're all correct. A lone semicolon typically does nothing and is harmless, and you can omit a return statement if you want.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
The iostream header that comes with the Dev-C++ package does take care of ye system() function (through seemingly endless internal includes). Be aware that this is not true for other C++ compilers. They may require the cstdlib header. Also system("pause") is strictly a DOS call and is not portable to other systems.
Something like
[php]
cin.sync(); // purge enter, optional as needed
cin.get(); // console wait
[/php]
will replace system("pause") and make prospective users happy.
vegaseat
DaniWeb's Hypocrite
5,986 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417