C Language Learning Tutorial fifth chapter-Functions (6)

Source: Internet
Author: User
Tags execution printf

Recursive calls to functions

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 repeatedly. Enter a new layer every time you call. For example there is a function f 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 5.9] The recursive method for calculating n! by recursion is used to calculate n! 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);
}
Long ff (int n)
{ ......
else F=ff (n-1) *n;
......
}
Main ()
{ ......
Y=FF (n);
......
}
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). The successive recursion expands as shown in Figure 5.3. 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. The return value of the function of FF (1) with the return value of 1,FF (2) is 1*2=2,FF (3) returned by 2*3=6,FF (4).
The return value is 6*4=24, and the last return value FF (5) is 24*5=120.

Example 5. 9 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 5.10] 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 on a (equal to 2, make it n ') a disk to B (with the help of C),
The steps are as follows:
(1) Move the n '-1 (equal to 1) disc on A to C, see Figure 5.5 (b).
(2) Move a disc on a to B, see Figure 5.5 (c)
(3) Move n '-1 (equal to 1) discs on C to B, see figure 5.5 (D)
B. Move a disc on a to C, see Figure 5.5 (E)
C. Move the n-1 on B (equal to 2, make it n ') a disk to C (with the help of a),
The steps are as follows:
(1) Move n '-1 (equal to 1) discs on B to a, see Figure 5.5 (F)
(2) move a plate on B to C, see Figure 5.5 (g)
(3) Move the n '-1 (equal to 1) disc on A to C, see Figure 5.5 (H).
This completes the process of moving three disks.
As can be seen from the above analysis, when n is greater than or equal to 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. Apparently this is a recursive
Process, the algorithm can be programmed as follows:
Move (int n,int x,int y,int z)
{
if (n==1)
printf ("%c-->%c\n", x,z);
Else
{
Move (N-1,x,z,y);
printf ("%c-->%c\n", 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 ');
}
Move (int n,int x,int y,int z)
{
if (n==1)
printf ("%-->%c\n", x,z);
Else
{
Move (N-1,x,z,y);
printf ("%c-->%c\n", x,z);
Move (N-1,Y,X,Z);
}
}
Main ()
{ ......
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. When n=4, the result of the program running 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

Scope of variables

When discussing the shape parametric of a function, it was mentioned that the form parametric allocates memory cells only during the call, and the call ends immediately. This indicates that the shape parametric is valid only within the function, leaving the function no longer available. The scope of the validity of this variable is called the field of the variable. Not only for parameter variables, all the quantities in C language have their own scope. Variable descriptions vary in the way they are scoped. C language variables, by scope can be divided into two types, namely, local variables and global variables.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.