How do you exit from a void function in C++?
Solution 1:
Use a return statement!
return;
or
if (condition) return;
You don't need to (and can't) specify any values, if your method returns void
.
Solution 2:
You mean like this?
void foo ( int i ) {
if ( i < 0 ) return; // do nothing
// do something
}
Solution 3:
void foo() {
/* do some stuff */
if (!condition) {
return;
}
}
You can just use the return keyword just like you would in any other function.