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

Started by
6 comments, last by TravisWells 22 years, 1 month ago
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
Advertisement
nop
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..."
Actually its no in either case.
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.
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)
from what i can remember, in C putting function() would be like putting function(...) in c++, it would not check the parameters
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement