HOME

TheInfoList



OR:

The void type, in several programming languages derived from C and Algol68, is the
return type In computer programming, the return type (or result type) defines and constrains the data type of the value returned from a subroutine or method. In many programming languages (especially statically-typed programming languages such as C, C+ ...
of a function that
returns Return may refer to: In business, economics, and finance * Return on investment (ROI), the financial gain after an expense. * Rate of return, the financial term for the profit or loss derived from an investment * Tax return, a blank document or t ...
normally, but does not provide a result value to its caller. Usually such functions are called for their
side effects In medicine, a side effect is an effect, whether therapeutic or adverse, that is secondary to the one intended; although the term is predominantly employed to describe adverse effects, it can also apply to beneficial, but unintended, consequenc ...
, such as performing some task or writing to their output parameters. The usage of the void type in such context is comparable to procedures in
Pascal Pascal, Pascal's or PASCAL may refer to: People and fictional characters * Pascal (given name), including a list of people with the name * Pascal (surname), including a list of people and fictional characters with the name ** Blaise Pascal, Frenc ...
and syntactic constructs which define
subroutines In computer programming, a function or subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed. Functions may ...
in
Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to: * Visual Basic .NET (now simply referred to as "Visual Basic"), the current version of Visual Basic launched in 2002 which runs on .NET * Visual Basic ( ...
. It is also similar to the
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 (and thus can hold no information). The carrier (underlying set) associated with a unit type can be any singleton set. ...
used in functional programming languages and type theory. See Unit type#In programming languages for a comparison. C and
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
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 roughly like a universal or top type. A program can probably convert a pointer to any type of data (except a
function pointer A function pointer, also called a subroutine pointer or procedure pointer, is a pointer that points to a function. As opposed to referencing a data value, a function pointer points to executable code within memory. Dereferencing the function poi ...
) to a pointer to void and back to the original type without losing information, which makes these pointers useful for polymorphic functions. The C language standard does not guarantee that the different pointer types have the same size or alignment.


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 replace the argument list of a function prototype to indicate that the function takes no arguments. Note that in all of these situations, void is ''not'' a type qualifier on any value. Despite the name, this is semantically similar to an implicit
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 (and thus can hold no information). The carrier (underlying set) associated with a unit type can be any singleton set. ...
, not a zero or bottom 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 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
compiler In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs tha ...
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 (; ; born 30 December 1950) is a Danish computer scientist, most notable for the invention and development of the C++ programming language. As of July 2022, Stroustrup is a professor of Computer Science at Columbia University ...
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. void f() above, has been deprecated in C99,Bjarne Stroustrup,
C and C++: Case Studies in Compatibility. Reconcilable differences? You decide
', Dr. Dobb's, September 01, 2002
print version
/ref> however.


In Haskell

Quite contrary to C++, in the
functional programming language In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that ...
Haskell the void type denotes the empty type, which has no inhabitant

A function into the void type does not return results, and a side-effectful program with type signature IO Void does not terminate, or crashes. In particular, there are no total functions into the void type.


References

{{Data types Data types Type theory