Functions in the C language can be called recursively, that is, you can adjust yourself directly (simply recursively) or indirectly (indirectly recursively).
Points:
1, C language functions can be invoked recursively.
2. Can be called directly or indirectly by two ways. Only direct recursive calls are currently discussed.
Second, recursive conditions
The following three conditions must be met in order to solve the problem by recursion:
1, can be resolved to solve the problem into a new problem, and this new problem solution is still the same as the original solution, but the object of treatment is regularly increasing or decreasing.
Description: The method of solving the problem is the same, the parameters of the call function are different each time (there is a regular increment or decrement), if there is no rule can not be applicable to recursive calls.
2, can apply this transformation process to solve the problem.
Description: Using other methods is more troublesome or difficult to solve, and using recursive method can solve the problem well.
3, there must be a clear end to the conditions of recursion.
Description: Be sure to be able to end recursive calls where appropriate. This may cause the system to crash.
Third, recursive examples
Example: Using recursive method to find n!
When n>1, the question of n! can be converted to n (n-1)! New problems.
Like n=5:
The first part: 5*4*3*2*1 N (n-1)!
Part II: 4*3*2*1 (n-1) * (n-2)!
Part III: 3*2*1 (n-2) (n-3)!
Part IV: 2*1 (n-3) (n-4)!
Part five: 1 (n-5)! 5-5=0, get a value of 1, end 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 instructions
1. When the function calls itself, the system will automatically keep the current variables and parameters in the function temporarily, in the new round of call process, the system for the new call function used in the variables and parameters to open up additional storage unit (memory space). Each call to the function uses a variable in a different memory space.
2. The more layers of recursive calls, the more storage units are occupied by the same name variables. Be sure to remember that every time a function is called, the system opens up new memory space for the function's variables.
3. When the function of this call ends, the system releases the memory space occupied by this call. The process of the program returns to the call point at the previous level, while obtaining data on the memory space occupied by the variables and parameters in the function when it first entered the layer.
4. All recursion problems can be solved by a non recursive method, but for some more complicated recursive problems, the method often makes the program become very complex and difficult to read, and the recursive call of function can make the program concise and clear with good readability when solving such problems. But because of the recursive call process, A recursive call to a function often lowers the efficiency of a program by creating a memory space for the variables in each layer call, remembering the return point after each layer call, and adding a lot of extra overhead.
V. Procedure Flow
FAC (int n)/* Use a different parameter for each call * *
{int t;///Each call will open a different memory space for the variable t. *
if (n==1) | | (n==0)/* When meeting these conditions return 1 * *
return 1;
Else
{T=N*FAC (n-1);/* Every time the program runs here, the function is called with n-1 as an argument, here is the call point * *
return t; /* Runs only when all the procedures in the previous call have ended. */
}
}