LOTUSSCRIPT 言語
引数を参照渡しするときは、メモリ内の値へのポインタが渡されます。関数は引数を処理します。関数が参照によって渡された値を変更すると、元の値も変更されます。
引数を値渡しするときは、メモリ内のコピーが渡されます。関数はコピーで処理を行います。関数が値によって渡された値を変更すると、その効果は関数に対してローカルです。コピーは変更されますが、メモリ内の元の値は影響を受けません。
関数やサブルーチンで変更される変数は、処理が終わると変更された値を保持します。
値渡し 以下の処理ができます。
関数やサブルーチンを呼び出すとき、引数は常に値渡しされます。
関数やサブルーチンを呼び出すときに、引数を参照渡しにするか値渡しにするかを使い分けられます。
変数引数が関数やサブルーチンによって変更されても、処理が終わった後も元の値を保持します。関数やサブルーチンは渡された変数のコピーだけを処理するので、変数自体は変化しません。
例 例 1 ' Define a function FOver with three Integer parameters: ' a variable, an array variable, and a list variable. Function FOver(a As Integer, b() As Integer, c List As Integer) ' ... End Function
Dim x As Integer Dim y(5) As Integer Dim z List As Integer
' Call the function FOver correctly, with arguments ' whose types match the types of the declared parameters. Call FOver(x, y, z)
例 2 ' Define a function GLevel with one Integer list parameter. Function GLevel (b List As Integer) ' ... End Function
Dim z List As Integer
' Call the function GLevel incorrectly, passing a list ' argument by value. Call GLevel ((z)) ' Output: ' Error:Illegal pass by value:Z ' A list argument cannot be passed by value.
例 3 ' Define a function FExpr with two Integer parameters; ' the second must always be passed by value. Function FExpr(a As Integer, ByVal b As Integer) ' ... End Function
Dim x As Integer, w As Integer Dim y(5) As Integer Dim z List As Integer
' Call the function FExpr correctly with two Integer ' arguments:a constant and a variable. Call FExpr(TRUE, x) ' Both arguments are passed by value: ' the first, TRUE, because it is a constant; ' and the second, x, because of the ByVal declaration ' in FExpr.
' The following call produces two error messages: Call FExpr(TRUE, y) ' Output: ' Error:Illegal pass by value:Y ' Error:Type mismatch on:Y ' Because Y is an array variable, it is an illegal argument to ' pass by value and its type does not match the declared ' parameter type.
例 4 ' When a function modifies one of its parameters, ' the argument value is changed after the function returns ' if the argument was passed by reference.The value is not ' changed if the argument was passed by value.
Function FTRefOrVal(a As Integer) As Integer FTRefOrVal = a + 1 a = a + 5 End Function
Dim x As Integer, y As Integer
' Show results of passing argument by reference. Print x, FTRefOrVal(x), x ' Output: ' 0 1 5 ' The value of x was changed from 0 to 5 in FTRefOrVal.
' Show results of calling with argument by value ' (note the extra parentheses around y%). Print y, FTRefOrVal((y)), y ' Output: ' 0 1 0 ' The value of the copy of y was changed from 0 to 5 ' in FTRefOrVal.The value of y is unchanged.
関連項目