知识卡片
递归需要缩小问题并终止
递归函数调用自身处理较小的子问题,并用基例停止继续调用。阶乘示例在非负整数范围内,把n的结果写成n乘以n−1的结果,在n为0时返回1。若输入范围或递减条件不满足,调用可能不终止;有限整数范围下还要考虑结果溢出,递归写法本身不保证任意输入安全。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Go Language Foundations · 查看原书依据
Go allows recursive function calls, in which functions call themselves. Used properly, recursion can be a very powerful tool that can be applied to many types of problems. The canonical example is the calculation of the factorial of a positive integer, the product of all positive integers less than or equal to n: func factorial(n
参考来源
- 位置:《Cloud Native Go》第03章《Go Language Foundations》(来源项目book-sources;源文件:extracted/Cloud Native Go (Matthew A. Titmus)/_epub-src/OEBPS/ch03.xhtml)
- 结论依据:递归函数调用自身处理较小的子问题,并用基例停止继续调用。原文相应定义、示例或限制为此结论提供依据;补充纠错与版本边界在正文就近标明。
- 原始内容:Go allows recursive function calls, in which functions call themselves. Used properly, recursion can be a very powerful tool that can be applied to many types of problems. The canonical example is the calculation of the factorial of a positive integer, the product of all positive integers less than or equal to n: func factorial(n