unable to understand Increasing recursion
Unable to understand Increasing recursion
06.35
void incr(int n)
{
if (n < 1)
{
return;
}
incr(n - 1);
cout << n << " ";
}
@amit0001 okay so in this
if you have noticed we have written the cout statement after the recursion call which means that first there will be no ouput as the one recursion call will call another recursion call till it hits the base case:
and once after hitting the base case when it will return from that it will see what it has to do now then it will find the cout statement and it will print the value of n at that instant value of n is :
like if n=3 then it will call for and then for 1 and then 0 but when it will become less than 1 it will return and after returning it will print the values of n as 1 2 3.
@amit0001 see what the basic difference is whenever we write after the recursion call then it performs that piece of task after hitting the base case.
but in contrary to this when we write just with the recursion calls then it performs simultaneously.
not getting how after return it will go to down to print…
return means it stops the next execution ?
@amit0001 yes right return means no further recursion call will be made.
so in layman term after hitting return it will execute what it is wriiten after recursion call.
okay…thank you
…
yes i mark as resolve
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.