A question that has everything to do with the name of this site.
We know that one of the most commonly used examples for demonstrating the execution stack of a program is recursion. A recursive function must have: stop condition and recursion must solve a smaller instance of the same problem.
At runtime the recursive calls will be stacked and then unpinned (executed), from the smallest case to the largest, until the final value is returned.
If there is no stop condition or the recursive call is not for a smaller instance , the program goes into loop Stack Overflow .
Given the following code developed in C:
#include <stdio.h>
#include <conio.h>
int fat(int n);
int main()
{
int n;
printf("Calculo de FATORIAL\n");
printf("Entre com um numero:");
scanf("%d",&n);
printf("Fatorial:%d",fat(n));
getch();
return 0;
}
int fat(int n)
{
if (n==1)
return(1);
return(n * fat(n-1));
}
What would be the execution stack of 4! for example?
If there was no stopping condition, the program would stack until stack overflow occurred. At what point would this occur, ie how big is that stack?