Jump to content
  • Advertisement
Sign in to follow this  
shurcool

func(void) vs. func() declarations

This topic is 3449 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying to figure out which of the following two I should be using for declaring functions (in .h file) that take no parameters: 1.
int func(void);
2.
int func();
I vaguely remember hearing somewhere that if you use the 2nd way, it's ambiguous because the function func can then be defined to take any number of parameters, including none. Meaning the empty parenthesis don't specify anything about the parameters, but rather make it generic. On the other hand, the 1st way is supposed to specify with precision that the func takes no params. However, it seems that even using the 2nd way, the VC++ 2008 Express compiler gives an error if you try to define func that was declared as func(); with some params. Meaning the two methods above are functionally equivalent. Is that so? My question: Is there any practical difference between the two approaches? Is it in any way beneficial to use func(void); declarations over func();? P.S. I'm talking about both class member and non-member function, if there is any difference between the two when it comes to this issue. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
In C++, there's no difference between the two declarations: both declare a function taking no arguments and returning an int. In C, the first declares a function taking no arguments and returning an int. The second declares a function taking an unknown number of arguments and returning an int.

Share this post


Link to post
Share on other sites
I see. That answers my question (in a very concise manner, if I may say so; unlike my own posts, probably including this sentences too, haha), thank you!

So in C++, it's just a matter of style/preference.

Share this post


Link to post
Share on other sites
Quote:
Original post by shurcool
So in C++, it's just a matter of style/preference.

Bjarne says that f(void) is an abomination. So don't make him sad ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Bjarne says that f(void) is an abomination. So don't make him sad ;)

That would be a misquote. He has said that Dennis Ritchie and Doug McIlroy have called it an abomination (both in the book Design and Evolution of C++ and in the article "Sibling Rivalry: C and C++") to him. However, I've found no credible source that he actually said it himself. Even the Parashift C++ FAQ Lite which claims that Stroustrup has said it, cites "Sibling Rivalry: C and C++" as the source, where he only quotes Ritchie and McIlroy.

In any case, I personally prefer the int func(void) declaration for the simple reason that it makes it easier to grep or otherwise search for the function declaration or definition.

Share this post


Link to post
Share on other sites
I prefer the int func() declaration for the reason of consistency:

How many items are in the list (int, int)? How many parameters does func(int, int) take?

How many items are in the list (int)? How many parameters does func(int) take?

How many items are in the list (void)? How many parameters does func(void) take?

How many items are in the list ()? How many parameters does func() take?

See the point? The (void) declaration is a unique special case in which you write a list with one element to prototype a list with zero elements. (It makes marginally more sense to use 'void' to specify a return value because you can only return at most one thing anyway; but here C++ doesn't offer a choice anyway, so I have to just grumble a little.)

I guess what I'm really getting at is that 'void' is a really weird pseudo-type (and then there's the little matter of void*...).

Share this post


Link to post
Share on other sites
I favour the int func() declaration; I think it looks nicer and it feels (nay, is) more modern. Other languages that I frequently use, Java, C# and Python, all have that syntax too so I don't waste any brain cycles when I chop and change between languages [smile].

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
(and then there's the little matter of void*...).

Yeah, consider int func(void) vs. int func(void*), they look quite similar.

Share this post


Link to post
Share on other sites
Quote:
Original post by shurcool
Quote:
Original post by Zahlman
(and then there's the little matter of void*...).

Yeah, consider int func(void) vs. int func(void*), they look quite similar.
Ah but so does int func(int) and int func(int*).
I think the real point Zahlman was making is that void* stands out as a weird pointer type in that it follows different rules to all the others.

Share this post


Link to post
Share on other sites
Well I'm going through Stroustroup's latest book and he seems to prefer using int func(); over int func(void); through most of the book I have read so far.
He also talks about void abit and says there are no objects of type void, but is used to mean "no value returned"
so for example:

void v; // error: there are no objects of type void
void f(); // f returns nothing - f() does not return an object of type void

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!