Normal End for a Cycle-Main Procedure
A cycle-main procedure ends normally and control returns
to the calling procedure when the LR indicator is on and the H1 through H9
indicators are not on. The LR indicator can be set on:
- implicitly, as when the last record is processed from a primary or secondary file during the RPG program cycle
- explicitly, as when you set LR on.
A cycle-main procedure also ends normally if:
- The RETURN operation (with a blank factor 2) is processed, the H1 through H9 indicators are not on, and the LR indicator is on.
- The RT indicator is on, the H1 through H9 indicators are not on, and the LR indicator is on.
When a cycle-main procedure ends normally, the following
occurs:
- The Factor-2-to-Result-field move of a *ENTRY PARM operation is performed.
- All arrays and tables with a 'To file name' specified on the Definition specifications, and all locked data area data structures are written out.
- Any data areas locked by the procedure are unlocked.
- All global files that are open are closed.
- A return code is set to indicate to the caller that the procedure has ended normally, and control then returns to the caller.
On the next call to the cycle-main procedure, with the
exception of exported variables, a fresh copy is available for processing.
(Exported variables defined with the EXPORT keyword.
They are initialized only once, when the program is first activated
in an activation group. They retain their last assigned value on a new call,
even if LR was on for the previous call. If you want to re-initialize them,
you have to reset them manually.) 
TIP
If you are accustomed to ending with LR on to cause storage to be released,
and you are running in a named (persistent) activation group, you may want
to consider returning without an end. The reasons are:
- The storage is not freed until the activation group ends so there is no storage advantage to ending with LR on.
- Call performance is improved if the program is not re-initialized for each call.
You would only want to do this if you did not need your program re-initialized each time.