Many people feel dizzy when learning recursive functions. they cannot understand the principle and running mechanism of recursive functions. This article will explain in detail the running mechanism and application of recursive functions. So what is a recursive function? A recursive function is a self-called function. it calls the self-called function directly or indirectly in the function body... SyntaxHighlighter. all ();
Many people feel dizzy when learning recursive functions. they cannot understand the principle and running mechanism of recursive functions. This article will explain in detail the running mechanism and application of recursive functions.
So what is a recursive function?
A recursive function is a self-called function that calls itself directly or indirectly in the function body. However, you must set the self-called conditions. if the conditions are met, the function itself is called, if this parameter is not met, the function's self-call will be terminated, and the current process's main control right will be handed back to the previous function for execution. it may be difficult to understand this.
Well, let's take the example in Gao Luofeng's "Let's talk about PHP" as an example.
Function test ($ n ){
Echo $ n ."";
If ($ n> 0 ){
Test ($ n-1 );
} Else {
Echo "<--> ";
}
Echo $ n ."";
}
Test (10 );
?>
First, let's think about the final output result of this example?
Well, let's take a look at the output results of this function:
10 9 8 7 6 5 4 3 2 1 0 <--> 0 1 2 3 4 5 6 7 8 9 10
I don't know if this result is the same as what everyone imagined?
Well, I will explain it step by step...
Step 1: Execute test (10), echo 10, and then execute test (9) because 10> 0. The echo 10 is not executed yet.
Step 2: Execute test (9), echo 9, and then execute test (8) because of 9> 0. Similarly, echo 9 is not executed yet.
Step 3: Execute test (8), echo 8, and then execute test (7) because of 8> 0. Similarly, echo 8 is not executed in time.
Step 4: Execute test (7), echo 7, and then execute test (6) because of 7> 0. Similarly, echo 7 is not executed in time.
Step 5: Execute test (6), echo 6, and then execute test (5) because 6> 0. Similarly, echo 6 will not be executed in time.
...........
Step 10: Execute test (0) and echo 0. at this time, the 0> 0 conditions are not met. Instead, the test () function is not executed, but echo "<--> ", and execute echo 0
10 9 8 7 6 5 4 3 2 1 0 <--> 0 1 2 3 4 5 6 7 8 9 10
At this time, the output content is shown in the red part. at this time, the function no longer calls itself and begins to return the main control right of the process to the previous function for execution.
That is to say, the last echo that can be output before all the test () functions are executed.
The process is as follows:
In the first to tenth steps of function execution, the function outputs the green part. if the red part is not "ready", you should call the function to execute the operation, and so on, until the process is executed until it no longer meets the call conditions and outputs "<-->", the process should execute the code output "not come and.
Just like the games we usually play, killing a monster and dropping out a baby, but there are other monsters waiting for you to destroy, you have to destroy all the monsters before you can come back and pick up the baby one by one.
How can I explain it to you?
Some may ask again. after executing all the test functions, I finally output 0.
That is, output here,
10 9 8 7 6 5 4 3 2 1 0 <--> 0
Why is the next output 1 instead of 10,
To help you understand this problem, I will give you an example below:
See the following code:
Function one ($ num ){
Echo $ num;
Two ($ num-1 );
Echo $ num;
}
Function two ($ num ){
Echo $ num;
Three ($ num-1 );
Echo $ num;
}
Function three ($ num ){
Echo $ num;
}
One (3 );
?>
The above code breaks down the test () function. let's think about it:
When executing the one (3) function, like the test () function, output 3 first, and then call the two (2) function,
Note that the following 3 is not output yet,
Next, execute the two (2) function, output 2, and call the three (1) function. Similarly, there is no time to output the following 2,
Execute three (1) and output 1 directly. other functions are not called,
At this point, we want to see if the two () function has not been fully executed. well, after the two () function is executed, that is, output 2 below, and then start to execute the one () function that is not completed, that is, output 3 below. at this time, all functions are executed.
The output result is:
3 2 1 2 3
How can this problem be easily understood?
If you still don't quite understand it, please follow up with the post below and provide valuable suggestions. I will make corresponding improvements and explanations.
From zdrjlamp