#include<iostream>
using namespace std;
void fun2(int n)
{
if (n>0)
{
fun2(n-1);
cout << n << endl;
}
}
int main()
{
int x=3;
fun2(x);
return 0;
}
1 2 3
Difference b/w a loop and a recursion : Loop -> only ascending phase // Recursion -> Ascending and Descending phase.
If static variables are inside recursive function don’t show them in each tracing tree write them in global or outside variable and maintain a single copy of it.
Here static and global variable will give the same result.
If a recursive function is calling itself and that calling statement is the last statement in the function then it is called a recursive function.
3
i.e Time -> O(n).fun(3)
it'll create 4 activation records in the stack. Where as in a Loop only 1 activation record will be created.Therefore,
A loop is better than Tail Recursion.
If a recursive function is calling itself more than one time then it is called a Tree Recursion.
Recursion inside recursion: