引数 BY CONTENT をユーザー定義関数に渡します
ユーザー定義関数定義の仮パラメーターはBY REFERENCEまたはBY VALUEとしてのみ指定できますが、CONTENT-OF組み込み関数を使用して、ユーザー定義関数BY CONTENTに引数を効果的に渡すことができます。 これを行うには、関数定義で仮パラメーターをBY REFERENCEとして指定する必要があります。 ユーザー定義関数呼び出しでは、CONTENT-OF組み込み関数を使用して、効果的に渡されるようにしたい各引数をラップしますBY CONTENT。
このタスクについて
以下の関数定義の例と、関数の呼び出しを含むプログラムを参照してください。
Identification division.
Function-id. docalc.
Data division.
Linkage section.
1 kind pic x(3).
1 argA pic 999.
1 argB pic v999.
1 res pic 999v999.
Procedure division
using by reference kind argA argB
returning res.
if kind equal "add" then
compute res = argA + argB
end-if
goback.
End function docalc.
Identification division.
Program-id. 'mainprog'.
Environment division.
Configuration section.
Repository.
function content-of intrinsic
function docalc.
Data division.
Working-storage section.
1 result pic 999v999 usage display.
1 arg-1 pic x(3).
1 arg-2 pic 999.
1 arg-3 pic v999.
Procedure division.
move "add" to arg-1
move 10 to arg-2
move 0.23 to arg-3
* arg-1 is effectively BY CONTENT
* arg-2 is BY REFERENCE
* arg-3 is BY REFERENCE
compute result = docalc(content-of(arg-1) arg-2 arg-3)
display "hello from mainprog, result=" result
goback.
End program 'mainprog'.CONTENT-OF組み込み関数は、入力引数の一時コピーを作成します。 その後、一時コピーがBY REFERENCEユーザー定義関数に提供され、ユーザー定義関数定義内の仮パラメーターに対する変更は、効果的に一時コピーに対して行われます。