Void type: Difference between revisions
m r2.7.1) (robot Adding: vi:Void (kiểu dữ liệu) |
clarify per talk, some rewording |
||
Line 1: | Line 1: | ||
{{About|the void type in [[programming language]]s|types of astronomical voids|void (astronomy)|other uses|Void (disambiguation)}} |
{{About|the void type in [[programming language]]s|types of astronomical voids|void (astronomy)|other uses|Void (disambiguation)}} |
||
The '''void type''', in several [[Curly bracket programming language|programming languages derived from C and Algol68]], is the [[type theory|type]] for the result of a [[function (computer science)|function]] that does not return a |
The '''void type''', in several [[Curly bracket programming language|programming languages derived from C and Algol68]], is the [[type theory|type]] for the result of a [[function (computer science)|function]] that does not return a value to its caller. Usually such functions are called for their [[Side effect (computer science)|side effects]]. 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 programming language|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 <code>void *</code>), but this is an unrelated notion. Variables of this type are [[Pointer (computing)|pointer]]s to data of an ''unspecified'' type, so in this context (but not the others) void acts as a universal or [[top type]]. 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 [[polymorphism (computer science)|polymorphic]] functions (note that this is not particularly true for function pointers because functions are not data<ref>http://www.safercode.com/blog/2008/11/25/generic-function-pointers-in-c-and-void.html</ref>). |
C and [[C++]] also support the '''pointer to void type''' (specified as <code>void *</code>), but this is an unrelated notion. Variables of this type are [[Pointer (computing)|pointer]]s to data of an ''unspecified'' type, so in this context (but not the others) void acts as a universal or [[top type]]. 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 [[polymorphism (computer science)|polymorphic]] functions (note that this is not particularly true for function pointers because functions are not data<ref>http://www.safercode.com/blog/2008/11/25/generic-function-pointers-in-c-and-void.html</ref>). |
||
Line 7: | Line 7: | ||
== Void in C and C++ == |
== Void 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 appear as the sole [[function argument|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 |
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 appear as the sole [[function argument|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 <code>void</code>. |
||
In the earliest versions of C, functions with no specific result defaulted to a return type of <code>int</code> and functions with no arguments simply had empty argument lists. Pointers to untyped data were declared as integers or pointers to <code>char</code>. Some early C [[compiler]]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 [[Typecasting (programming)|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.<ref>http://cm.bell-labs.com/cm/cs/who/dmr/chist.html, "Standardisation."</ref> |
In the earliest versions of C, functions with no specific result defaulted to a return type of <code>int</code> and functions with no arguments simply had empty argument lists. Pointers to untyped data were declared as integers or pointers to <code>char</code>. Some early C [[compiler]]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 [[Typecasting (programming)|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.<ref>http://cm.bell-labs.com/cm/cs/who/dmr/chist.html, "Standardisation."</ref> |
Revision as of 19:24, 4 April 2011
The void type, in several programming languages derived from C and Algol68, is the type for the result of a function that does not return a value to its caller. Usually such functions are called for their side effects. 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 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 type. 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 particularly true for function pointers because functions are not data[1]).
Void 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 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 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, void and void pointers were part of the C language dialect supported by AT&T-derived compilers.[2]
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:[3]
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 C99.[4]
References
- ^ http://www.safercode.com/blog/2008/11/25/generic-function-pointers-in-c-and-void.html
- ^ http://cm.bell-labs.com/cm/cs/who/dmr/chist.html, "Standardisation."
- ^ Stroustrup, Bjarne (2009). Programming: Principles and Practice Using C++. Boston: Addison-Wesley. p. 996. ISBN 0321543726.
- ^ Bjarne Stroustrup, C and C++: Case Studies in Compatibility. Reconcilable differences? You decide, Dr. Dobb's, September 01, 2002; print version