Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Void type

From Wikipedia, the free encyclopedia
Return type for functions that do not output values when called
This article is about the void type in computer programming languages. For the concept of a void pointer, seePointer (computer programming) § C and C++. For types of astronomical voids, seeVoid (astronomy). For other uses, seeVoid (disambiguation).

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.

In C and C++

[edit]

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]

CC++ 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 Haskell

[edit]
Further information:Empty type

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.

In Rust

[edit]

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().

See also

[edit]

References

[edit]
  1. ^Ritchie, Dennis M. (1993)."The Development of the C Language". Association for Computing Machinery, Inc. Archived fromthe original on 1998-02-20 – via author and Lucent Technologies Inc. 1996.
  2. ^Stroustrup, Bjarne (2009).Programming: Principles and Practice Using C++. Boston: Addison-Wesley. p. 996.ISBN 978-0-321-54372-1.
  3. ^Bjarne Stroustrup,C and C++: Case Studies in Compatibility. Reconcilable differences? You decide,Dr. Dobb's, September 01, 2002;print version
  4. ^"WG14-N2432: Remove support for function definitions with identifier lists proposal"(PDF).open-std.org. September 25, 2019.Archived(PDF) from the original on December 27, 2022.
  5. ^"WG14-N2841: No function declarators without prototypes".open-std.org. 2021-10-10.Archived from the original on November 12, 2022.
  6. ^"C and C++ Patterns - CS 61".cs61.seas.harvard.edu. September 2025.
  7. ^"void".Hackage. Retrieved2025-07-14.
Uninterpreted
Numeric
Reference
Text
Composite
Other
Related
topics
Retrieved from "https://en.wikipedia.org/w/index.php?title=Void_type&oldid=1332164462"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp