Python recursive functions in a detailed

Source: Internet
Author: User

Python Recursive Functions detailed

A function that calls the current function itself within a function is a recursive function

The following is an example of a recursive function:

The first time a person touches a recursive function, it gets disoriented by the call itself, and looking at the function call above, the result will be:

Why did you come up with the above results? Because all of the code behind the calling function itself is forgotten, which is the Python code after the else.

The actual output of this recursive function is the following result:

I believe you see here are a little bit, small, the same, I see this recursive function for the first time, can only understand the first result. That's because, when most people are doing things, break the first thing, be arranged to do the second thing, will forget the first thing follow-up things, if in doing the second thing, and interrupted, be arranged to do the third thing, will be the first, the second to do the follow-up things to forget ..., This is why the recursive function is not understood.

Here I have done a decomposition of this recursive function, I hope to help everyone.

To explain the recursive function, when calling the recursive function Digui (3), the following procedure is performed:

when the function itself is called, the code after it does not end, but waits for the condition to be False, and then after executing the code, the same color of the print () statement waits for the corresponding color function.

Python recursive functions in a detailed

Related Article

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.