0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

再帰的(リカーシブ)プログラム

Posted at

再帰的(リカーシブ)プログラム

再帰とは、プログラムを実行中に自分自身を呼び出して実行しても、正しい結果を返すことができるプログラム構造

再帰の実現手法

この再帰的処理のためには、自分自身を呼び出すごとに局所変数(ローカル変数)のセットを一新し、以前のものとは独立させる必要がある。そのため、自分自身を呼び出す前に局所変数の内容をスタックに退避し、呼び出したプログラムから戻ったとき、スタックから取り出す、LIFO(Last-In First-Out)方式による処理が必要になる。

出典

気づき

こういう処理にもLIFOが使われているのか。

問題

proc(n)
 n=0 ならば戻る
 そうでなければ
 {
  nを印字する
  proc(n-1)を呼び出す
  nを印字する
 }
 を実行して戻る

気づき

戻るとはプログラムの先頭に戻ることをさしていたのか。
知らなかった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?