What Is a Recursive Call?
A recursive call is a special kind of nested call. It is a function that calls itself or calls itself again after calling other functions. As long as the calls between functions can cause a loop, it must be a recursive call. Recursive call is a solution One is a logical idea that divides a large job into smaller jobs that are gradually decreasing. For example, a monk needs to move 50 stones. He thought that as long as 49 stones are removed first, the remaining one can be moved. Then consider the 49 blocks. As long as the 48 blocks are removed, the remaining blocks can be moved. Recursion is an idea, but in the program, it is achieved by relying on the function nesting feature.
Recursive call
- Chinese name
- Recursive call
- Foreign name
- recursive invocation
- Function model
- fun (formal parameter)
- Recursive example
- Code to calculate factorial
- A recursive call is a special kind of nested call. It is a function that calls itself or calls itself again after calling other functions. As long as the calls between functions can cause a loop, it must be a recursive call. Recursive call is a solution One is a logical idea that divides a large job into smaller jobs that are gradually decreasing. For example, a monk needs to move 50 stones. He thought that as long as 49 stones are removed first, the remaining one can be moved. Then consider the 49 blocks. As long as the 48 blocks are removed, the remaining blocks can be moved. Recursion is an idea, but in the program, it is achieved by relying on the function nesting feature.
- Recursion in C
- Code to calculate factorial
- long fact (long n)
- {
- if (n == 0 || n == 1) return 1L;
- else return n * fact (n-1);
- }
- This function is called fact, and it calls itself. This is a typical recursive call. The calling process is similar to a stack.
- Note: The calling function is called function again. Performing a recursive function will call itself repeatedly. Each call will enter a new level.
- int f (int x)
- {
- int y;
- z = f (y);
- return z;
- } This function is a recursive function. But running the function will call itself endlessly, which is of course incorrect. To prevent recursive calls from being made without termination, there must be a means within the function to terminate the recursive calls. A common method is to add a conditional judgment. After a certain condition is met, no recursive call is made, and then return layer by layer. The following example illustrates the execution process of a recursive call.
- Note: Linked lists are, to some extent, recursive calls.
- Recursion in Pascal
- const
- z = 10000;
- var
- a: array [0..z + 1] of integer;
- n, j, i, k: longint;
- begin
- readln (n); write (n, '! =');
- begin
- a [1]: = 1;
- for i: = 1 to n do
- begin
- for j: = 1 to z do
- a [j]: = a [j] * i;
- for k: = 1 to z do
- begin
- a [k + 1]: = a [k + 1] + a [k] dl 10;
- a [k]: = a [k] mod 10;
- end;
- end;
- i: = z; k: = 0;
- repeat
- if a [i] <> 0 then k: = 1;
- i: = i-1;
- until k = 1;
- k: = 0;
- for j: = i + 1 downto 1 do
- write (a [j]);
- end;
- writeln;
- end.
- Recursion in C ++ language
- #include <iostream>
- using namespace std;
- int fac (int n)
- {
- int s = 1;
- for (int i = n; i> 0; i--)
- {
- if (s <= s * i) s = s * i;
- else
- {
- cout << "over int area" << endl;
- return 0;
- };
- }
- return s;
- }
- void main ()
- JAVA recursive call
- public class TestDg {
- public static void main (String [] args) {
- System.out.println (method (5));
- }
- public static int method (int n) {
- if (n == 1)
- return 1;
- else
- return n * method (n-1);
- }
- }
- Hanoi Tower-the most classic case of software recursive call
- #include <
Before recursive call
- When calling another function during the running of one function, the system needs to complete 3 things before running the called function:
- (1) Pass all the actual parameters, return address and other information to the called function to save;
- (2) Allocate a storage area for the local variables of the called function;
- (3) Transfer control to the entry of the called function.
Calling recursively
- And before returning the calling function from the called function, the system should also complete 3 tasks:
- (1) Save the calculation result of the adjusted function;
- (2) release the data area of the called function;
- (3) Transfer control to the calling function according to the return address saved by the called function. When multiple functions form a nested call, follow the principle of call first and return first.
Recursive call recursive function characteristics
- The structure of all recursive functions is similar.
- (1) The function should call itself directly or indirectly.
- (2) There must be a recursive termination condition check, that is, after the recursive termination condition is met, its own function is no longer called.
- (3) If the conditions for recursive termination are not met, the expression involving the recursive call is called. When calling the function itself, the parameters related to the termination condition need to be changed, and the direction of the recursive termination needs to be changed.
Summary of recursive calls
- The function calling principle is consistent with the implementation of the data structure stack. It also shows that function calls are implemented through the stack.