Returning from a Subprogram

To return control from a subprogram, the subprogram may end with an EXIT PROGRAM, a GOBACK, or a STOP RUN statement. If the subprogram ends with an EXIT PROGRAM or a GOBACK statement, control returns to its immediate caller without ending the run unit. An implicit EXIT PROGRAM statement is generated if there is no next executable statement in a called program. If the subprogram ends with a STOP RUN statement, all programs in the run unit up to the nearest control boundary are ended, and control returns to the program prior to the control boundary.

A subprogram is usually left in its last-used state when it ends with EXIT PROGRAM or GOBACK. The next time it is called in the run unit, its internal values will be as they were left, except that all PERFORM statements are considered to be complete and will be reset to their initial values. In contrast, a main program is initialized each time it is called. There are two exceptions:
  • A subprogram that is dynamically called and then canceled will be in the initial state the next time it is called.
  • A program, which has the INITIAL clause specified in its PROGRAM-ID paragraph, will be in the initial state each time it is called.