ユーザ定義関数
第 1 章では プロシージャについて、この章では ObjectScript が提供する内部関数について学習してきました。内部関数は、ユーザ自身が記述するユーザ定義関数と異なり、システム定義関数と呼ばれます。結果を返すプロシージャとして機能します。関数では、Quit コマンドの引数として結果を返すように値を特定します。
ユーザ定義関数はシステム定義関数のように使用しますが、関数名の前に $$ を使用する点が異なります。
SAMPLES>do ^funcexample
Enter a number: 4
The factorial of 4 is 24
SAMPLES>do ^funcexample
Enter a number: 5
The factorial of 5 is 120
SAMPLES>write $$fact^funcexample(6) ; call it directly
720
SAMPLES>
以下は、funcexample.mac コードです。
funcexample ; example of user-defined function
read !, "Enter a number: ", num
set fact = $$fact( num )
write !, "The factorial of ", num, " is ", fact
quit
fact(number) PUBLIC
; compute factorial
{
if number < 1 {quit "Error!"}
if (number = 1) || (number = 2) {quit number}
set x = number * $$fact( number - 1 )
quit x
}