Go: Deep understanding of the invocation process of recursive functions
Here is an example of a simple but well-illustrated recursive invocation:
1 /*Recursive Example*/2#include <stdio.h>3 voidUp_and_down (int);4 intMainvoid)5 {6Up_and_down (1);7 return 0;8 }9 voidUp_and_down (intN)Ten { Oneprintf"Level %d:n Location%p/n", n,&n);/*1*/ A if(n<4) -Up_and_down (n+1); -printf"Level %d:n Location%p/n", n,&n);/*2*/ the } - - Output Results -Level1: N Location 0240ff48 +Level2: N Location 0240ff28 -Level3: N Location 0240ff08 +Level4: N Location 0240fee8 ALevel4: N Location 0240fee8 atLevel3: N Location 0240ff08 -Level2: N Location 0240ff28 -Level1: N Location 0240ff48
First of allMain ()Using Parameters1Called a functionUp_and_down ()SoUp_and_down ()Medium form parametersNThe value is1,So print the statement#1Output theLevel1。 Then, becauseNThe value is less than4SoUp_and_down ()(section1-level) Usage parametersn+1That is, the value2Called theUp_and_down () (The2Level).MakesNIn section2is assigned in a level call2,Print Statements#1The output isLevel2。 Similarly, the following two calls are printed separatelyLevel3AndLevel4。
When the implementation of the first4Level call,NThe value is4SoifThe condition of the statement is not satisfied. This is not the time to continue callingUp_and_down ()Function. The4Level call then executes the print statement#2, that is, the outputLevel4BecauseNThe value is4。 Now the function needs to executereturnStatement, at which point4The call function that returns control to the function, that is, the3-Level call function. The3The previous executed statement in the level call function isifclause in the4-Level invocation. Therefore, it continues to execute its successor code, that is, to execute the print statement#2, this will outputLevel3. When the first3After the level call has ended, the first2Level call function to begin execution, that is, the outputLevel2. In turn.
Note that each level of recursion uses its own private variable n . You can see the value of the address to prove it.
The basic principle of recursion:
1 each function call will be returned one time. When a program flow executes at the end of a level of recursion, it moves to the previous level of recursion to continue execution.
in the 2 recursive function, the statements before the recursive call and all levels of the function are in the same order. If the print statement #1 in front of the recursive call statement, it was executed 4 times in the order of the recursive call.
3 Each level of a function call has its own private variable.
in the 4 recursive function, the execution order of the statements after the recursive call statement is reversed in the order of each called function.
5 Although each level recursively has its own variables, the function code does not get copied.
6 The Recursive function must contain statements that can terminate recursive calls.
Let's look at an example of a specific recursive function call:
1 /*Enter an integer, Output binary form*/2#include <stdio.h>3 voidTo_binary (unsignedLongn);4 5 intMainvoid)6 {7UnsignedLongNumber ;8printf"Enter An integer (Q to quit):/n");9 while(SCANF ("%ul", &number) = =1)Ten { Oneprintf"Binary equivalent:"); A to_binary (number); -Putchar ('/ N'); -printf"Enter An integer (Q to quit):/n"); the } -printf"done./n"); - return 0; - + } - voidTo_binary (unsignedLongN/*Recursive Functions*/ + { A intR; atr=n%2;/*computes the value of the n%2 before the recursive call, and then outputs after the recursive invocation of the statement. This - The first calculated value is instead the last output*/ - if(n>=2) -To_binary (n/2); -Putchar ('0'+R);/*if R is 0, the expression ' 0 ' +r is the character ' 0 '; if R is 1, the value of the expression is - ' 1 '. Note that the numeric encoding of the character ' 1 ' is greater than the numeric encoding of the character ' 0 ' 1. in Both the ASCII and EBCDIC codes satisfy this condition. */ - return; to } + - the * the output is: $ Panax Notoginseng Enter An integer (Q to quit): - 9 theBinary equivalent:1001 + Enter An integer (Q to quit): A 255 theBinary equivalent:11111111 +Enter an integer (Q to quit):
Deep understanding of the invocation process of recursive functions