Ray C
Ray C

Reputation: 617

void function parameter in both the definition and declaration? Or just one?

There are a ton of posts explaining why explicitly doing foo(void) is better than foo() because it specifies that there are no function arguments instead of an unknown amount.

However, I cannot seem to find if it's best practice to put void in both the header file declaration and the source file definition such as:

foo.h

void foo(void);

foo.c

void foo(void)
{
     return;
}

OR


foo.h

void foo(void);

foo.c

void foo()
{
     return;
}

Which one is better practice, and is there a specific reason to do one over the other?

Upvotes: 0

Views: 100

Answers (2)

Keith Thompson
Keith Thompson

Reputation: 263497

Old-style function declarations and definitions with empty parentheses are an obsolescent feature (N1570 6.11.6, 6.11.7). There are hardly any good reasons to use them.

Use (void) consistently, not ().

Upvotes: 4

M.M
M.M

Reputation: 141628

In your specific case it makes no difference, however it is a good habit to write:

void foo(void)
{

because there may come a time when you write a function body without also having a separate prototype beforehand, and this serves as a prototype whereas void foo() { does not.

Upvotes: 4

Related Questions