Void Type - in C and C++

In C and C++

A function with void result type ends either by reaching the end of the function or by executing a return statement with no returned value. The void type may also appear as the sole argument of a function prototype to indicate that the function takes no arguments. Note that despite the name, in all of these situations, the void type serves as a unit type, not as a zero or bottom type, even though unlike a real unit type which is a singleton, the void type is said to comprise an empty set of values, and the language does not provide any way to declare an object or represent a value with type void.

In the earliest versions of C, functions with no specific result defaulted to a return type of int and functions with no arguments simply had empty argument lists. Pointers to untyped data were declared as integers or pointers to char. Some early C compilers had the feature, now seen as an annoyance, of generating a warning on any function call that did not use the function's returned value. Old code sometimes casts such function calls to void to suppress this warning. By the time Bjarne Stroustrup began his work on C++ in 1979–1980, void and void pointers were part of the C language dialect supported by AT&T-derived compilers.

The explicit use of void vs. giving no arguments in a function prototype has different semantics in C and C++, as detailed in the following table:

C C++ equivalent
void f(void); void f; //preferred
void f(void); void f(void);
void f(...); /*accepts a variable number of arguments*/ void f(...); //accepts a variable number of arguments
void f; /*accepts a constant but unknown number of arguments*/ // no equivalent

A C prototype taking no arguments, e.g. void f above, has been deprecated however in C99.

Read more about this topic:  Void Type