Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 In C and C++  





2 In Haskell  





3 References  














Void type






Čeština
Deutsch
فارسی
Français

Italiano

Polski
Русский
Slovenčina
Українська
Tiếng Vit

 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


The void type, in several programming languages derived from C and Algol68, is the return type 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 proceduresinPascal and syntactic constructs which define subroutinesinVisual Basic. It is also similar to the unit type used in functional programming languages and type theory. See Unit type#In programming languages for a comparison.

C and C++ 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 convert a pointer to any type of data (except a function pointer) 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++[edit]

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, 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 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,[citation needed] void and void pointers were part of the C language dialect supported by AT&T-derived compilers.[1]

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:[2]

C C++ equivalent
void f(void); void f(); (preferred)
void f(void);
void f(); (accepts a constant but unknown number of arguments) template <typename... Ts> void f(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 in C99.[3]InC23 (and C++), a function prototype with empty parentheses declares a function with zero parameters.[4][5]

In Haskell[edit]

Quite contrary to C++, in the functional programming language Haskell the void type denotes the empty type, which has no inhabitants [1]. 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[edit]

  1. ^ Ritchie, Dennis M. (1993). "The Development of the C Language". Association for Computing Machinery, Inc. via the author and Lucent Technologies Inc. 1996. Archived from the original on 1998-02-20.
  • ^ Stroustrup, Bjarne (2009). Programming: Principles and Practice Using C++. Boston: Addison-Wesley. p. 996. ISBN 978-0-321-54372-1.
  • ^ Bjarne Stroustrup, C and C++: Case Studies in Compatibility. Reconcilable differences? You decide, Dr. Dobb's, September 01, 2002; print version
  • ^ "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.
  • ^ "WG14-N2841 : No function declarators without prototypes". open-std.org. 2021-10-10. Archived from the original on November 12, 2022.

  • Retrieved from "https://en.wikipedia.org/w/index.php?title=Void_type&oldid=1194600165"

    Categories: 
    Data types
    Type theory
    Unknown content
    Hidden categories: 
    Articles with short description
    Short description is different from Wikidata
    All articles with unsourced statements
    Articles with unsourced statements from May 2021
     



    This page was last edited on 9 January 2024, at 20:40 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki