Start of change

Invoking user-defined functions

User-defined functions may be invoked in most places that intrinsic functions may be invoked.

To invoke user-defined functions without the FUNCTION keyword, specify the function name in the REPOSITORY paragraph. For details, see REPOSITORY paragraph in the Enterprise COBOL for z/OS® Language Reference.

The following example shows a compilation group containing a user-defined function definition called "docalc" and a program containing the function invocation:
   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 docalc.
   Data division.
    Working-storage section.
     1 result pic 999v999 usage display.
   Procedure division.
       compute result = docalc("add" 10 0.23)
       display "hello from mainprog, result=" result
       goback.
   End program 'mainprog'.
After running this program, the output is as follows:
hello from mainprog, result=010230
End of change