I would like to understand the concept of recursion beyond "It's a function that calls itself".
If I write the following code:
The following will print
I need help in understanding how the stack unwinds.
Regards
If I write the following code:
Code:
void stack(int num)
{
if (num == 0)
{
printf("we are done \n");
}
else
{
printf("%d has been added to the stack \n", num);
stack(num - 1);
printf("%d has been removed from the stack \n", num);
}
}
int main(int argc, const char * argv[])
{
stack(10);
return 0;
}
The following will print
Code:
10 has been added to the stack
9 has been added to the stack
8 has been added to the stack
7 has been added to the stack
6 has been added to the stack
5 has been added to the stack
4 has been added to the stack
3 has been added to the stack
2 has been added to the stack
1 has been added to the stack
we are done
1 has been removed from the stack
2 has been removed from the stack
3 has been removed from the stack
4 has been removed from the stack
5 has been removed from the stack
6 has been removed from the stack
7 has been removed from the stack
8 has been removed from the stack
9 has been removed from the stack
10 has been removed from the stack
I need help in understanding how the stack unwinds.
Regards