Typing Formal Parameters

To ensure that a formal parameter of a subroutine is of a certain type, you can specify that type in the FORM statement. To do this, enter TYPE <t> or LIKE <f> (see Basic Form of the DATA Statement) after the formal parameter in the list after TABLES, USING, or CHANGING. This type specification is optional.

When you call a subroutine with PERFORM, the system checks whether the types of the actual parameters in the PERFORM statement are compatible with the types assigned to the formal parameters. The compatibility rules are set out in the table below. There are no type conversions. If types are incompatible, the system outputs an error message during the syntax check for internal subroutine calls or reacts with a runtime error in the case of external subroutine calls.

The following compatibility rules apply for the typing of formal parameters:

Typing

Syntax check for actual parameters

No type specification, TYPE ANY

The system accepts actual parameters of any type. All attributes of the actual parameter are passed to the formal parameter.

TYPE TABLE

The system checks whether the actual parameter is an internal table. All attributes and the structure of the table are passed from the actual parameter to the formal parameter.

TYPE C, N, P, or X

The system checks whether the actual parameter is of type C, N, P, or X. The length of the parameter and its DECIMALS specification (for type P) are passed from the actual parameter to the formal parameter.

TYPE D, F, I, or T, LIKE <f>,TYPE <ud>

These types are fully determined. The system checks whether the data type of the actual parameter agrees completely with the type of the formal parameter.

(<ud> is user-defined)

REPORT SAPMZTST.

DATA:
DATE1 TYPE D, DATE2 TYPE T,
STRING1(6) TYPE C, STRING2(8) TYPE C,
NUMBER1 TYPE P DECIMALS 2, NUMBER2 TYPE P,
COUNT1 TYPE I, COUNT2 TYPE I.

PERFORM TYPETEST USING DATE1 STRING1 NUMBER1 COUNT1.
SKIP.
PERFORM TYPETEST USING DATE2 STRING2 NUMBER2 COUNT2.

FORM TYPETEST USING NOW
TXT TYPE C
VALUE(NUM) TYPE P
INT TYPE I.
DATA: T.
DESCRIBE FIELD NOW TYPE T.
WRITE: / 'Type of NOW is', T.
DESCRIBE FIELD TXT LENGTH T.
WRITE: / 'Length of TXT is', T.
DESCRIBE FIELD NUM DECIMALS T.
WRITE: / 'Decimals of NUM are', T.
DESCRIBE FIELD INT TYPE T.
WRITE: / 'Type of INT is', T.
ENDFORM.

After SAPMZTST, the output appears as follows:

TYPE of NOW is D
Length of TXT is 6
Decimals of NUM are 2
Type of INT is I

TYPE of NOW is T
Length of TXT is 8
Decimals of NUM are 0
Type of INT is I

An internal subroutine TYPETEST is called two times with different actual parameters. All actual and formal parameters are compatible and no error message occurs during the syntax check. If you would declare, for example, COUNT2 with TYPE F instead of TYPE I in the calling program, the syntax check would report an error because the formal parameter INT is specified with TYPE I. Note that, depending on their type specifications, the formal parameters can have different types and attributes for each call of the subroutine.

 

For more information, see the keyword documentation on FORM.