i don’t get to know that how the elements are popped up and how using the base case we haulted our function.
How the elements are popped up from the stack?
a recursive function has 3 main components
1)base case :the smallest problem you can solve yourself
2)smaller problem:you call your same function on smaller problem
3)self work:you do some basic work
consider fibonacci code
if(n==0||n==1){
return n;
}
it is the base case as this the smallest problem we can solve and we know its ans…thus we return the value from here
int fbn1=fib(n-1);
int fbn2=fib(n-2);
these are the smaller problems on which we call and ask recursion to do work when recursion return us the result of these 2 problems we then calculate our own result i.e int ans=fbn1+fbn2;
when a particular function hits a base case it is removed from stack…the control goes to the next line of the function which had called it…in that function there is one line left to execute that is to print (n)…thus the entire work of that function is done…thus it is removed from the stack
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.