void (keyword)

from Wikipedia, the free encyclopedia

The keyword void ( English for void , invalid , empty ) is used in some programming languages instead of a data type to indicate that no data is passed or the type of data is not specified. Syntactically, it is voidtreated like a data type, but it can only appear in certain places. For example, it is not possible voidto declare a variable of type .

void as the return type

In the programming languages C , C ++ , D , Java and C # , voidfunctions or methods without a return value (so-called procedures ) are used. In these languages, a return type must be specified syntactically for each function or method. If no value is returned, is used instead void.

The following example defines a method with no return value in Java:

void hallo() {
   System.out.println("Hallo Welt!");
}

void in C and C ++

In C and C ++ voida basic data type is syntactically and semantically. It is the so-called "empty data type" (English empty type ) and a "incomplete type" . The latter ensures that no objects of the type voidcan be created.

void is used in these languages ​​for three additional tasks, except as a placeholder type for functions without a return value (see above):

Untyped pointers

With void*a is pointer declared without the type of data indicate that the pointer points (untyped pointer, pointer to void ). An untyped pointer cannot be dereferenced or used for pointer arithmetic, but must first be converted into a typed pointer using type conversion .

Any data pointer can be converted to a void pointer. When converting back to the original data type, the language standard requires that the same value be obtained. Conversion of the void pointer to a data type other than the original is only permitted for charpointers, otherwise the program is invalid ("undefined behavior"), and the compiler generally cannot recognize this.

Function arguments

Functions without arguments should be declared in C with the keyword voidinstead of an empty argument list, as in the following example:

void hallo(void) {
   printf("Hallo Welt!\n");
}

This special case is necessary because there are two different ways of declaring functions in C. In the original notation ( old-style ), the types of the arguments were not specified in the function header; the new notation was introduced so that the compiler can check the number and types of arguments when a function is called. In order to ensure compatibility with existing C code, declarations with the old notation were still allowed. So that function declarations without arguments are not mistakenly recognized as old-style , it was determined that in this case the keyword is used voidinstead of the argument list.

In C ++, however, old-style function declarations are not possible. Hence, voidan argument list is equivalent to an empty argument list.

Function return

void as return type of a function means that this function does not return any value.

Example:

void addiere(int s1, int s2, double *summe) {
     *summe = s1 + s2;
     return;  /* formal erlaubt aber ohne jegliche Wirkung, d.h. kann entfallen */
}

Individual evidence

  1. ^ Bjarne Stroustrup: The C ++ Programming Language, Special Edition. Addison-Wesley, Reading MA 2000, ISBN 978-0-201-70073-2 , pp. 816-817.
  2. ^ Margaret A. Ellis, Bjarne Stroustrup: The Annotated C ++ Reference Manual. Addison-Wesley, Reading MA 1990, ISBN 978-0-201-51459-9 , p. 138.