We call recursion or recursion when a function calls itself.
In a recursive function, each call is created in memory a recurrence of the function with "isolated" commands and variables from previous occurrences, so a new scope for each call the function.
At the time the line is executed x(n-1)
, the current occurrence awaits a response of the new occurrence, that is, it is necessary to wait for the result of the operation x(n-1)
for the next operation to be carried out.
In practice the following operations take place:
- Call the function
x(n)
.
- Print the value of
n
(before).
- Ends the occurrence if
n
is equal to 0
returning a previous occurrence response (if any).
- Cycle
for
.
- Print the value of
i
.
- Starts a new occurrence
x(n-1)
(return to point 2) and await reply.
- Back to the point
4
if i < 3
.
- Print the value of
n
(after).
See that at the point 6
we create a new occurrence (n-1) and this causes the current occurrence (n) to wait for the response of the new occurrence before moving to the point 7
.
A step by step example for n=2
according to the above scheme:
1. x(2);
2. print "before, n: 2"
3. N > 0
4. For i=1
5. print "for i: 1"
6. x(1) //n-1 em que n=2
1. Iniciou uma nova ocorrência
2. print "before, n: 1"
3. N > 0
4. For i=1
5. print "for i: 1"
6. x(0) //n-1 em que n=1
1. Iniciou uma nova ocorrência
2. print "before, n: 0"
3. N <= 0
8. print "after, n: 0"
[Termina a ocorrência]
4. For i=2
5. print "for i: 2"
6. x(0) //n-1 em que n=1
1. Iniciou uma nova ocorrência
2. print "before, n: 0"
3. N <= 0
8. print "after, n: 0"
[Termina a ocorrência]
4. For i=3
8. print "after, n: 1"
[Termina a ocorrência]
4. For i=2
5. print "for i: 2"
6. x(1) //n-1 em que n=2
1. Iniciou uma nova ocorrência
2. print "before, n: 1"
3. N > 0
4. For i=1
5. print "for i: 1"
6. x(0) //n-1 em que n=1
1. Iniciou uma nova ocorrência
2. print "before, n: 0"
3. N <= 0
8. print "after, n: 0"
[Termina a ocorrência]
4. For i=2
5. print "for i: 2"
6. x(0) //n-1 em que n=1
1. Iniciou uma nova ocorrência
2. print "before, n: 0"
3. N <= 0
8. print "after, n: 0"
[Termina a ocorrência]
4. For i=3
8. print "after, n: 1"
[Termina a ocorrência]
4. For i=3
8. print "after, n: 2"
[Termina a ocorrência]
In the above listing, I used the horizontal spaces to simulate a new occurrence of the function x
.
If we extract only the lines in which the "print" appears, we will have the following:
before, n: 2
for i: 1
before, n: 1
for i: 1
before, n: 0
after, n: 0
for i: 2
before, n: 0
after, n: 0
after, n: 1
for i: 2
before, n: 1
for i: 1
before, n: 0
after, n: 0
for i: 2
before, n: 0
after, n: 0
after, n: 1
after, n: 2
Although the first occurrence is x(2)
, the after, n: 2
only appears at the bottom of the list because new occurrences have appeared in the middle and fired the console.log
before returning a reply.
I’m going to do some research on these terms to learn more. Regarding the flow of the code I have another question, why after the
after, n:1
, marked as blue in the print, it goes straight to thefor
instead of going to thebefore, n:1
?– dmg Geronimo