Void parameter
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."
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
-SirKnight
Edited by - SirKnight on February 1, 2002 4:31:57 PM
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.
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!
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!
Geordi
George D. Filiotis
George D. Filiotis
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.
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.
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.
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
.. and then call it
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
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
"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
Popular Topics
Advertisement