企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 参考资料 维基百科:[recursion](https://en.wikipedia.org/wiki/Recursion#In_computer_science) # 定义 > Recursion is when a function calls itself. Some programming languages(particularly functional programming languages like Scheme ,ML ,or Haskell) use recursion as a basic tool for implementing algorithms that in other languages would typically be expressed using iteration .Procedural languages like c tend to emphasize iteration over recursion ,but can support recursion as well. # Common Problems with recursion ### omitting the base case Suppose we leave out the if statement in printRangeRecursive: ``` void printRangeRecursiveBad(int start,int end) { printf("%d",start); printRangeRecursiveBad(start-1,end); } ``` This will still work,in a sense.**Our output will be a long string of numbers followed by a segmentation fault,when we blow out the stack.** ### Blowing out the stack Blowing out the stack is what happens when a recursion is too deep.One of the ways this can happen is if we forget the base case as above,but it can also happen if we just try to use recursion to do too much. For this reason,its bast to try to avoid linear recursion. Fortunately,linear recursions are often **tail-recursive** ,where the recursive call is the last thing th recursive function does;in this case,we can use a standard transformation to convert the tail-recurive function into an iterative function. # 汇编 # 最后递归 ``` #include <stdio.h> void f() { int a[5]; int i; for(i=0;i<6;i++) { printf("%d",i); a[i] -= 4; } } int main() { f(); } ```