Python note seven (recursive function)

Source: Internet
Author: User

Before we introduce recursive functions, we first introduce the following characteristics of the use of the following recursive functions:

1. A recursive function is a function that calls itself inside a function body

2. The recursive function of the operation of the scale to be reduced, so that can be calculated

3. The number of recursive layers do not exceed 999, because the function call is implemented through a stack (stack) of this data structure, whenever entering a function call, the stack will add a stack of frames, whenever the function returns, the stack will be reduced by a stack of frames. Too many calls to the layer will cause the stack to overflow.

4. Theoretically all recursive functions can be represented by loops, but recursion is more visually understandable (although I'm more accustomed to using loops ~ ~ ~ ~ ~ ~)

def factorial (n): ...      if n ==1: ...          return 1      ... Else :...          return n*factorial (n-1) ... >>> factorial (5)120

The above code we use the loop to achieve ~~~~~

def factorial (n): ...    FAC=1     ...  while n>1:        ... FAC=fac*n        ... n-=1     ... return FAC>>> factorial (5)120

Python note seven (recursive function)

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.