組み込み関数の例

このプログラム例 intrins.cbl では、プログラムで組み込み関数を使用する方法をいくつか説明します。

注記:

組み込み関数には、整数型、数字型、および英数字型の 3 種類があります。次のサンプルで使用されている FACTORIAL 関数は、数字型関数です。ここでのコーディング方法は、他の種類の関数に適用されない場合があります。

  1$set mf noosvs ans85
  2
  3*************************************************************
  4* Copyright Micro Focus Limited 1991. All Rights Reserved.  *
  5* This demonstration program is provided for use by users   *
  6* of Micro Focus products and may be used, modified and     *
  7* distributed as part of your application provided that you *
  8* properly acknowledge the copyright of Micro Focus in this *
  9* material.                                                 *
 10*************************************************************
 11
 12*************************************************************
 13*                                                           *
 14*                     INTRINS.CBL                           *
 15*                                                           *
 16*    This program demonstrates some of the ways you can     *
 17*    use Intrinsic Functions in your COBOL application.     *
 18*    This program uses the FACTORIAL Intrinsic Function     *
 19*    to illustrate the following capabilities:              *
 20*                                                           *
 21*    1) Data item is assigned the value of a function       *
 22*    2) Function is used as a data item in an EVALUATE      *
 23*           statement                                       *
 24*    3) Function is used as a data item in an IF            *
 25*           statement                                       *
 26*    4) Function uses an array element (fixed index) as     *
 27*           an argument                                     *
 28*    5) Function uses an array element (variable index)     *
 29*           as an argument                                  *
 30*    6) Data item is assigned the value of a function of    *
 31*           a function                                      *
 32*    7) Data item, assigned the value of the function,      *
 33*           is used in a COMPUTE statement                  *
 34*    8) Data item is assigned the value of the sum of       *
 35*           two functions                                   *
 36*    9) Function is used in the UNTIL condition of a        *
 37*           PERFORM ... UNTIL statement                     *
 38*                                                           *
 39*                                                           *
 40*    To familiarize yourself with the Intrinsic function    *
 41*    syntax, try running INTRINS under Debugger.            *
 42*                                                           *
 43*    Compile the program using:                             *
 44*                                                           *
 45*           COBOL INTRINS ANIM;                             *
 46*                                                           *
 47*    then debug the program:                                *
 48*                                                           *
 49*           ANIMATE INTRINS                                 *
 50*                                                           *
 51*                                                           *
 52*                                                           *
 53*                                                           *
 54*                                                           *
 55*************************************************************
 56 working-storage section.
 57 78 fals value 0.
 58 78 tru  value 1.
 59
 60 01 true-or-false               pic 9(1).
 61
 62 01 factor                      pic s9(10).
 63
 64 01 val                         pic s9(10).
 65
 66 01 indx                        pic 9(5) comp-x.
 67
 68 01 arg                         pic 9(2) comp-x  value 5.
 69
 70 01 arr                                         value "40537".
 71     03 elem   occurs 5 times   pic 9.
 72
 73 procedure division.
 74
 75 main-section.
 76
 77************************************************************
 78* Form 1 - Data item is assigned the value of the function *
 79************************************************************
 80
 81     compute factor = function factorial(0)
 82
 83************************************************************
 84* Form 2 - Function is used as a data item in an EVALUATE  *
 85*          statement                                       *
 86************************************************************
 87
 88     evaluate function integer(6.5)
 89      when 6
 90         move tru to true-or-false
 91      when other
 92         move fals to true-or-false
 93     end-evaluate
 94
 95************************************************************
 96* Form 3 - Function is used as a data item in an IF        *
 97*          statement                                       *
 98************************************************************
 99
100     if function integer (function factorial(arg)) = 120 then
101         move tru to true-or-false
102     else
103         move fals to true-or-false
104     end-if
105
106  ************************************************************
107  * Form 4 - Function uses an array element (fixed index) as *
108  *          an argument                                     *
109  ************************************************************
110
111     compute factor = function factorial(elem(4))
112
113  ************************************************************
114  * Form 5 - Function uses an array element (variable index) *
115  *          as an argument                                  *
116  ************************************************************
117
118       move 4 to indx
119       compute factor = function factorial(elem(indx))
120
121  ************************************************************
122  * Form 6 - Data item is assigned the value of a function   *
123  *          of a function                                   *
124  ************************************************************
125
126     compute factor = function factorial(
127                        function factorial(3))
128
129  ************************************************************
130  * Form 7 - Data item, assigned the value of the function,  *
131  *          is used in a COMPUTE statement                  *
132  ************************************************************
133
134     compute val = function factorial(3) + 5
135
136  ************************************************************
137  * Form 8 - Data item is assigned the value of the sum of   *
138  *          two functions                                   *
139  ************************************************************
140
141     compute val = function factorial(3) +
142                   function factorial(5)
143
144  ************************************************************
145  * Form 9 - Function is used in the UNTIL condition of a    *
146  *          PERFORM ... UNTIL statement                     *
147  ************************************************************ 
148
149     move 1 to indx
150     perform para-1 until function integer (function 
151           factorial(indx))  = 120
152     stop run.
153
154 para-1.
155     compute indx = indx + 1.
注記:
  • 81 行目
     compute factor = function factorial(0) 

    関数の値がデータ項目に割り当てられます。

  • 88~93 行目
     evaluate function integer(6.5)
      when 6
         move tru to true-or-false
      when other
         move fals to true-or-false
     end-evaluate

    関数が EVALUATE 文のデータ項目として使用されています。

  • 100~104 行目
     if function integer (function factorial(arg)) = 120 then
         move tru to true-or-false
     else
         move fals to true-or-false
     end-if

    関数が IF 文のデータ項目として使用されています。

    数字型関数の結果は浮動小数点型であり、正確な整数値は期待できません。この例では、integer 関数を使用して、IF 文に使用する整数を取得しています。

  • 111 行目
     compute factor = function factorial(elem(4))

    関数の引数として配列要素(固定索引)を使用しています。

  • 118~119 行目
     move 4 to indx
     compute factor = function factorial(elem(indx))

    関数の引数として配列要素(可変索引)を使用しています。

  • 126~127 行目
     compute factor = function factorial (function factorial(3)) 

    関数値を引数として関数に渡し、その戻り値をデータ項目に入れています。

  • 134 行目
     compute val = function factorial(3) + 5 

    関数値を入れたデータ項目を COMPUTE 文で使用しています。

  • 141~142 行目
     compute val = function factorial(3) + function factorial(5) 

    2 つの関数値を合計し、その結果をデータ項目に入れています。

  • 149~155 行目
        move 1 to indx
        perform para-1 until function integer (function
           factorial(indx)) = 120
      stop run.
    
     para-1.
        compute indx = indx + 1.

    関数を PERFORM ... UNTIL 文の UNTIL 指定の条件として使用しています。