Home > Article > Backend Development > How to use void in c++
The void type in C is a special data type that means the function does not return any value. Its main uses include: declaring functions that do not return any value. As a function parameter, it means that the function does not require any input. As a pointer type, it means that the pointer does not point to any specific object.
The void type in C
What is the void type?
void is a special data type in C, which means that the function does not return any value.
Uses of void
The void type is mainly used in the following scenarios:
<code class="cpp">void print_hello() { std::cout << "Hello, world!" << std::endl; }</code>
<code class="cpp">void swap(int& a, int& b) { int temp = a; a = b; b = temp; }</code>
<code class="cpp">void* ptr = nullptr;</code>
The difference between a void function and a function with a return type of int
void function and The main difference between functions with return type int is:
Example
The following example demonstrates the use of void type:
<code class="cpp">void print_number(int n) { std::cout << "The number is: " << n << std::endl; } int main() { print_number(42); return 0; }</code>
Output:
<code>The number is: 42</code>
In this example , the print_number
function is declared as void type, which means it does not return any value. However, it can accept an integer argument and print that integer.
The above is the detailed content of How to use void in c++. For more information, please follow other related articles on the PHP Chinese website!