[Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] does cout before return statement of recursive function stops it?


The body of an if-statement can be either a compound statement, which is a list of statements surrounded by {}, or it is the single statement following the if‘s condition. That means that this code:

if(n>=2)
    cout<<"number of times the function called: "<<endl;
    return n*factorial(n-1);

is completely equivalent to:

if(n>=2){
    cout<<"number of times the function called: "<<endl;
}
return n*factorial(n-1);

You probably intended the following:

int factorial(int n){
    if(n>=2){
        cout<<"number of times the function called: "<<endl;
        return n*factorial(n-1);
    }
    return 1; // you should ALWAYS return something from a non-void function
}

0

solved [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] [Solved] does cout before return statement of recursive function stops it?