Start of Content Area

Terminating Subroutines  Locate the document in its SAP Library structure

A subroutine normally ends at the ENDFORM statement. However, you can terminate them earlier by using the EXIT or CHECK statement. When you terminate a subroutine with EXIT or CHECK, the current values of the output parameters (CHANGING parameters passed by value) are passed back to the corresponding actual parameter.

Use EXIT to terminate a subroutine unconditionally. The calling program regains control at the statement following the PERFORM statement.

Example

PROGRAM FORM_TEST.

PERFORM TERMINATE.

WRITE 'The End'.

FORM TERMINATE.
   WRITE '1'.
   WRITE '2'.
   WRITE '3'.
   EXIT.
   WRITE '4'.
ENDFORM.

The produces the following output:

1 2 3 The End

In this example, the subroutine TERMINATE is terminated after the third WRITE statement.

Use CHECK to terminate a subroutine conditionally. If the logical expression in the CHECK statement is untrue, the subroutine is terminated, and the calling program resumes processing after the PERFORM statement.

Example

PROGRAM FORM_TEST.

DATA: NUM1 TYPE I,
NUM2 TYPE I,
RES TYPE P DECIMALS 2.

NUM1 = 3. NUM2 = 4.
PERFORM DIVIDE USING NUM1 NUM2 CHANGING RES.

NUM1 = 5. NUM2 = 0.
PERFORM DIVIDE USING NUM1 NUM2 CHANGING RES.

NUM1 = 2. NUM2 = 3.
PERFORM DIVIDE USING NUM1 NUM2 CHANGING RES.

FORM DIVIDE USING N1 N2 CHANGING R.
CHECK N2 NE 0.
R = N1 / N2.
   WRITE: / N1, '/', N2, '=', R.
ENDFORM.

The produces the following output:

         3 /          4 =           0.75

         2 /          3 =           0.67

In this example, the system leaves the subroutine DIVIDE during the second call after the CHECK statement because the value of N2 is zero.

If the EXIT or CHECK statement occurs within a loop in a subroutine, it applies to the loop, and not to the subroutine. EXIT and CHECK terminate loops in different ways (see Loops), but behave identically when terminating subroutines.