I'm currently refactoring/tidying up some old C code used in a C++ project, and regularly see functions such as:
int f(void)
which I would tend to write as:
int f()
Is there any reason not to replace (void) with () throughout the codebase in order to improve consistency, or is there a subtle difference between the two that I am unaware of?
More specifically, if a virtual member function in C++ is described as:
virtual int f(void)
and a derived class includes a member function:
int f()
is this a valid override? Additionally, am I likely to encounter any linker problems based on almost identical signatures?
If you omit the void , you have to search for function() and will therefore also find all function calls, making it more difficult to find the actual implementation. Show activity on this post. In C++, there is no difference in main() and main(void) . But in C, main() will be called with any number of parameters.
Functions may be return type functions and non-return type functions. The non-return type functions do not return any value to the calling function; the type of such functions is void.
Void functions are created and used just like value-returning functions except they do not return a value after the function executes. In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does not return a value.
void foo() means "a function foo taking an unspecified number of arguments of unspecified type" void foo(void) means "a function foo taking no arguments"
In C, the declaration int f(void)
means a function returning int that takes no parameters. The declaration int f()
means a function returning int that takes any number of parameters. Thus, if you have a function that takes no parameters in C, the former is the correct prototype.
In C++, I believe int f(void)
is deprecated, and int f()
is preferred, as it specifically means a function that takes no parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With