RETURN (回到呼叫端)

代碼 因素1 擴充因素2
RETURN (H M/R)   表示式

RETURN 作業會傳回呼叫程式。 如果將值傳回給呼叫程式,則會在 表示式 運算元中指定回覆值。

由於 RETURN 作業而發生的動作會因作業是在 cycle-main 程序或子程序中而有所不同。 當傳回 cycle-main 程序時,會發生下列情況:
  1. 已檢查中止指示器。 如果已開啟中止指示器,則程序會異常結束。 (所有開啟的檔案都會關閉,錯誤回覆碼會設為向呼叫常式指出程序已異常結束,且控制會回到呼叫常式。)
  2. 如果沒有開啟中止指示器,則會檢查 LR 指示器。 如果 LR 開啟,則程式會正常結束。 (寫入已鎖定的資料區結構、陣列及表格,並重設外部指示器。)
  3. 如果沒有開啟中止指示器,且 LR 未開啟,則程序會返回呼叫常式。 下次執行程序時,會保留資料。 不會寫出檔案和資料區。 如需在 *NEW 啟動群組中執行如何影響 RETURN 作業的相關資訊,請參閱 Rational® Development Studio for i: ILE RPG Programmer 's Guide 中有關呼叫程式和程序的章節。

當子程序傳回時,如果在被呼叫程式或程序的原型上指定,則會將回覆值傳遞給呼叫程式。 自動檔案已關閉。 不會自動發生任何其他動作。 必須手動關閉所有靜態或廣域檔案及資料區。 您可以設定 LR 之類的指示器,但這不會導致程式終止。

如需如何使用作業延伸器 H、M 及 R 的相關資訊,請參閱 數值運算的精準度規則

在傳回值的子程序中, RETURN 作業必須在子程序內編碼。 實際回覆值具有與 EVAL 表示式左側相同的角色,而 RETURN 作業的延伸因數 2 具有與右側相同的角色。 只有在原型已將回覆值定義為陣列時,才會傳回陣列。

注意!

如果子程序傳回值,您應該確保在到達程序結尾之前執行 RETURN 作業。 如果子程序在沒有遇到 RETURN 作業的情況下結束,則會向呼叫程式發出異常信號。

效能提示

在原型上指定 RTNPARM 關鍵字,可大幅增進傳回大量值的效能。 如需相關資訊,請參閱 RTNPARM

如需相關資訊,請參閱 呼叫作業

圖 1. 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