Void C ++ Function Declarations

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

What is the difference between these two ads and which is used more often?

void function1(); 

and

 void function2( void ); 
+4
source share
6 answers

In C ++ there is no difference where it is clearly defined that it represents 0 parameters.

However, he does one thing in C. A function with (void) means without a parameter, while () means 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 the function definition indicates that the function does not accept arguments. An empty argument list in a function declaration indicates that the function can accept any number or type of argument. In this way,

 int f() { ... } 

indicates that the function f takes no arguments. Nonetheless,

 int f(); 

just indicates that the number and type of parameters are unknown. To explicitly indicate that a function does not accept any arguments, you must define a function with the void keyword.

+7
source

There is no difference in C++ .
The second declaration explicitly states that the function does not accept any parameter.

The second is more commonly used in C The first is one that is more common in C++ .

In case C there is a difference because:
Using (void) you indicate that the function has no parameters, and with () you indicate that the parameters are unspecified (unknown number of arguments).

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

+6
source

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

+1
source

There is no difference in C ++, and the second form is saved only for C compatibility. The first form is preferred in C ++.

In C, they mean different things. The first form defines a function that takes an unknown number of arguments, and the second is a function that takes null arguments.

0
source

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

In addition, there is no difference.

The first is used more often in user code, simply because it is shorter.

0
source

there really is no difference. If you do not have any parameters to pass the user void method or empty parentheses. Do not pay attention to the fact that they simply passed parameters. If your method does not have a return value, you need to use void keyword.the first one is more common in C #

0
source

All Articles