Ending and reentering main programs or subprograms

Whether a program is left in its last-used state or its initial state, and to which caller it returns, can depend on the termination statements that you use.

You can use any of three termination statements in a program, but they have different effects as shown in the following table.

Table 1. Effects of termination statements
Termination statement Main program Subprogram
EXIT PROGRAM No action taken Return to calling program without ending the run unit. An implicit EXIT PROGRAM statement is generated if the called program has no next executable statement.

In a threaded environment, the thread is not terminated unless the program is the first (oldest) one in the thread.

STOP RUN Return to calling program.1 (Might be the operating system, and application will end.)

STOP RUN terminates the run unit, and deletes all dynamically called programs in the run unit and all programs link-edited with them. (It does not delete the main program.)

In a threaded environment, the entire Language Environment® enclave is terminated, including all threads running within the enclave.

Return directly to the program that called the main program.1 (Might be the operating system, and application will end.)

STOP RUN terminates the run unit, and deletes all dynamically called programs in the run unit and all programs link-edited with them. (It does not delete the main program.)

In a threaded environment, the entire Language Environment enclave is terminated, including all threads running within the enclave.

GOBACK Return to calling program.1 (Might be the operating system, and application will end.)

GOBACK terminates the run unit, and deletes all dynamically called programs in the run unit and all programs link-edited with them. (It does not delete the main program.)

In a threaded environment, the thread is terminated.2

Return to calling program.

In a threaded environment, if the program is the first program in a thread, the thread is terminated.2

  1. If the main program is called by a program written in another language that does not follow Language Environment linkage conventions, return is to this calling program.
  2. If the thread is the initial thread of execution in an enclave, the enclave is terminated.

A subprogram is usually left in its last-used state when it terminates with EXIT PROGRAM or GOBACK. The next time the subprogram is called in the run unit, its internal values are as they were left, except that return values for PERFORM statements are reset to their initial values. (In contrast, a main program is initialized each time it is called.)

There are some cases in which programs will be in their initial state:

  • A subprogram that is dynamically called and then canceled will be in the initial state the next time it is called.
  • A program that has the INITIAL clause in the PROGRAM-ID paragraph will be in the initial state each time it is called.
  • Data items defined in the LOCAL-STORAGE SECTION will be reset to the initial state specified by their VALUE clauses each time the program is called.

related concepts  
Comparison of WORKING-STORAGE and LOCAL-STORAGE  
Language Environment Programming Guide (What happens during termination:
   thread termination)