LOTUSSCRIPT 言語
引数をとらない関数を実行する
引数のない関数をステートメントに含めて呼び出す場合、関数名の後ろに空のカッコを付けても付けなくても構いません。
以下に例を示します。
Dim anInt As Integer Dim aDouble As Double Function Cubit1 As Double ' Return the cube of anInt% and display a message ' saying what that value is. Cubit1# = anInt% ^ 3 Print anInt% & " cubed = " & Cubit1# & "." End Function anInt% = 4 aDouble# = Cubit1# ' Output:4 cubed is 64. aDouble# = Cubit1# ' Output:4 cubed is 64. Print aDouble# ' Output:64 Print Cubit1# ' Output:4 cubed is 64. 64
関数名だけを入力して、パラメータのない関数を呼び出すことができます。この場合、空のカッコを付けることはできません。
Cubit1# ' Output:4 cubed is 64
単一の引数をとる関数を実行する
単一の引数をとる関数を呼び出す場合、関数をステートメントに含めるときに引数をカッコで囲む必要があります。
Dim anInt As Integer Dim aDouble As Double Function Cubit2(X As Integer) As Double ' Return the cube of X% and display a message ' saying what that value is. Cubit2# = X% ^ 3 Print X% & " cubed = " & Cubit2# & "." End Function anInt% = 4 aDouble# = Cubit2#(anInt%) ' Output:4 cubed is 64. Print aDouble# ' Output:64 Print Cubit2#(anInt%) ' Output:4 cubed is 64. 64
以下のいずれかの方法で、パラメータが 1 つの関数を呼び出すことができます。
Call Cubit2#(anInt%) ' Output:4 cubed is 64. (anInt% is passed by reference.) Cubit2# anInt% ' Output:4 cubed is 64. (anInt% is passed by reference.) Cubit2#(anInt%) ' Output:4 cubed is 64. (anInt% is passed by value.)
複数の引数をとる関数を実行する
複数の引数を持つ関数を呼び出す場合、関数をステートメントに含めるときに、引数をカッコで囲む必要があります。
Dim anotherInt As Integer Function Cubit3(X As Integer, Y As Integer) As Double ' Return the product of X% and Y%. Cubit3# = X% * Y% Print X% & " times " Y% & " = " & Cubit3# & "." End Function anInt% = 4 aDouble# = Cubit2#(anInt%) ' Output:4 times 6 = 24. 24
複数の引数を求める関数を呼び出すこともできます。これには、Call ステートメントを使用するか、または関数名、引数の順に入力します。Call ステートメントにはカッコを付ける必要があります。関数名自体にはカッコは付けられません。
Call Cubit3#(anInt%, anotherInt%) ' Output:4 times 6 = 24. Cubit3# anInt%, anotherInt% ' Output:4 times 6 = 24.
関数を再帰的に実行する
再帰関数とは、それ自身を呼び出す関数です。関数の中からそれ自身を呼び出すことを、再帰呼び出しといいます。
再帰関数の定義には、再帰を終了する手段が必要です。
再帰の深さは、32K バイトのスタックの大きさによって制限されます。
引数のない関数を再帰的に呼び出す場合、関数の戻り値を使用するのであれば、呼び出しの中で関数名の後ろに空のカッコを入れておく必要があります。カッコは、関数が呼び出されることを示します。カッコのない関数名は、関数の戻り値を表す変数と解釈されます。
例 1
Function Facto# (theNum%) ' Calculate theNum% factorial and make it ' the return value of Facto#. If theNum% <= 0 Then Facto# = 0 ElseIf theNum% = 1 Then Facto# = 1 Else Facto# = theNum% * Facto#(theNum% -1) End If End Function
例 2
これは、引数のない再帰関数を示しています。
Function Recurse As Integer ' ... ' Call Recurse and assign the return value to x. x = Recurse() ' ... ' Assign the current value of the Recurse variable to x. x = Recurse ' ... End Function
関連項目