A function calls itself in its function body called a recursive call. This function is called a recursive function. The C language allows recursive invocation of functions. In a recursive call, the calling function is a function of the modulation. Executing a recursive function calls itself over and over again, entering a new layer every time it is called
First, the basic content:
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:
Copy Code code as follows:
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
Copy Code code as follows:
FAC (int n)/* * Each call uses a different parameter/
{int t;////Each call will open a different memory space for variable t/*
if (n==1) | | (n==0)/* When these conditions are met to return 1 */
return 1;
Else
{T=N*FAC (n-1);/* Every time the program runs here, it will call this function again with n-1 as a parameter, here is the call point * * *
return t; /* Runs only when all the procedures in the previous call have ended. */
}
}
A function calls itself in its function body called a recursive call. This function is called a recursive function. The C language allows recursive invocation of functions. In a recursive call, the calling function is a function of the modulation. The execution of a recursive function calls itself repeatedly, entering the new layer every time it is invoked. For example there is a function f as follows:
Copy Code code as follows:
int f (int x)
{
int y;
z=f (y);
return z;
}
This function is a recursive function. But running the function will endlessly call itself, which is certainly not true. In order to prevent recursive calls to proceed without termination, there must be a means of terminating recursive calls within the function. The common approach is to add conditional judgments, meet certain conditions, no longer recursive calls, and then return to each layer. The following example illustrates the execution of a recursive call.
"Example 8.5" using recursive method to compute n!
The recursive method for calculating n! can be expressed by using the following formula:
N!=1 (n=0,1)
NX (N-1)! (n>1)
The formula can be programmed as follows:
Long ff (int n)
{
Long F;
if (n<0) printf ("N<0,input error");
else if (n==0| | N==1) f=1;
else F=ff (n-1) *n;
return (f);
}
Main ()
{
int n;
Long y;
printf ("Ninput a Inteager number:n");
scanf ("%d", &n);
Y=FF (n);
printf ("%d!=%ld", n,y);
}
The function ff given in the program is a recursive function. When the main function calls FF, it enters the function ff execution, and if n<0,n==0 or n=1 will end the function execution, the FF function itself is called recursively. Since the argument of each recursive call is n-1, the value of the n-1 is given to the parameter n, and the value of the parameter n is 1 when the n-1 value is 1 o'clock, which will terminate the recursion. It can then be returned on a per-layer basis.
Let's take another example to illustrate the process. Set the implementation of this program input to 5, that is, 5!. The calling statement in the main function is Y=FF (5), and after entering the FF function, because n=5 is not equal to 0 or 1, the F=FF (n-1) *n, or F=ff (5-1) *5, should be executed. This statement makes a recursive call to FF (4).
After four recursive calls, the value of the FF function parameter becomes 1, so the recursive call is not resumed and the calling function is returned on a layered basis. A function return value of FF (1) is a return value of 1,FF (2) with the return value of 1*2=2,FF (3) of 2*3=6,FF (4) is 6*4=24, and the last value FF (5) is 24*5=120.
Example 8.5 can also be done without a recursive method. If you can use recursion, that is, multiply by 2 from 1, then multiply by 3 ... Until N. Recursive chunking recursive method is easier to understand and implement. But some problems can only be realized by recursive algorithm. The typical problem is the Hanoi tower problem.
"Example 8.6" Hanoi tower problem
There are three needles on a board, A,b,c. A PIN has 64 sizes of discs, big in the bottom, small on the top. As shown in Figure 5.4. To move the 64 discs from the A-pin to the C-pin, you can only move one disc at a time, and the move can be done with a B-pin. But at any time, the disc on any needle must keep the big plate down and the small disc on the top. Find the steps to move.
The analysis of this algorithm is as follows, set A has n a plate.
If n=1, the disk is moved directly from a to C.
If n=2, then:
1. Move the n-1 (equal to 1) discs on A to B;
2. Move a disc from A to C;
3. Finally, move the n-1 (equal to 1) discs on B to C.
If n=3, then:
A. Move the n-1 (equal to 2, make it n ') discs on A to B (with the help of C) as follows:
(1) Move n '-1 (equal to 1) discs on a to C.
(2) Move one of the discs on A to B.
(3) Move n '-1 (equal to 1) discs on C to B.
B. Move one of the discs on a to C.
C. Move the n-1 on B (equal to 2, make it n ') a disk to C (with the help of a), as follows:
(1) Move n '-1 (equal to 1) discs on B to a.
(2) move a plate on B to C.
(3) Move n '-1 (equal to 1) discs on a to C.
This completes the process of moving three disks.
As you can see from the above analysis, when n is greater than 2 o'clock, the moving process can be decomposed into three steps:
The first step is to move the n-1 disc on A to B;
The second step is to move a disc on a to C;
The third step is to move the n-1 disc on B to C, where the first and third steps are similar.
When N=3, the first and third steps are decomposed into a similar three-step, which is to move N '-1 discs from one needle to another, where N ' =n-1. Obviously this is a recursive process, the algorithm can be programmed as follows:
Move (int n,int x,int y,int z)
{
if (n==1)
& nbsp; printf ("%C-->%CN", x,z);
Else
{
move (n-1,x,z,y);
& nbsp; printf ("%C-->%CN", x,z);
Move (n-1,y,x,z);
}
}
Main ()
{
int h;
printf ("Ninput number:n");
scanf ("%d", &h);
printf ("The Step to moving%2d diskes:n", h);
Move (H, ' A ', ' B ', ' C ');
}
as you can see from the program, the move function is a recursive function that has four formal parameter n,x,y,z. n indicates the number of discs, x,y,z three needles respectively. The move function is the function of moving n disks on X to Z. When N==1, the disc on the x is moved directly to Z and the output is x→z. For example, N!=1 is divided into three steps: recursively calls the move function, moves the n-1 disk from X to Y, outputs the x→z, recursively calls the move function, and moves the n-1 disk from Y to Z. In the recursive call process n=n-1, so the value of n gradually descending, the last n=1, the end of recursion, layered return. The result of the program running when N=4 is:
input Number:
4
the step to moving 4 Diskes:
a→b
a→c
b→c
a→b
c→a
c→b
a→b
a→c
b→c
b→a
c→a
b→c
A→B
a→c
b→c