Archived

This topic is now archived and is closed to further replies.

Question about () and (void) in C/C++

This topic is 5751 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

I've seen functions with no arguments declared like this:
    
int get_current_frame();
  
and like this:
      
int get_current_frame(void);
  
Is there a difference? Edited by - TravisWells on March 17, 2002 7:03:45 PM

Share this post


Link to post
Share on other sites
I think that in C the "void" was required, but I''m not sure. In C++ they would be the same. That''s the only difference.

--------------------
Matthew Calabrese
Realtime 3D Orchestra:
Programmer, Composer,
and 3D Artist/Animator
"I can see the music..."

Share this post


Link to post
Share on other sites
That actually depends on your compiler. Typically, if you leave the arguments blank, the compiler defaults it to be void. It''s better practice to put void in there just to tell the complier explicitly.

I remember if you leave out the return type on a function, some compilers assume it returns int, others assume it is void. So it''s just a better idea to tell the compiler exactly what you want.

Share this post


Link to post
Share on other sites
quote:
Original post by LyLFox
That actually depends on your compiler. Typically, if you leave the arguments blank, the compiler defaults it to be void. It''s better practice to put void in there just to tell the complier explicitly.

I remember if you leave out the return type on a function, some compilers assume it returns int, others assume it is void. So it''s just a better idea to tell the compiler exactly what you want.


The C++ standards says that you need the FULL prototype of a function to be correct.

The compilers that do not spit out an error when you forget to put the return type of function is not standard compliant.

The standard also says that () means (void)

Share this post


Link to post
Share on other sites
quote:
Original post by barazor
from what i can remember, in C putting function() would be like putting function(...) in c++, it would not check the parameters


() and (void) were not used in the same place.

In pre-ANSI C, you used what is now called ''old-style'' function definitions. A function would be defined as

double foo(a,b)
int a;
int b;
{
... code ...
};


or, if it didn''t have any parameter

double foo(void)
{
... code ...
}


If you didn''t provide a prototype visible from the call site, the compiler would assume the function took and returned an int. But if you did provide a prototype, it had to be of the form double foo();, without a parameter list : parameters would not be checked, you could call foo(12, 23, 50) or foo();

Since the C calling convention (what Microsoft calls ''cdecl'', as opposed to their ''standard'' calling convention) is that the caller cleans up the stack, it worked, even if the function itself might not behave properly.

I assume the ellipsis came when compilers began to check for arguments, so that code written previously which took advantage of the lack of checking, using techniques similar to va_start/va_end could still compile.

Share this post


Link to post
Share on other sites