• Advertisement

Archived

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

Void parameter

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

Recently I keep seeing function prototypes in C++ with the parameter list of "(void)". Is this the same as an empty parameter list "()"? Also, can you use it in function definition as well? "I''''m not evil... I just use the power of good in evil ways."

Share this post


Link to post
Share on other sites
Advertisement
A function written like this void myfunc( void ) is the same as void myfunc( ). So yes putting the void in the function parameter list is the same as not having anything in there at all. I prefer to put void in the param list if my func doesnt take any params but its just a part of my style, its not like it serves a purpose. I think a long time ago in the early C days you had to put void in the paramlist if your function didnt take any params.

-SirKnight

Edited by - SirKnight on February 1, 2002 4:31:57 PM

Share this post


Link to post
Share on other sites
quote:
Original post by SirKnight
I think a long time ago in the early C days you had to put void in the paramlist if your function didnt take any params.

In C if you don''t put void as the parameter in a function prototype the function takes an unspecified number of arguments of unknown types. I''m not sure if this is still allowed though. It greatly hinders the compiler''s ability to check for valid function calls.

Share this post


Link to post
Share on other sites
Which (I think) is called overloading. It is still supported, the good ol'' printf() and it''s ilk all took an indefinite amount of parameters. It''s possible to tell a C++ compiler to link to an overloaded function without complaining, but I''ve forgotten how.

Unfortunately you can''t write an overloaded function in C++ and I''m relatively sure you can''t do it in C. You have to write asm code to take advantage of that level of functionality.

George D. Filiotis
Are you in support of the ban of Dihydrogen Monoxide? You should be!

Share this post


Link to post
Share on other sites
called "var args". variable number of arguments. it is allowed in "C". not allowed in "Pascal".

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
According to the K&R, a function without arguments must be declared as 'int foo(void)'. Declaring it as 'int foo()' has a completely different effect, that of disabling parameter checking.

This (should) let you write

    
int foo()
char a,b,c,d; /* old style parameter declaration */
{
return printf( "%d %d %d %d", a,b,c,d );
}


.. and then call it

  
int foo();
long v;
foo( v );


It's f ugly, but if you disable the warnings, and ensure that your stack push/pops are done properly, then it 'should' work.

Of course if your compiler isn't backward compatible with K&R or ANSI C, well, that won't help you.

On the other hand, C++ assumes that foo() means foo(void);

Edited by - Fruny on February 1, 2002 5:54:11 PM

Share this post


Link to post
Share on other sites

  • Advertisement