Python Basics "Eighth article": Anatomy of Recursive functions

Source: Internet
Author: User

Recursive functions

If the function contains a call to itself, the function is a recursive function!

Let's start with the Fibonacci sequence: The Fibonacci sequence becomes the Golden section, showing the form 0, 1, 1, 2, 3, 5, 8, 13, 21, and 、.......

Can see the first two number of the sum equal to the third number 0 + 1 = +1=2,1+2=3......

Parse the recursive function through the Fibonacci sequence:

1 #!/usr/bin/env Python32 #detailed analysis of recursive functions through Fibonacci sequences3 #0, 1, 1, 2, 3, 5, 8, , 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,286574 5 defF1 (Depth, a1, A2):6     Print("First%s cycles:"%depth,a1,a2)7     8     ifdepth = = 3:9         Ten         returnA1,A2 One      Aa3 = A1 +A2 -R = F1 (depth +1, A2, A3) -     returnR the  -ret = F1 (1,0,1) - Print(ret) -  + " " - Code Analysis: + Depth: To clearly analyze the operation and status of each step A A1: To get Fibonacci numbers from the initial goal at A2: The first two numbers and the third number can be seen from the Fibonacci sequence. - The program starts from the top down and then executes: - 1. When you see a function, first put the entire function in memory - 2. Proceed to the next step to see the function name (F1) with the argument (1,0,1) assigned to a variable (ret), execute the function - 3. The result of the function is output when the execution continues. - the process of executing the function: in  - 1. The argument is passed into the corresponding parameter of the function that is placed in memory (depth = 1, a1 = 0, a2 = 1) to def f1 (depth = 1, a1 = 0, a2 = 1): +  - 2.%s,%depth is now equal to 1, a1 = 0, a2 = 1 the print ("%s cycles:"%depth,a1,a2) = 1th Cycle: 0 1 *  $ 3. Set a condition here, only let it loop 3 times, then return the result of the loop 3 times, and will take out the last return value (first in hand)Panax Notoginseng if depth = = 3: - return A1,A2 the Results 3 times: 0 1 (first time) + 1 1 (second time) A 1 2 (third time) the  + 4. Assign a variable a3 to equal A1 plus A2 and, for the first time A1 = 0, a2 = 1 so a3 = 1 - a3 = A1 + a2 ===> 1 = 0 + 1 $  $ 5. When the program executes to see a variable R, the contents of the variable can see the name F1, this is the name of the function, in this meaning is called the function itself, this is recursion! - F1 (Depth +1, A2, A3) This code means to call itself, depth +1, A2, A3 into the parameters of the function itself. - so the current R = F1 (depth +1, A2, A3) ===> r = F1 (2, 1, 1) the when we get the value of this variable R, I go down to see ' return R ', and return value will take the value just obtained from the R variable as the argument of the function and re-pass it into the parameter of the function. - This is where the second cycle begins! So the way the second cycle starts is:Wuyi  the start for the first time -  Wu def F1 (depth, a1, A2): Def F1 (1, 0, 1): - print ("%s times loop:"%depth,a1,a2) print ("%s cycles:"%depth,0,1) About if depth = = 1:if Depth = = 1: #循环一次, meet the conditions $ return A1,A2 return 0,1 #第一次返回的值 - a3 = a1 + A2 1 = 0 + 1 - r = F1 (depth +1, A2, a3) R = F1 (2, 1, 1) #第二次开始的地方 - return R return r #把值返回到函数中 A  + ret = F1 (1,0,1) ret = F1 (1,0,1) the print (ret) print (ret) -  $ second time the  the def F1 (2, 1, 1): the print ("%s times loop:"%depth,1,1) the if depth = = 2: #第二次循, meet the conditions - return to #第二次返回的值 in 2 = 1 + 1 nth time ... the r = F1 (3, 1, 2) #第三次开始的地方 the return R #把值传入到函数中 About  the ret = f1 (1,0,1) the print (ret) the " "

The results obtained from the above:

1 /library/frameworks/python.framework/versions/3.5/bin/python3.5/users/zk/pycharmprojects/old_boy/ day07/ recursive function. py2 1th cycle: 0 13 2nd cycle: 1 1 4 3rd Cycles : 1 25 (1, 2)6  7 Process finished with exit code 0

Python Basics "Eighth article": Anatomy of Recursive functions

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.