Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return from void functions in C++
The void functions are called void because they do not return anything. "A void function cannot return anything" this statement is not always true. From a void function, we cannot return any values, but we can return something other than values. Some of them are like below.
A void function can return
A void function cannot return any values. But we can use the return statement. It indicates that the function is terminated.
Example
The following example demonstrates a void function with the return statement:
#include <iostream>
using namespace std;
void my_func() {
cout << "From my_function" << endl;
return;
}
int main() {
my_func();
return 0;
}
Following is the output to the above program:
From my_function
A void function can return another void function
You can also use a function having return type void inside a void function with return statement.
Example
In this example, one void function can call another void function while it is terminating that means, inside my_func(), it calls another_func() using the return statement. This doesn't return a value but instead executes another void function before it is fully exiting.
#include <iostream>
using namespace std;
void another_func() {
cout << "From another_function" << endl;
return;
}
void my_func() {
cout << "From my_function" << endl;
return another_func();
}
int main() {
my_func();
return 0;
}
Following is the output to the above program:
From my_function From another_function