Basic Pascal Tutorial/Chapter 4/Recursion/ja

This is an old revision of Basic Pascal Tutorial/Chapter 4/Recursion/ja, edited by Derakun at . The current revision is 110342.

Created page with "{{Recursion/ja}} 4E - 再帰 (著者: Tao Yue, 状態: 原文のまま修正なし) '''Recursion''' means allowing a function or procedure to call itself until some limit is..."

Template:Recursion/ja

4E - 再帰 (著者: Tao Yue, 状態: 原文のまま修正なし)

Recursion means allowing a function or procedure to call itself until some limit is reached.

The summation function, designated by an uppercase letter sigma (Σ) in mathematics, can be written recursively:

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

Suppose you call Summation for 3.

a := Summation(3);

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 a test to stop the recursion, the base condition. Under all other conditions, the recursion should go deeper. If there is no base condition, the recursion will either not take place at all, or become infinite.

In the example above, the base condition was if num = 1.

previous contents next