Question

The void keyword is only used as a return type to indicate that a method does not return a value. But why can't void be used as a formal parameter in method definition to indicate that it does not accept any arguments, like:

void fun(void){
    ...
}
Was it helpful?

Solution

It's unnecessary. If a method does not have arguments, specifying an empty argument set () is sufficient.

OTHER TIPS

I believe in C having no arguments meant it ignored any arguments given e.g. you could write int main() or int main(int argc, char** argv) This meant when you wanted to make it clear you really meant no arguments you had to write (void)

In Java you have () to mean no arguments, so you don't need another way of saying this.

In Java, if you want to define a method with no parameters, you simply need to have method(). Void doesn't explicitly mean nothing, it means no return value.

int func(void) was a kludge introduced by Bjarne Stroustrup into C++ because int func() already meant any number of arguments in C. That problem doesn't arise in Java so introducing it would have been redundant.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top