Functions in C can be called recursively, that is, they can be called directly (simple recursion) or indirectly (indirect recursion) by themselves.
Key points:
1. c functions can be called recursively.
2. It can be called either directly or indirectly. Currently, we only discuss direct recursive calls.
Ii. Recursive Conditions
The recursive method must meet the following three conditions:
1. the problem to be solved can be transformed into a new problem, and the solution to this new problem is still the same as the original solution, only the processed objects increase or decrease regularly.
Note: The method for solving the problem is the same. The parameters for calling a function are different each time (with a regular increase or decrease). If there is no rule, recursive calls cannot be applied.
2. You can apply this conversion process to solve the problem.
Note: using other methods is troublesome or difficult to solve, while using recursive methods can solve the problem well.
3. There must be a clear condition for ending recursion.
Note: You must end recursive calls in appropriate places. Otherwise, the system may crash.
Iii. Recursive instances
For example, use a recursive method to calculate n!
When n> 1, calculate n! Can be converted to n * (n-1 )! .
For example, n = 5:
Part 1: 5*4*3*2*1 n * (n-1 )!
Part 2: 4*3*2*1 (n-1) * (n-2 )!
Part 3: 3*2*1 (n-2) (n-3 )!
Part 4: 2*1 (n-3) (n-4 )!
Part 5: 1 (N-5 )! 5-5 = 0, get the value 1, and end the recursion.
Source program:
Fac (int n)
{Int t;
If (n = 1) | (n = 0) return 1;
Else
{T = n * fac (n-1 );
Return t;
}
}
Main ()
{Int m, y;
Printf ("Enter m :");
Scanf ("% d", & m );
If (m <0) printf ("Input data Error! N ");
Else
{Y = fac (m );
Printf ("n % d! = % D n ", m, y );
}
}
Iv. Recursive description
1. When the function calls itself, the system automatically retains the current variables and parameters in the function for the moment. During the next round of calling, the system opens up another storage unit (memory space) for the variables and parameters used by the newly called function ). The variables used by each function call are in different memory spaces.
2. The more layers of recursive calls, the more storage units occupied by variables with the same name. Remember that the system will open up new memory space for the variable of the function every time the function is called.
3. When the function of this call ends, the system will release the memory space occupied by this call. The process of the program is returned to the call point of the previous layer, and the data of the memory space occupied by the variables and parameters in the function when it first enters this layer is obtained.
4. All recursive problems can be solved using non-recursive methods. However, for some complicated recursive problems, non-recursive methods often make the program very complex and hard to understand, the recursive call of functions can make the program concise, clear, and readable when solving such problems. However, due to the recursive call process, the system needs to open up memory space for the variables in each layer of calls, remember the return points after each layer of calls, and increase a lot of additional overhead, therefore, recursive function calls usually reduce the program running efficiency.
V. Procedure
Fac (int n)/* use different parameters for each call */
{Int t;/* each call will open up different Memory Spaces for variable t */
If (n = 1) | (n = 0)/* when these conditions are met, 1 */is returned */
Return 1;
Else
{T = n * fac (n-1);/* each time the program runs here, N-1 is used as the parameter to call this function again. This is the call point */
Return t;/* It is run here only when all the processes of the previous call are completed. */
}
}