# Recursion/zh CN

English (en) français (fr) 日本語 (ja) 中文（中国大陆）‎ (zh_CN)

4E - 递归调用 (原作者: Tao Yue, 状态: 未更改)

Summation（求和）函数，数学中通过一个大写的Sigma]来指定。常见的递归例子：

function Summation (num : integer) : integer;
begin
if num = 1 then
Summation := 1
else
Summation := Summation(num-1) + num
end;

a := Summation(3);
• Summation(3) 变成 Summation(2) + 3
• Summation(2) 变成 Summation(1) + 2
• 为1时, 递归停止，变为 1
• Summation(2) 变成 1 + 2 = 3
• Summation(3) 变成 3 + 3 = 6
• a 为 6

Recursion works backward until a given point is reached at which an answer is defined, and then works forward with that definition, solving the other definitions which rely upon that one.

All recursive procedures/functions should have some sort of test so stop the recursion. Under one condition, called the base condition, the recursion should stop. Under all other conditions, the recursion should go deeper. In the example above, the base condition was if num = 1. If you don't build in a base condition, the recursion will either not take place at all, or become infinite.

 上一页 目录 下一页