如何在函数的某个断点处退出代码

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

在这个函数中计算一个数的阶乘:

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

如何添加条件

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

你可以在cout语句后使用std::exit()