When I first started learning C++ the guy who wrote the book I was read went on and on how he liked to put void in a function that took no arguments. He talked about how the editor told him that was the C way and he should drop it. So my question was why is that the C way. Well I got my answer today. I had learn some time ago the old C way of writing a function was like this
fun(arg1, arg2)
int arg1, float arg2;
{
...
return 1;
}
and the prototype would of been
int fun();
Now this was bad because the compile could not use the prototype to check the arguments. So it was changed to the way we all know and love
int fun(int arg1, float arg2)
{
...
return 1;
}
and prototype
int fun(int arg1, float arg2);
Now where does void come in. Well according "The C Programming Language" if you did a prototype for a function that takes not arguments like
int fun();
The compile will think this is the old style and stop checking the arguments so that is why they add the void.
int fun(void);
Now the compiler will keep checking the arguments. So I don't know if todays C compilers still do this or not. I know that gcc will still compile the old style of C so I am guessing it must. For C++ which will not like the old style of functions this will not matter. So it seem to keep the void in C++ is a bad idea because the problem that caused it to be used in C (being backwards compatible) is no longer a problem. So that is C/C++ story of the day.
1 comment:
Ahh, so that is why. Neat!
Maybe I should read that book. Ya know, since I have a copy of it and all.
Post a Comment