2017 Winter Vacation 0 The recursive function of the function of the basic Learning Python series

Source: Internet
Author: User

What is a recursive function?
Inside the function, you can also continue to call other functions, if a function calls itself internally, this function is a recursive function
Give an example of a factorial that asks N:

def fact (n): if n = = 1:return 1; Else : return N * Fact (n-1)Print fact (5)>>>120

The above is a recursive function

The procedure for running this function is as follows:

===> fact (5)===> 5 * FACT (4)===> 5 * (4 * FACT (3))===> 5 * (4 * (3 * FACT (2))) /c4>===> 5 * (4 * (3 * (2 * FACT (1))) ===> 5 * (4 * (3 * (2 * 1)))===> 5 * (4 * (3 * 2)) ===> 5 * (4 * 6)===> 5 * 24===> 120

Note: However, the use of recursive functions need to prevent stack overflow, function calls through the stack (stack) This data structure to achieve, whenever entering a function call, the stack will add a stack of frames, whenever the function returns, will reduce the stack frame, because the size of the stack is not infinite, so, Excessive number of recursive calls can cause stack overflow

2017 Winter Vacation 0 The recursive function of the function of the basic Learning Python series

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.