Home > Backend Development > PHP Tutorial > A popular understanding of functions and recursion_PHP tutorial

A popular understanding of functions and recursion_PHP tutorial

WBOY
Release: 2016-07-14 10:09:41
Original
870 people have browsed it

Brothers who have no foundation may find it difficult to understand the recursion of functions, especially the concept of returning output in recursion and how it is returned.

The following is an example from the textbook. I will take it out and explain it based on my understanding. Hope this helps those who haven't figured it out yet.
function test($n){
echo $n."nbsp";
if($n>0){
                test($n-1);//Call the function itself.
}else{
echo "<--->";
}
echo $n."$nbsp";
}
test(3);//Call function output.
?>
The result is 3 2 1 0 <---> 0 1 2 3
What is easier for everyone to understand is the output of 3 2 1 0 in the front, but it is not very clear why the output of 0 1 2 3 in the back is.
To understand the recursive regression output, you must first figure out what a function is,
Official explanation:
A function is a named block of code that performs a specified task.
One of the reasons to use functions is:
Can improve program reusability
Reusability means it can be used repeatedly without writing the same code multiple times. It is to write the code repeatedly written in the program as a function, and then just call the function name when it is needed in other places, without having to write the same code again.
To put it simply, a function is a piece of code that has been written and a gun that has been built, and then placed in a public warehouse,
Functions with different functions have their own names [function function name(){}], just like guns with different functions also have their own names, such as rifles, sniper rifles, pistols, etc. These well-named functions and guns Everything has been written, built, and placed in the warehouse.
Then when someone wants to use a certain function, they go to the warehouse and call the name of the function [function name ()], and then take the function out and use it, just like the police go to the warehouse to get it according to different requirements of the task. Guns with different functions.
————This is the definition of function.
Usage of function:
The official name is: call such as aaa();
Popular understanding is: who (called the name of the function) came here and wrote the code given to you at this position.
My understanding is that it is replacement. The function call is a placeholder. When encountering this placeholder [aaa()], it is replaced with the placeholder [aaa()] specified by the placeholder that has been written. code in the function. Replace when encountered, and then the program continues to execute in sequential order.
Specific examples:
/*=============Warehouse containing functions (pistols)========================*/
function test($n){
echo $n."nbsp";
if($n>0){
                test($n-1);//Call the function itself.
}else{
echo "<--->";
}
echo $n."$nbsp";
}
/*=============Warehouse containing functions (pistols)======================*/
test(3); //Call the function. As I think, it encounters a placeholder and needs to replace the code in the function named test. (You can also understand it as going to the warehouse and calling it test. The gun is loaded with 3 bullets)
So the first encounter of test(3) became
/***********************Replace************************ on first call*/
echo $n."nbsp"; //At this time $n=3 there are three bullets
if($n>0){ //Judge that $n is 3 greater than 0, execute the following
test ($ n-1); // When you encounter the call function again, replace it when you encounter it (one shot of the bullet)
}else{
echo "<--->"; //Do not execute
}
echo $n."$nbsp"; // At this time $n=3, so the output is 3
/***********************Replace************************ on first call*/
/***********************Replace********************** on the second call*/
echo $n."nbsp"; //At this time $n=3 there are three bullets
IF ($ n & gt; 0) {// Judgment $ n & gt; 3 executes
                                                                                                                                                                                                                       echo $n."nbsp";
                                                                                                                                                                                                            
test ($ n-1); // When you encounter the call function again, replace it when you encounter it, (if the bullet is hit again)
                                                                                                                                                       
                                                                                                                                                                                                                                          echo "<--->"; //Do not execute
                    }
                                                                                                                                                                                                       echo $n."$nbsp";//At this time $n=2, so the output is 2
}else{
echo "<--->"; //Do not execute
}
echo $n."$nbsp"; //At this time $n=3, so the output is 3
/***********************Replace********************** on the second call*/
/***********************Replace************************ on the third call*/
echo $n."nbsp"; //At this time $n=3 there are three bullets
IF ($ n & gt; 0) {// Judgment $ n & gt; 3 executes
                                                                                                                                                                                                                       echo $n."nbsp";
IF ($ n & gt; 0) {// Judgment $ n is 2 greater than 0, execute below
                                                                                                                                                                                                                                   echo $n."nbsp";
IF ($ n & gt; 0) {// Judgment $ n is 1 greater than 0, execute below
test ($ n-1); // When you encounter the call function again, replace it when you encounter it, (one shot is hit)
                                                                                                                                                                 
echo "& lt; --- & gt;" "; // No execution
                                                                                                                                                                                                                      
echo $ n. "$ Nbsp"; // At this time $ n = 1, so output 1
                                                                                                                                                     
                                                                                                                                                                                                                                          echo "<--->"; //Do not execute
                      }
                                                                                                                                                                                                       echo $n."$nbsp";//At this time $n=2, so the output is 2
}else{
echo "<--->"; //Do not execute
}
echo $n."$nbsp"; //At this time $n=3, so the output is 3
/***********************Replace************************ on the third call*/
/***********************Replace************************ on the fourth call*/
echo $n."nbsp"; //At this time $n=3 there are three bullets
IF ($ n & gt; 0) {// Judgment $ n & gt; 3 executes
                                                                                                                                                                                                                       echo $n."nbsp";
IF ($ n & gt; 0) {// Judgment $ n is 2 greater than 0, execute below
                                                                                                                                                                                                                                   echo $n."nbsp";
IF ($ n & gt; 0) {// Judgment $ n is 1 greater than 0, execute below
                                                                                                     
                                                                                                                                                                                                                     echo $n."nbsp";
                                                                                                                                                                                                                         ​ 
test ($ n-1); // I will not replace it because it is not executed.
                                                                                                                    
echo "& lt; --- & gt;"; // Output & lt; ----- & gt;
                                                                                                                                                                                                     
echo $ n. "$ Nbsp"; // At this time $ n = 0, so output 0
                                                                                                                                  
echo "& lt; --- & gt;" "; // No execution
                                                                                                                                                                                                                         
echo $ n. "$ Nbsp"; // At this time $ n = 1, so output 1
                                                                                                                                                       
                                                                                                                                                                                                                                          echo "<--->"; //Do not execute
                    }
                                                                                                                                                                                                       echo $n."$nbsp";//At this time $n=2, so the output is 2
}else{
echo "<--->"; //Do not execute
}
echo $n."$nbsp"; //At this time $n=3, so the output is 3
/***********************Replace************************ on the fourth call*/
/*##################Finally, we get a code consisting only of if judgment statements and basic expressions############## #############*/
test(3);//Only call replacement once at the beginning;
//$n=3
echo $n."nbsp";//Output 3
if($n>0){
//test(3-1) is replaced by the following
echo $n."nbsp";//Output 2
                if($n>0){
                                                                                                                               using   using                                              
                                                                                                                                                                                   echo $n."nbsp"
                                                                                                                                                           
                                                                                                                                                                                      to
                                                                                                                                                                                                          echo $n."nbsp";
                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                     
test ($ n-1); // Not executed because it is not about 0
                                                                                                                                          
echo "& lt; --- & gt;" "; // Output & lt; --- & gt;
                                                                                                                                                                                                                         
echo $ n. "$ Nbsp"; // Output 0
                  }else{
                                                                                                                                                                                                                                                                          echo "<--->";//Do not execute
                }
                                                                                                                                                                                                                              echo $n."$nbsp";//Output 1
        }else{
                    echo "<--->";//Do not execute
        }
echo $n."$nbsp";//Output 2
}else{
echo "<--->";////Do not execute
}
echo $n."$nbsp";//Output 3
/*##################Finally got######################## ##*/
The result output in sequence according to the final code is
The output result is 3 2 1 0 <--> 0 1 2 3
The use of functions and recursion is ultimately parsed into the above code for execution,
This is what I understand about functions and recursion. In one sentence: "Originally there was no recursion in the world. As more functions appeared, there was recursion."
A little simple understanding, I hope it can help brothers who have no foundation to understand functions, understand recursion, and finally form their own ideas and ideas

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477637.htmlTechArticleBrothers without basic knowledge may find it difficult to understand the recursion of functions, especially the concept of returning output in recursion. , how it is returned. Below is an example from the textbook, take it out...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template