Local Data in the Subroutine 

Data declarations in procedures create local data types and objects that are only visible within that procedure. There are two kinds of data types and objects - dynamic and static. Dynamic data objects only exist while the subroutine is running, while static objects still exist after the subroutine has finished running, and retain their values until the next time the subroutine is called. You can also declare local field symbols. A special kind of data object for subroutines are copies of global data on a local data stack. You define and address them using field symbols.

Dynamic Local Data Types and Objects

Local data types and objects declared in subroutines using the TYPES and DATA statements are deleted when the subroutine ends, and recreated each time the routine is called.

Every subroutine has its own local namespace. If you declare a local data type or object with the same name as a global data type or object, the global type or object cannot be addressed from within the subroutine. Local data types or data objects hide identically named global data types or objects. This means that if you use the name of a data type or object in the subroutine, you always address a locally declared object - if this exists - and otherwise a globally declared one. To avoid this, you must assign other names to local types and objects. For example, you might name all of your local data starting with ‘L_’.

PROGRAM FORM_TEST.

TYPES WORD(10) TYPE C.
DATA TEXT TYPE WORD.

TEXT = '1234567890'. WRITE / TEXT.

PERFORM DATATEST.

WRITE / TEXT.

FORM DATATEST.
   TYPES WORD(5) TYPE C.
DATA TEXT TYPE WORD.
   TEXT = 'ABCDEFGHJK'. WRITE / TEXT.
ENDFORM.

When you run the program, the following is displayed:

1234567890

ABCDE

1234567890

In this example, a data type WORD and a data object TEXT with type WORD are declared globally in the main program. After assigning a value to TEXT and writing it to the list, the internal subroutine DATATEST is called. Inside the subroutine, a data type WORD and a data object TEXT with type WORD are declared locally. They hide the global type and object. Only after the subroutine is finished are the global definitions are valid again.

Static Local Data Objects

If you want to keep the value of a local data object after exiting the subroutine, you must use the STATICS statement to declare it instead of the DATA statement. With STATICS you declare a data object that is globally defined, but only locally visible from the subroutine in which it is defined.

PROGRAM FORM_TEST.

PERFORM DATATEST1.
PERFORM DATATEST1.

SKIP.

PERFORM DATATEST2.
PERFORM DATATEST2.

FORM DATATEST1.
   TYPES F_WORD(5) TYPE C.
DATA F_TEXT TYPE F_WORD VALUE 'INIT'.
   WRITE F_TEXT.
   F_TEXT = '12345'.
   WRITE F_TEXT.
ENDFORM.

FORM DATATEST2.
   TYPES F_WORD(5) TYPE C.
STATICS F_TEXT TYPE F_WORD VALUE 'INIT'.
   WRITE F_TEXT.
   F_TEXT = 'ABCDE'.
   WRITE F_TEXT.
ENDFORM.

When you run the program, the following is displayed:

INIT 12345 INIT 12345

INIT ABCDE ABCDE ABCDE

In this example, two similar subroutines DATATEST1 and DATATEST2 are defined. In DATATEST2, the STATICS statement is used instead of the DATA statement to declare the data object F_TEXT. During each call of DATATEST1, F_TEXT is initialized again, but it keeps its value for DATATEST2. The VALUE option of the STATICS statement functions only during the first call of DATATEST2.

Local Field Symbols

All field symbols declared in a subroutine using the FIELD-SYMBOLS statement are local. The following rules apply to local field symbols:

Local Copies of Global Fields

In a subroutine, you can create local copies of global data on the local stack. To do this, use a local field symbol and the following variant of the ASSIGN statement:

ASSIGN LOCAL COPY OF <f> TO <FS>.

The system places a copy of the specified global field <f> on the stack. In the subroutine, you can access and change this copy without changing the global data by addressing the field symbol <FS>.

You can use the LOCAL COPY OF addition with all variants of the ASSIGN statement except ASSIGN COMPONENT.

Other variants of the ASSIGN statement that are used in subroutines are:

ASSIGN LOCAL COPY OF INITIAL <f> TO <FS>.

This statement creates an initialized copy of the global field <f> on the stack without copying the field contents.

ASSIGN LOCAL COPY OF INITIAL LINE OF <itab> TO <FS>.

This statement creates an initial copy of the line of a global internal table <itab> on the stack.

ASSIGN LOCAL COPY OF INITIAL LINE OF (<f>) TO <FS>.

This statement creates an initial copy of the line of a global internal table <itab> on the stack. The internal table is specified dynamically as the contents of the field <f>.

DATA TEXT(5) VALUE 'Text1'.

PERFORM ROUTINE.

WRITE TEXT.

FORM ROUTINE.
FIELD-SYMBOLS <FS>.
ASSIGN LOCAL COPY OF TEXT TO <FS>.
WRITE <FS>.
<FS> = 'Text2'.
WRITE <FS>.
ASSIGN TEXT TO <FS>.
WRITE <FS>.
<FS> = 'Text3'.
ENDFORM.

The output is:

Text1 Text2 Text1 Text3

By assigning TEXT to <FS> in the subroutine ROUTINE in the program FORMPOOL, you place a copy of the field TEXT on the local data stack. By addressing <FS>, you can read and change this copy. The global field TEXT is not affected by operations on the local field. After you have assigned the field to the field symbol without the LOCAL COPY OF addition, it points directly to the global field. Operations with the field symbol then affect the global field.