Entering content frame

Naming 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    p1 p2... ]
             [CHANGING p1 p2... ].

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


REPORT demo_mod_tech_perform_int.

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.

FORM out.
  WRITE: / 'Sum of', num1, 'and', num2, 'is', sum.

This produces the following output:

Sum of          2 and          4 is          6

Sum of          7 and         11 is         18

In this example the subroutines addit and out are defined at the end of the program. addit is called by the program and out is called by addit. The subroutines have access to the global fields num1, num2 und 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    p1 p2... ]
                   [CHANGING p1 p2... ] [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 progdoes not contain a subroutine subr. 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). In order to save memory space, you should keep the number of subroutines called in different programs to a minimum.


Suppose a program contains the following subroutine:

REPORT formpool.

FORM header.
  WRITE: / 'Program started by', sy-uname,
         / 'on host', sy-host,
           'date:', sy-datum, 'time:', sy-uzeit.


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

REPORT demo_mod_tech_perform_ext.

PERFORM header(demo_mod_tech_formpool_1) IF FOUND.

In this example, no data is passed between the calling program and the 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    p1 p2... ]
                                   [CHANGING p1 p2...
                                   [IF FOUND].

The names of the subroutine and the external program are the contents of the fields fsubr und fprogrespectively. By using the option IF FOUND, you can prevent a runtime error from being triggered if no subroutine with the name fsubr is found. If you omit the parentheses, this variant of the PERFORMstatement behaves like the static variant.


Assume a program contains the following subroutines:

PROGRAM formpool.

FORM sub1.
  WRITE: / 'Subroutine 1'.

FORM sub2.
  WRITE: / 'Subroutine 2'.

Dynamic Subroutine Specification:

PROGRAM form_test.

DATA: progname(8) TYPE c VALUE 'FORMPOOL',
      subrname(8) TYPE c.

subrname = 'SUB1'.
PERFORM (subrname) IN PROGRAM (progname) IF FOUND.

PERFORM (subrname) IN PROGRAM (progname) IF FOUND.

This 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 subr2 ... subrn.

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


REPORT demo_mod_tech_perform_list.

  PERFORM sy-index OF sub1 sub2.

FORM sub1.
  WRITE / 'Subroutine 1'.

FORM sub2.
  WRITE / 'Subroutine 2'.

This 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