I have a couple of questions about the main function. Do you need to put the ; after the last }? Do you need to put a return statement before the last }? If so, what number do you put in the (), i.e. return (0), return (1), etc. What does the number mean?

Here are a couple of simple programs that all compile and run without errors in Dev C++ 4.9.9.0. Which one is the correct or the proper way to create a main function? And why?

NO RETURN, NO ;

#include<iostream>

using namespace std;

int main()
{
	cout<<"test"<<endl;
	system("pause");
}

NO RETURN

#include<iostream>

using namespace std;

int main()
{
	cout<<"test"<<endl;
	system("pause");
};

NO ;

#include<iostream>

using namespace std;

int main()
{
	cout<<"test"<<endl;
	system("pause");
	return(0);
}

RETURN AND ;

#include<iostream>

using namespace std;

int main()
{
	cout<<"test"<<endl;
	system("pause");
	return(0);
};

Thank You! :confused:

You dont need the semicolon after the last '}'. As for the return type, the compiler assumes that if you return a type of int and not explicit value is given then it is 0. Therefore, it is good practice to always include "return 0;".

>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.

>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.

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

cin.sync();  // purge enter, optional as needed
cin.get();   // console wait

will replace system("pause") and make prospective users happy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.