Difference between revisions of "Basic Pascal Tutorial/Chapter 4/Recursion/zh CN"

From Free Pascal wiki
Jump to navigationJump to search
m (Kai Burghardt moved page Recursion/zh CN to Basic Pascal Tutorial/Chapter 4/Recursion/zh CN: tidy up main name space: create subpage hierarchy for basic Pascal tutorial [cf. [[Special: PermaLink/14...)
m (bypass language bar/categorization template redirect [cf. discussion])
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Basic Pascal Tutorial/Chapter 4/Recursion}}
4E - 递归调用 (原作者: Tao Yue, 状态: 未更改)
4E - 递归调用 (原作者: Tao Yue, 状态: 未更改)
Line 42: Line 42:
{|style=color-backgroud="white" cellspacing="20"
{|style=color-backgroud="white" cellspacing="20"
|[[Basic Pascal Tutorial/Chapter 4/Scope/zh_CN|上一页]]
|[[Basic Pascal Tutorial/Contents/zh CN|目录]]
|[[Basic Pascal Tutorial/Chapter 4/Forward Referencing/zh_CN|下一页]]

Latest revision as of 16:20, 20 August 2022

български (bg) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

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




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


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.




上一页 目录 下一页