() why {}

I wondered why int main () uses these parenthesis, but then {} to begin and execute programme is this just code blocks or a c++ rule.

Like all functions in C++ the function main() requires a return type and parenthesis to hold the parameters, if any, and braces to denote the beginning and end of the function. And remember the function main() may have parameters.
1
2
3
4
5
int main(int argc, char** argv)
{

   return 0;
}


All C++ functions begin like this:
1
2
3
4
RETURN_TYPE   function_name ( INPUT_PARAMETER_LIST)
{
  // function code
}


So this function:

int main()

has a RETURN_TYPE of int, it is named main, and it has no input parameters.

Here is an example of a different function:
double beans_on_toast( int a, float b)
so this function has a RETURN_TYPE of double, it is named beans_on_toast, and it has two input parameters (one is an int, named a, and the second is a float, named b).

Time to learn about functions: http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.