Python Recursive functions

Source: Internet
Author: User

Inside a function, you can call other functions. If a function calls itself internally, the function is a recursive function.

For example, let's calculate factorial n! = 1 x 2 x 3 x ... x n , which is represented by a function fact(n) , and you can see:

Fact (n) = n! = 1 x 2 x 3 x ... x (n-1) x n = (n-1)! X n = fact (n-1) x n

Therefore, fact(n) can be expressed as n x fact(n-1) , only n=1 need special handling.

So, it fact(n) is written in a recursive way:

def fact (n):    if n==1:        return 1    return n * Fact (n-1) above is a recursive function. You can try:>>> Fact (1) 1>>> fact (5) 120>>> fact (100) 9332621544394415268169923885626670049071596826438162146859296389521759999322991560894146397615651828625369792082722375825 1185210916864000000000000000000000000L if we calculate fact (5), we can see the calculation process according to the function definition as follows:===> fact (5) ===> 5 * FACT (4) ===> 5 * (4 * FACT (3)) ===> 5 * (4 * (3 * FACT (2))) ===> 5 * (4 * (3) (2 * fact (1))) ===> 5 * (4 * (3 * (2 * 1)) ===> 5 * ( 4 * (3 * 2)) ===> 5 * (4 * 6) ===> 5 * 24===> 120 The advantage of a recursive function is that the definition is simple and logically clear. In theory, all recursive functions can be written in a circular way, but the logic of the loop is not as clear as recursion. The use of recursive functions requires careful prevention of stack overflow.

The method of solving recursive call stack overflow is optimized by tail recursion , in fact, the effect of tail recursion and loop is the same, so it is possible to think of the loop as a special tail recursive function.

Tail recursion means that when a function returns, it calls itself, and the return statement cannot contain an expression. In this way, the compiler or interpreter can optimize the tail recursion, so that the recursive itself, regardless of the number of calls, only occupy a stack frame, there is no stack overflow situation.

def fact (N):    return Fact_iter (n, 1) def fact_iter (num, product):    if num = = 1:        return product    return fact_ ITER (num-1, num * product) can see that return fact_iter (num-1, num * product) returns only the recursive function itself, and num-1 and num * Product are evaluated before the function call, without affecting function tuning Use. Fact (5) corresponds to the invocation of Fact_iter (5, 1) as follows:===> Fact_iter (5, 1) ===> Fact_iter (4, 5) ===> Fact_iter (3,) ===> Fact_iter (2) ===> Fact_iter (1, +) ===> 120 tail recursive call, if optimized, the stack will not grow, so no matter how many calls will not cause the stack overflow.

  

  

Python 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.