How to call a void function in C++
Generally, any function has two types:
1. Void function: It doesn't return any value.
2. Non-void function: It returns some value.
Program to call a void function in C++
#include <iostream>
using namespace std;
void check()
{
cout <<"The void function has nothing to be returned, this is just a statement. \n";
}
int main()
{
check();
return 0;
}
Output:

Generally, the return statement is replaced by the print statement in a void function. Instead of returning a value, we simply print that particular value in the function itself.
For example, let’s see this program:
#include <iostream>
using namespace std;
string check()
{
return "The void function has nothing to be returned, this is just a statement";
}
int main()
{
cout<<check()<<endl;
return 0;
}
Output:

Here, we can observe the output, which is the same in both cases. But the thing here is we used a non-void function. You can see the changes in the following:
void check() à string check()
Here void is replaced by string since we must return a value which is of string data type.
check() à cout<<check()<<endl;
In the first case, we just called the function, but in the second case, we reached that function in a print statement (to show the output on the console). We can also observe that initially, we wrote the print statement in the void function, but in the second program, we wrote a print statement in the main function.
It's also important to know that, sometimes, the main function can be represented as a void as well as a non-void function in C, but in C++, it's not supported.
Reason: The reasoning as to why a return value is preferred from the main function is that the runtime library of C++ (in cases of both UNIX and Windows environments) uses the return value of the main function() as the exit code for the program, or better to say, the current process being executed.
Example:
#include <iostream>
using namespace std;
int main()
{
cout<<"Main function which is of non-void type"<<endl;
return 0;
}
Output:

#include <iostream>
using namespace std;
void main()
{
cout<<"Main function which is of void type"<<endl;
}
Output:

As usual, an error has occurred.
1)In C++, a void function can contain a return statement as well.
Example:
#include <iostream>
using namespace std;
void fun()
{
cout << "Hello";
return;
}
int main()
{
fun();
return 0;
}
Output:
Hello
2) Any void function can return another void function. Let’s see an example to understand better:
Example:
#include <iostream>
using namespace std;
void fun1()
{
cout<<"I am function 1"<<endl;
}
void fun2()
{
// fun2 Returning fun1 which is a void function
return fun2();
}
int main()
{
// Calling void function
fun1();
return 0;
}
Output:
