Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you exit from a void function in C++?

Tags:

c++

People also ask

How do you get out of a void method?

Use a return statement! if (condition) return; You don't need to (and can't) specify any values, if your method returns void .

Can you return out of a void function?

A void function can returnA void function cannot return any values. But we can use the return statement. It indicates that the function is terminated. It increases the readability of code.

What is exit () function in C?

CServer Side ProgrammingProgramming. The exit () function is used to break out of a loop. This function causes an immediate termination of the entire program done by the operation system. The general form of the exit() function is as follows − void exit (int code);

Do you have to return for void C?

If a return value isn't required, declare the function to have void return type. If a return type isn't specified, the C compiler assumes a default return type of int . Many programmers use parentheses to enclose the expression argument of the return statement. However, C doesn't require the parentheses.


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.


You mean like this?

void foo ( int i ) {
    if ( i < 0 ) return; // do nothing
    // do something
}

void foo() {
  /* do some stuff */
  if (!condition) {
    return;
  }
}

You can just use the return keyword just like you would in any other function.