A Example (in the description from C + +):
Line number Program
0 P (int w)
1 {if (W>o)
2 {cout<<>< p=""><>
3 p (w-1);
4 p (w-1);
5}
6}
End
Print results after executing statement p (4): 4 3 2 1 1 2 1 1 3 2 1 1 2 1 1
Two Description
1. Recursive calls are the same as normal invocation, except that each function is called itself.
2. We can programmatically set the stack (user stack) ourselves to implement the same functionality as "recursive invocation".
3. 3. In recursive invocation, the system automatically sets up and manages the stack (the system stack), and
Without our intervention, this also adds to the mystery of recursive invocation. For better
To understand "recursive invocation", the system stack is now represented in a tabular manner.
4. Some notes for the "stack" format
The format of the stack is:
Square A
Square b
Grid C
The line number returned after the function call
Functions that are called
The value of W
Each time the function is called "into the stack" once, the function is finished, "out of the stack" once
Three Program Explanation:
1. Start calling P (4), at which point the statements executed are: 1, 2, 3
End
P (4)
4
Executes the statement of P (4) 2:cout<<>< p=""><>
However, because of statement 3, P (3) is also called during execution, and only P (3) is executed to continue execution P (4).
2. Start calling P (3), at which point the statements executed are: 1, 2, 3
4
P (3)
3
End
P (4)
4
When P (3) is finished executing, the statement 4 in P (4) is executed (so in square A, fill in "4").
Executes the statement of P (3) 2:cout< < p="">
As in the case above, when executing to statement 3, P (2) is called, and only P (2) is executed to continue with P (3).
3. Start calling P (2), at which point the statements executed are: 1, 2, 3
4
P (2)
2
4
P (3)
3
End
P (4)
4
Executes the statement of P (2) 2:cout<<>< p=""><>
As in the case above, when executing to statement 3, p (1) is called, and only P (1) is executed to continue with P (2).
4. Start calling P (1), at which point the statements executed are: 1, 2, 3
4
P (1)
1
4
P (2)
2
4
P (3)
3
End
P (4)
4
Executes the statement of P (2) 2:cout<<>< p=""><>
As in the case above, when executing to statement 3, p (0) is called, and only P (0) is executed to continue with P (1).
5. Begins calling P (0), at which point the statement executed is: 1
4
P (0)
0
4
P (1)
1
4
P (2)
2
4
P (3)
3
End
P (4)
4
Because W=0 does not satisfy statement 1, it jumps directly to statement 5, 6, and P (0) executes, p (0) is "out of the stack" operation.
6. The statements executed at this time are: 4
4
P (1)
1
4
P (2)
2
4
P (3)
3
End
P (4)
4
Since P (0) is executed and P (0) is 4 in square A, the statement 4:p (w-1) of P (1) is continued, and P (1) is called because the W value is 1 in P (0) C.
7. Start calling P (0)
5
P (0)
0
4
P (1)
1
4
P (2)
2
4
P (3)
3
End
P (4)
4
When P (0) is finished executing, the statement 5 in P (1) is executed (so in square A, fill in "5").
Because W=0 does not satisfy statement 1, it jumps directly to statement 5, 6, and P (0) executes, p (0) is "out of the stack" operation.
8. The statements executed at this time are: 5
4
P (1)
1
4
P (2)
2
4
P (3)
3
End
P (4)
4
Since P (0) is executed and P (0) is 5 in square A, the statement 5 (the last sentence) of P (1) continues to execute, so P (1) is finished and P (1) is "out of the stack" operation.
9.
4
P (2)
2
4
P (3)
3
End
P (4)
4
Since P (1) is executed and P (1) is 4 in square A, the statement 4:p (w-1) of P (2) is continued, and P (2) is called because the W value is 2 in P (1) c.
10. Start calling P (1), at which point the statements executed are: 1, 2, 3
5
P (1)
1
4
P (2)
2
4
P (3)
3
End
P (4)
4
When P (1) is finished executing, the statement 5 in P (2) is executed (so in square A, fill in "5").
Executes the statement of P (1) 2:cout<<>< p=""><>
When you execute to statement 3, you also call P (0), and only P (0) is finished to continue execution P (1).
11. Called P (0), the statement executed at this time is: 1
4
P (0)
0
5
P (1)
1
4
P (2)
2
4
P (3)
3
End
P (4)
4
Because W=0 does not satisfy statement 1, it jumps directly to statement 5, 6, and P (0) executes, p (0) is "out of the stack" operation.
12. The statements executed at this time are: 4
5
P (1)
1
4
P (2)
2
4
P (3)
3
End
P (4)
4
Since P (0) is executed and P (0) is 4 in square A, the statement 4:p (w-1) of P (1) is continued, and P (1) is called because the W value is 1 in P (0) C.
13. Start calling P (0)
5
P (0)
0
5
P (1)
1
4
P (2)
2
4
P (3)
3
End
P (4)
4
When P (0) is finished executing, the statement 5 in P (1) is executed (so in square A, fill in "5").
Because W=0 does not satisfy statement 1, it jumps directly to statement 5, 6, and P (0) executes, p (0) is "out of the stack" operation.
14. The statements executed at this time are: 5
5
P (1)
1
4
P (2)
2
4
P (3)
3
End
P (4)
4
Since P (0) is executed and P (0) is 5 in square A, the statement 5 (the last sentence) of P (1) continues to execute, so P (1) is finished and P (1) is "out of the stack" operation.
15.
4
P (2)
2
4
P (3)
3
End
P (4)
4
Since P (1) is executed and P (1) is 5 in square A, the statement 5 (the last sentence) of P (2) continues to execute, so P (2) is finished and P (2) is "Out of the stack" operation.
Note: Actually step 10~15 repeats the step 4~9, because they all call the P (1)
16.
4
P (3)
3
End
P (4)
4
Since P (2) is executed and P (2) is 4 in square A, the statement 4:p (w-1) of P (3) is continued, and P (3) is called because the W value is 3 in P (2) c.
17. Start calling P (2), at which point the statements executed are: 1, 2, 3
5
P (2)
2
4
P (3)
3
End
P (4)
4
When P (2) is finished executing, the statement 5 in P (3) is executed (so in square A, fill in "5").
Executes the statement of P (2) 2:cout<<>< p=""><>
As in the case above, when executing to statement 3, p (1) is called, and only P (1) is executed to continue with P (2).
18. Start calling P (1)
Omitted......
Note: Actually the step 17~29 repeats the 3~15, because they all call the P (2)
In this step, 2 1 1 are printed (see steps 3, 4, 10)
Four Conclusion and Analysis:
Steps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Results
4
3
2
1
1
2
1
1
3
2
1
1
2
1
1
The 5th result repeats the 4th result, because they all call P (1)
6th, 7, 8 results Repeat 3rd, 4, and 5 results, because they all call P (2)
The 9th to 15th result repeats the 2nd to 8th result, because they all call P (3)
http://www.bkjia.com/PHPjc/477490.html www.bkjia.com true http://www.bkjia.com/PHPjc/477490.html techarticle A. Example (described in C + +): line number program 0 p (int w) 1 {if (wo) 2 {coutw; 3 p (w-1); 4 p (w-1); 5} 6} End of execution statement P (4) After printing results: 4 3 2 1 1 2 1 ...