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:
- The function
displayMessageis declared with thevoidkeyword, indicating it does not return a value. - The function does not take any parameters, so its parameter list is empty.
- The function body contains a statement to print
"This is a void function!"to the console. - In the
mainfunction, thedisplayMessagefunction 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:
- The function
addAndDisplaytakes two integer parameters,aandb. - The
voidkeyword specifies that the function does not return a value. - The function calculates the sum of
aandbusing the expression(a + b)and prints the result to the console. - In the
mainfunction, theaddAndDisplayfunction is called with the arguments5and10, and the sum15is 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:
- The
void*pointerptris used as a generic pointer that can store the address of any data type. - The second parameter
typespecifies the type of data thatptrpoints to. - The
switchstatement determines the data type and prints the value at the pointer’s address after performing astatic_cast. - In the
mainfunction:- The address of the integer variable
numis passed toprintValuewith the type'i'. - The address of the float variable
piis passed toprintValuewith the type'f'.
- The address of the integer variable
- The console outputs the values:
Integer: 42andFloat: 3.14.
Key Points about void Keyword
- The
voidkeyword indicates that a function does not return a value. - It is commonly used for functions that perform actions like printing or modifying global variables.
void*is used to create generic pointers that can point to any data type.- For functions that do not take parameters, the declaration can include
voidin the parameter list for clarity.
