Void type
Encyclopedia
The void type, in several programming languages derived from C and Algol68, is the type
for the result of a function that returns normally, but does not provide a result value to its caller. Usually such functions are called for their side effects
, such as performing some task or writing to their output parameters. The usage of the void type in such context is comparable to that of the syntactic constructs which define subroutines in Visual Basic
and procedures in Pascal. It is also similar to the unit type
used in functional programming languages and type theory; however, there are some differences in allowable usage, in that the void type is taken to be an empty type with no values. See Unit type#In programming languages for a comparison.
C and C++
also support the pointer to void type (specified as
. A program can convert a pointer to any type of data to a pointer to void and back to the original type without losing information, which makes these pointers useful for polymorphic functions (note that this is not necessarily true for function pointers because functions are not data).
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
In the earliest versions of C, functions with no specific result defaulted to a return type of
s 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:
A C prototype taking no arguments, e.g.
.
Type theory
In mathematics, logic and computer science, type theory is any of several formal systems that can serve as alternatives to naive set theory, or the study of such formalisms in general...
for the result of a function that returns normally, but does not provide a result value to its caller. Usually such functions are called for their side effects
Side effect (computer science)
In computer science, a function or expression is said to have a side effect if, in addition to returning a value, it also modifies some state or has an observable interaction with calling functions or the outside world...
, such as performing some task or writing to their output parameters. The usage of the void type in such context is comparable to that of the syntactic constructs which define subroutines in Visual Basic
Visual Basic
Visual Basic is the third-generation event-driven programming language and integrated development environment from Microsoft for its COM programming model...
and procedures in Pascal. It is also similar to the unit type
Unit type
In the area of mathematical logic, and computer science known as type theory, a unit type is a type that allows only one value . The carrier associated with a unit type can be any singleton set. There is an isomorphism between any two such sets, so it is customary to talk about the unit type and...
used in functional programming languages and type theory; however, there are some differences in allowable usage, in that the void type is taken to be an empty type with no values. See Unit type#In programming languages for a comparison.
C and C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
also support the pointer to void type (specified as
void *
), but this is an unrelated notion. Variables of this type are pointers to data of an unspecified type, so in this context (but not the others) void acts as a universal or top typeTop type
The top type in type theory, commonly abbreviated as top or by the down tack symbol , is the universal type—that type which contains every possible object in the type system of interest. The top type is sometimes called the universal supertype as all other types in any given type system are...
. A program can convert a pointer to any type of data to a pointer to void and back to the original type without losing information, which makes these pointers useful for polymorphic functions (note that this is not necessarily true for function pointers because functions are not data).
In C and C++
A function with void result type ends either by reaching the end of the function or by executing a return statementReturn statement
In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after where the subroutine was called, known as its return address. The return address is saved, usually on the process's call stack, as part of the operation...
with no returned value. The void type may also appear as the sole argument of a function prototype
Function prototype
A function prototype in C, Perl or C++ is a declaration of a function that omits the function body but does specify the function's return type, name, arity and argument types...
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
Unit type
In the area of mathematical logic, and computer science known as type theory, a unit type is a type that allows only one value . The carrier associated with a unit type can be any singleton set. There is an isomorphism between any two such sets, so it is customary to talk about the unit type and...
, not as a zero or bottom type
Bottom type
In type theory, a theory within mathematical logic, the bottom type is the type that has no values. It is also called the zero or empty type, and is sometimes denoted with falsum .A function whose return type is bottom cannot return any value...
, 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 compilerCompiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...
s 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
Bjarne Stroustrup
Bjarne Stroustrup ; born December 30, 1950 in Århus, Denmark) is a Danish computer scientist, most notable for the creation and the development of the widely used C++ programming language...
began his work on C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
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
Function prototype
A function prototype in C, Perl or C++ is a declaration of a function that omits the function body but does specify the function's return type, name, arity and argument types...
has different semantics in C and C++, as detailed in the following table:
C++ | C equivalent |
---|---|
void f; //preferred |
void f(void); |
void f(void); |
void f(void); |
void f(...); //accepts any arguments |
void f; /*accepts any arguments*/ |
A C prototype taking no arguments, e.g.
void f
above, has been deprecated however in C99C99
C99 is a modern dialect of the C programming language. It extends the previous version with new linguistic and library features, and helps implementations make better use of available computer hardware and compiler technology.-History:...
.