Search code examples
c++functionrecursionbreak

How do I exit the code at a certain breaking point in a function


In this function to calculate the factorial of a number:

int fact(int n) {
    
    if (n == 0 || n == 1)
        return 1;
    else
        return n * fact(n - 1);
}

How do I add a condition

if(n<0)
cout <<"Error negative values are not accepted";
//Here I want to add something that makes me exit the function and stop the code after the cout statement is printed

Solution

  • You can use std::exit() after the cout statement