Recursion I understand

Source: Internet
Author: User

Although I have read a few articles, I have summarized them.

First, call yourself

Second, the order is large to small.

Third: When the variable is 1, the recursive if (temp = 1) return 1 is introduced; that is, the exit must be set; otherwise, the loop will continue.

Fourth:

When a function calls itself, it allocates memory for the new local variables and parameters in the stack, and the function code re-runs with these variables and parameters. Recursive calls do not copy the function code again, but the parameters are new. When each recursive call returns, the old local variables and parameters are removed from the stack, and the function call point within the function is restarted. Recursive functions are described as "push and pull" for themselves ".

Fifth: recursive operation is slow and memory consumption

Sixth: dynamic planning can be effective without repeated operations

End

In imperative programming, we can often use loops instead of recursion to solve some problems, so as not to cause stack overflow due to data size. However, in functional programming, the only method to implement a "loop" is "recursion". Therefore, tail recursion and CPS are of great significance for functional programming. Understanding tail recursion is also very helpful for programming thinking. Therefore, you may wish to think more and practice it for your own use.

Reference: http://www.cnblogs.com/winter-cn/archive/2008/08/23/1274475.html

Http://msdn.microsoft.com/zh-cn/library/z3dk2cc3

Http://www.cnblogs.com/jeffreyzhao/archive/2009/04/01/tail-recursion-explanation.html

I understand it in theory, but it won't be used ~ Please advise

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.