C++ void Keyword

The void keyword in C++ serves multiple purposes. It is primarily used to define functions that do not return any value. Additionally, it can be used to declare pointers to unspecified types.

In this tutorial, we will covers all scenarios where the void keyword is used, along with syntax and examples.


Syntax

Function Returning void

</>
Copy
void function_name(parameter_list) {
    // Function body
}
void
A keyword used to specify that the function does not return any value.
function_name
The name of the function being defined or declared. It should follow C++ naming conventions.
parameter_list
The list of parameters that the function accepts. Each parameter consists of a type and a name.
Function body
The set of statements enclosed in curly braces {} that defines what the function does.

Using void* for Generic Pointers

</>
Copy
void* pointer_name;
void*
A pointer to an unspecified type. It is used to create generic pointers that can point to any data type.
pointer_name
The name of the pointer being declared. It is used to reference the memory address of any type of variable.

Examples

Example 1: Void Function with No Parameters

In this example, we use void keyword in a function that does not return a value.

</>
Copy
#include <iostream>
using namespace std;

void displayMessage() {
    cout << "This is a void function!" << endl;
}

int main() {
    displayMessage();
    return 0;
}

Output:

This is a void function!

Explanation:

  1. The function displayMessage is declared with the void keyword, indicating it does not return a value.
  2. The function does not take any parameters, so its parameter list is empty.
  3. The function body contains a statement to print "This is a void function!" to the console.
  4. In the main function, the displayMessage function is called, and the message is displayed on the console.

Example 2: Void Function with Parameters

In this example, we use void keyword in a function takes parameters but does not return any value.

</>
Copy
#include <iostream>
using namespace std;

void addAndDisplay(int a, int b) {
    cout << "Sum: " << (a + b) << endl;
}

int main() {
    addAndDisplay(5, 10);
    return 0;
}

Output:

Sum: 15

Explanation:

  1. The function addAndDisplay takes two integer parameters, a and b.
  2. The void keyword specifies that the function does not return a value.
  3. The function calculates the sum of a and b using the expression (a + b) and prints the result to the console.
  4. In the main function, the addAndDisplay function is called with the arguments 5 and 10, and the sum 15 is displayed on the console.

Example 3: Using void* for Generic Pointers

In this example, we use void keyword for a generic pointer to store the address of different types of data.

</>
Copy
#include <iostream>
using namespace std;

void printValue(void* ptr, char type) {
    switch (type) {
        case 'i':
            cout << "Integer: " << *(static_cast<int*>(ptr)) << endl;
            break;
        case 'f':
            cout << "Float: " << *(static_cast<float*>(ptr)) << endl;
            break;
    }
}

int main() {
    int num = 42;
    float pi = 3.14;
    
    printValue(&num, 'i');
    printValue(&pi, 'f');
    return 0;
}

Output:

Integer: 42
Float: 3.14

Explanation:

  1. The void* pointer ptr is used as a generic pointer that can store the address of any data type.
  2. The second parameter type specifies the type of data that ptr points to.
  3. The switch statement determines the data type and prints the value at the pointer’s address after performing a static_cast.
  4. In the main function:
    • The address of the integer variable num is passed to printValue with the type 'i'.
    • The address of the float variable pi is passed to printValue with the type 'f'.
  5. The console outputs the values: Integer: 42 and Float: 3.14.

Key Points about void Keyword

  1. The void keyword indicates that a function does not return a value.
  2. It is commonly used for functions that perform actions like printing or modifying global variables.
  3. void* is used to create generic pointers that can point to any data type.
  4. For functions that do not take parameters, the declaration can include void in the parameter list for clarity.