Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ void function declarations [duplicate]

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 );
like image 915
lorenzoid Avatar asked Dec 13 '22 08:12

lorenzoid


2 Answers

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.

like image 108
wormsparty Avatar answered Jan 04 '23 03:01

wormsparty


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

like image 21
Alok Save Avatar answered Jan 04 '23 02:01

Alok Save