4

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 );
Community
  • 1
  • 1
lorenzoid
  • 1,812
  • 10
  • 33
  • 51

6 Answers6

7

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.

rid
  • 61,078
  • 31
  • 152
  • 193
wormsparty
  • 2,481
  • 19
  • 31
6

There is no difference in C++.
The second declaration just explicitly says that function takes no parameter.

Second is more commonly used in C, First is the one that is more common in C++.

There is a difference in case of C because:
With (void), you're specifying that the function has no parameters, while with () you are specifying that the parameters are unspecified(unknown number of arguments).

However, if it was not a function declaration but a function definition, then even in C it is same as (void).

Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

There is no difference. I'd say the first one is more common, clear and concise.

rgargente
  • 1,821
  • 2
  • 19
  • 30
0

In C++, there is no difference, and the second form is only retained for C compatibility. The first form is preferred in C++.

In C, they mean different things. The first form specifies a function which takes an unknown number of arguments, and the second is a function taking zero arguments.

jalf
  • 243,077
  • 51
  • 345
  • 550
0

Some very old (non-standard) C compiler might complain about the first one, so the second one should be more portable.

Apart from that, there is no difference.

The first one is used more commonly in user code, quite simply because it's shorter.

Daniel Kiss
  • 311
  • 2
  • 6
0

actually there is no difference .if you have not any parameters to pass to the method user void or empty parentheses .notice that it just fro passed parameters.if your method has not any returned value you have to use void keyword.the first one is more common in C#

Ali Foroughi
  • 4,540
  • 7
  • 42
  • 66