My question comes from the fact that it is a bit confusing to know how this result is printed: ABCDEFGHIJKLMNOPQRSTUVWXYZ.
It is assumed that if there is a continuous recursion until reaching the letter A and finally printing it. How do you get to the letter B? Does memory execute all printf() starting from A? to then execute B, then C, etc. in memory of? Thanks.
#include<stdio.h>
void funcionA(char c);
void funcionB(char c);
int main()
{
printf("\n");
funcionA('Z'); //90
printf("\n");
return 0;
}
void funcionA(char c)
{
if(c > 'A') //65
funcionB(c);
printf("%c", c);
}
void funcionB(char c)
{
funcionA(--c);
}
Recursion is a lot like a loop: it repeats an action until a condition is met. The difference with recursion is that you accumulate function calls and when completing the exit condition you must unwind the calls made.
You can understand it better by simplifying your code :
When you call
funcionA
, the first thing it does is call ,funcionA
and the second thing it does is print the received parameter; since the first thing it does is call itself, you won't get to the second statementfuncionA
until you unwind all the calls:You can check this by turning the instructions over
funcionA
and observing that the printing is done backwards:Sample: