RETURN (呼び出し元への戻し)

自由形式構文 RETURN{(HMR)}
コード 演算項目 1 拡張演算項目 2
RETURN (H M/R)

RETURN 命令は呼び出し元へ戻します。 呼び出し元に値が戻されると、 オペランドに戻り値が示されます。

RETURN 命令の結果として行われる処置は、命令がサブプロシージャー内にあ るかどうかによって異なります。 プログラムまたはメイン・プロシージャーが戻すと、次のことが行われます。

  1. 停止標識が検査されます。 停止標識がオンになっている場合には、プロシージャーは異常終了 します (オープンされているファイルはすべてクローズされて、呼び出し元ルーチンに プロシージャーが異常終了したことを示すエラー戻りコードが設定され、呼び出し元ルーチンに制御が戻されます)。
  2. 停止標識がオンになっていない場合には、LR 標識が検査されます。LR がオンになっている場合には、プログラムは正常に 終了します (ロックされたデータ域構造、配列、およびテーブルが書き出されて、外部標 識がリセットされます)。
  3. 停止標識がオンでなく LR がオンもなっていない場合には、プロシージャ ーは呼び出し元ルーチンに戻します。 データは次回のプロシージャーの実行に備えて保存されます。 ファイルおよびデータ域は書き出されません。 *NEW 活動化グループでの 実行が RETURN の操作にどのような影響を与えるかについて は、「WebSphere Development Studio: ILE RPG プログラマーの手引き」の呼び出し元のプログラムおよびプロシージャー の章を参照してください。

サブプロシージャーが戻すと、戻り値 (呼び出されたプログラムまたはプロシ ージャーのプロトタイプで指定された場合) が呼び出し元に渡されます。 自動的には何も行われません。 ファイルおよびデータ域はすべて手操作でクローズしなければなりません。 LR などの標識を設定できますが、 これでプログラムの終了を行うことはできません。 命令拡張 H、M、および R がどのように使用されるかについて は、数値演算の精度の規則を参照してください。

値を戻すサブプロシージャーでは、RETURN 命令はそのサブプロシージャー内で コーディングしなければなりません。 実際に戻された値は EVAL 式の左側と同じ働きをし、RETURN 命令の拡張演算項目 2 は右側と同じ働きをします。 配列を戻すことができるのは、プロトタイプが戻り値を配列として定義してい る場合だけです。

注意!

サブプロシージャーが値を戻した場合には、プロシージャーの終わりに達する 前に RETURN 命令が実行されていることを確認してください。 サブプロシージャーが RETURN 命令を 見付けないで終了している場合には、呼び出し元に例外が通知されます。

詳細については、呼び出し命令を参照してください。

図 346. RETURN 命令の例
      * This is the prototype for subprocedure RETNONE. Since the
      * prototype specification does not have a data type, this
      * subprocedure does not return a value.
     D RetNone         PR
      * This is the prototype for subprocedure RETFLD. Since the
      * prototype specification has the type 5P 2, this subprocedure
      * returns a packed value with 5 digits and 2 decimals.
      * The subprocedure has a 5-digit integer parameter, PARM,
      * passed by reference.
     D RetFld          PR             5P 2
     D   Parm                         5I 0
      * This is the prototype for subprocedure RETARR. The data
      * type entries for the prototype specification show that
      * this subprocedure returns a date array with 3 elements.
      * The dates are in *YMD/ format.
     D RetArr          PR              D   DIM(3) DATFMT(*YMD/)
      * This procedure (P) specification indicates the beginning of
      * subprocedure RETNONE. The data specification (D) specification
      * immediately following is the procedure-interface
      * specification for this subprocedure. Note that the
      * procedure interface is the same as the prototype except for
      * the definition type (PI vs PR).
     P RetNone         B
     D RetNone         PI
      * RetNone does not return a value, so the RETURN
      * operation does not have factor 2 specified.
     C                   RETURN
     P RetNone         E
      * The following 3 specifications contain the beginning of
      * the subprocedure RETFLD as well as its procedure interface.
     P RetFld          B
     D RetFld          PI             5P 2
     D   Parm                         5I 0
     D Fld             S             12S 1 INZ(13.8)
      * RetFld returns a numeric value.  The following RETURN
      * operations show returning a literal, an expression and a
      * variable. Note that the variable is not exactly the same
      * format or length as the actual return value.
     C                   RETURN       7
     C                   RETURN       Parm * 15
     C                   RETURN       Fld
     P RetFld          E
      * The following 3 specifications contain the beginning of the
      * subprocedure RETARR as well as its procedure interface.
     P RetArr          B
     D RetArr          PI              D   DIM(3)
     D SmallArr        S               D   DIM(2) DATFMT(*ISO)
     D BigArr          S               D   DIM(4) DATFMT(*USA)
      * RetArr returns a date array.  Note that the date
      * format of the value specified on the RETURN operation
      * does not have to be the same as the defined return
      * value.
      * The following RETURN operation specifies a literal.
      * The caller receives an array with the value of the
      * literal in every element of the array.
     C                   RETURN        D'1995-06-27'
      * The following return operation returns an array
      * with a smaller dimension than the actual return value.
      * In this case, the third element would be set to the
      * default value for the array.
     C                   RETURN        SmallArr
      * The following return operation returns an array
      * with a larger dimension than the actual return
      * value.  In this case, the fourth element of BigArr
      * would be ignored.
     C                   RETURN        BigArr
     P RetArr          E