C++ void function declarations

cfunction-declarationvoid

Possible Duplicate:
C++ Why put void in params?

What's the difference between these two declarations and which is used more commonly?

void function1();

and

void function2( void );

Best Answer

There is no difference in C++, where it is well defined that it represents 0 parameters.

However it does make one in C. A function with (void) means with no parameter, whereas () means with any number of parameters.

From http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fparam_decl.htm

An empty argument list in a function definition indicates that a function that takes no arguments. An empty argument list in a function declaration indicates that a function may take any number or type of arguments. Thus,

int f()
{
    ...
}

indicates that function f takes no arguments. However,

int f();

simply indicates that the number and type of parameters is not known. To explicitly indicate that a function does not take any arguments, you should define the function with the keyword void.