Entering content frameNaming Subroutines Locate the document in its SAP Library structure

With the PERFORM statement, you can call subroutines which are coded in the same ABAP program (internal calls), or subroutines which are coded in other ABAP programs (external calls).

You can also specify the name of the subroutine dynamically at runtime, and call subroutines from a list.

Internal Subroutine Calls

To call a subroutine defined in the same program, you need only specify its name in the PERFORM statement:

PERFORM <subr> [USING   ... <pi>... ]
               [CHANGING... <pi>... ].

The internal subroutine can access all of the global data of the calling program.

Example

PROGRAM FORM_TEST.

DATA: NUM1 TYPE I,
NUM2 TYPE I,
SUM TYPE I.

NUM1 = 2. NUM2 = 4.
PERFORM ADDIT.

NUM1 = 7. NUM2 = 11.
PERFORM ADDIT.

FORM ADDIT.
  SUM = NUM1 + NUM2.
  PERFORM OUT.
ENDFORM.

FORM OUT.
  WRITE: / 'Sum of', NUM1, 'and', NUM2, 'is', SUM.
ENDFORM.

The produces the following output:

Sum of 2 and 4 is 6

Sum of 7 and 11 is 18

In this example, two internal subroutines ADDIT and OUT are defined at the end of the program. ADDIT is called from the program and OUT is called from ADDIT. The subroutines have access to the global fields NUM1, NUM2, and SUM.

External subroutine calls

The principal function of subroutines is for modularizing and structuring local programs. However, subroutines can also be called externally from other ABAP programs. In an extreme case, you might have an ABAP program that contained nothing but subroutines. These programs cannot run on their own, but are used by other ABAP programs as pools of external subroutines.

However, if you want to make a function available throughout the system, you should use function modules instead of external subroutines. You create function modules in the ABAP Workbench using the Function Builder. They are stored in a central library, and have a defined release procedure.

You can encapsulate functions and data in the attributes and methods of classes in ABAP Objects. For any requirements that exceed pure functions, you can use global classes instead of external subroutines.

When you call a subroutine externally, you must know the name of the program in which it is defined:

PERFORM <subr>(<prog>) [USING   ... <pi>... ]
                       [CHANGING... <pi>... ] [IF FOUND].

You specify the program name <prog> statically. You can use the IF FOUND option to prevent a runtime error from occurring if the program <prog> does not contain a subroutine <sub>. In this case, the system simply ignores the PERFORM statement.

When you call an external subroutine, the system loads the whole of the program containing the subroutine into the internal session of the calling program (if it has not already been loaded). For further information, refer to Memory Structure of an ABAP Program. In order to save memory space, you should keep the number of subroutines called in different programs to a minimum.

Example

Suppose a program contains the following subroutine:

PROGRAM FORMPOOL.

FORM HEADER.
  WRITE: / 'Program started by', SY-UNAME,
         / 'on host', SY-HOST,
           'date:', SY-DATUM, 'time:', SY-UZEIT.
  ULINE.
ENDFORM.

The subroutine can then be called from another program as follows:

PROGRAM FORM_TEST.

PERFORM HEADER(FORMPOOL) IF FOUND.

In this example, no data is passed between calling program and subroutine.

Specifying Subroutines Dynamically

You can specify the name of a subroutine and, in the case of external calls, the name of the program in which it occurs, dynamically as follows:

PERFORM (<fsubr>)[IN PROGRAM (<fprog>)][USING   ... <pi>... ]
                                       [CHANGING... <pi>... ]
                                       [IF FOUND].

The names of the subroutine and the external program are the contents of the fields <fsubr> and <fprog> respectively. By using the option IF FOUND, you can prevent a runtime error from being triggered if <fprog> does not contain a subroutine with the name <fsubr>. If you omit the parentheses, this variant of the PERFORM statement behaves like the static variant.

Example

Suppose a program contains the following subroutines:

PROGRAM FORMPOOL.

FORM SUB1.
  WRITE: / 'Subroutine 1'.
ENDFORM.

FORM SUB2.
  WRITE: / 'Subroutine 2'.
ENDFORM.

Dynamic subroutine specification:

PROGRAM FORM_TEST.

DATA: PROGNAME(8) VALUE 'FORMPOOL',
SUBRNAME(8).

SUBRNAME = 'SUB1'.
PERFORM (SUBRNAME) IN PROGRAM (PROGNAME) IF FOUND.

SUBRNAME = 'SUB2'.
PERFORM (SUBRNAME) IN PROGRAM (PROGNAME) IF FOUND.

The produces the following output:

Subroutine 1

Subroutine 2

The character field PROGNAME contains the name of the program, in which the subroutines are contained. The names of the subroutines are assigned to the character field SUBRNAME.

Calling Subroutines from a List

You can call a subroutine from a list as follows:

PERFORM <idx> OF <subr1> <subr 2>.... <subr n>.

The system calls the subroutine specified in the subroutine list in position <idx>. You can only use this variant of the PERFORM statement for internal subroutine calls, and only for subroutines without a parameter interface. The field <idx> can be a variable or a literal.

Example

PROGRAM FORM_TEST.

DO 2 TIMES.
  PERFORM SY-INDEX OF SUB1 SUB2.
ENDDO.

FORM SUB1.
  WRITE / 'Subroutine 1'.
ENDFORM.

FORM SUB2.
  WRITE / 'Subroutine 2'.
ENDFORM.

The produces the following output:

Subroutine 1

Subroutine 2

In this example, the two internal subroutines, SUB1 and SUB2, are called consecutively from a list.

 

 

 

 

 

 

Leaving content frame