Thevoid type, in severalprogramming languages, more socurly bracket programming languages derived fromC andALGOL 68, is thereturn type of afunction thatreturns normally, but provides no result value to its caller. Usually such functions are called for theirside effects, such as performing some task or writing to their input parameters. The use of the voiddata type in such context is comparable toprocedures inPascal andsyntactic constructs which definesubroutines inVisual Basic. It is also similar to theunit type used infunctional programming languages and type theory. SeeUnit type#In programming languages for a comparison.
C andC++ also support thevoid pointer (orpointer to void type), denotedvoid*, but this is an unrelated notion. Variables of this type arepointers to data of anunspecified type, so in this context (but not the others)void* acts roughly like a universal orany type; it does not literally "point" to avoid in memory. A program can convert a pointer to any type of data (except afunction pointer) to a pointer to void and back to the original type without losing information, which makes these pointers useful forpolymorphic functions. The C language standard does not guarantee that the different pointer types have the same size or alignment.
A function withvoid result type ends either by reaching the end of the function or by executing areturn statement with no returned value. Thevoid type may also replace theargument list of afunction prototype to indicate that the function takes no arguments. Note that in all of these situations,void isnot a type qualifier on any value. Despite the name, this is semantically similar to an implicitunit type, not a zero orbottom type (which is sometimes confusingly called the "void type"). Unlike a real unit type which is a singleton, the void type lacks a way to represent its value and the language does not provide any way to declare an object or represent a value with typevoid.
In other C-derived languages, such asJava andC#,void exists similarly with the same use. While Java does not have explicit pointers, C# hasvoid* inside ofunsafe blocks.
In the earliest versions of C, functions with no specific result defaulted to a return type ofint and functions with no arguments simply had empty argument lists. Pointers to untyped data were declared as integers or pointers tochar. Some early Ccompilers 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 sometimescasts such function calls to void to suppress this warning. By the timeBjarne Stroustrup began his work on C++ in 1979–1980,[citation needed]void andvoid* were part of the C language dialect supported by AT&T-derived compilers.[1]
The explicit use ofvoid vs. giving no arguments in afunction prototype had different semantics in C and C++, as detailed in this table:[2]
| C | C++ equivalent |
|---|---|
voidf(void);voidf() (allowed since C23) | voidf(); (preferred)voidf(void); (allowed for backwards compatibility with C) |
voidf(); (until C23, accepts a constant but unknown number of arguments) | template<typename...Ts>voidf(Ts...ts){}(not strictly equivalent) |
The C syntax to declare a (non-variadic) function with an as-yet-unspecified number of parameters, e.g.void f() above, was deprecated inC99.[3] InC23 (and C++), a function prototype with empty parentheses declares a function with zero parameters.[4][5]
In C and C++, one can write a "void cast" to explicitly indicate to discard a value.[6]
voidf([[maybe_unused]]inta,intb){(void)a;returnb;}
In contrast to C++, in thefunctional programming languageHaskell, the void type denotes theempty type, which has no inhabitants.[7] A function into the void type does not return results, and a side-effectful program with type signatureIO Void does not terminate, or crashes. In particular, there are nototal functions into the void type.
In functional programming, theunit type would replace the use of the void type in C. This type has a unique result which provides no new information.
InRust'sforeign function interface, there exists astd::ffi::c_void type equivalent to C/C++void. There is no nativevoid in Rust, as it uses the unit type().