A Guide to "Void" in Computer Programming

Void functions are stand-alone statements

Mixed race girl standing in parking lot under raining binary code
Donald Iain Smith / Getty Images

In computer programming, when void is used as a function return type, it indicates that the function does not return a value. When void appears in a pointer declaration, it specifies that the pointer is universal. When used in a function's parameter list, void indicates that the function takes no parameters. 

Void as a Function Return Type

Void functions, also called nonvalue-returning functions, are used just like value-returning functions except void return types do not return a value when the function is executed.

The void function accomplishes its task and then returns control to the caller. The void function call is a stand-alone statement. 

For example, a function that prints a message doesn't return a value. The code in C++ takes the form:

void printmessage ( )

{

  cout << "I'm a function that prints a message!";

}

int main ( )

{

  printmessage ( );

}

A void function uses a heading that names the function followed by a pair of parentheses. The name is preceded by the word "void," which is the type.

Void as a Function Parameter

The void can also appear in the parameter list part of the code to indicate the function takes no actual parameters. C++ can take the empty parentheses, but C requires the word "void" in this usage. In C, the code takes the form:

void printmessage (void )

{

  cout << "I'm a function that prints a message!";

Note that the parentheses that follow the function name are not optional in any case.

Void as a Pointer Declaration

The third use of void is a pointer declaration that equates to a pointer to something left unspecified, which is useful to programmers who write functions that store or pass pointers without using them. Eventually, it must be cast to another pointer before it is dereferenced.

A void pointer points to objects of any data type.