Skip to content

ABAP Keyword Documentation →  ABAP Programming Guidelines →  Robust ABAP →  Modularization units 

Typing of Formal Parameters

Other versions: 7.31 | 7.40 | 7.54

Background

The typing of formal parameters can be complete or generic. Formal parameters of methods must be, and formal parameters of function modules and subroutines should be, explicitly typed using the TYPE or LIKE addition. When you connect actual parameters to formal parameters, the system checks whether the data type of the actual parameter corresponds to the typing of the formal parameter.

  • Complete typing completely defines the data type of a formal parameter and applies to both dynamic access and static access to the formal parameter.
  • Generic typing does not completely define the data type of a formal parameter. Instead, the actual data type of the supplied actual parameter is used. For dynamic access to such a formal parameter, the properties of the actual parameter apply. For static access, the properties that are defined by the typing apply.

For generic typing, a set of predefined generic types is available in ABAP, which are only intended for the typing of formal parameters and field symbols. Using them in any other way can lead either to errors or to missing properties being completed with default values. The generic types are: any, any table, c, clike, csequence, data, decfloat, hashed table, index table, n, numeric, object, simple, sorted table, standard table, table, x, and xsequence. Self-defined table types without completely specified table key are also generic.

Rule

Be as specific as possible when typing formal parameters

Be only as generic as necessary when typing formal parameters. Completely generic types (any) should be the exception rather than the rule.

Details

Absolute type security within a procedure can only be achieved with complete typing. It should always be used when providing a generic service is not a defined goal. It is much easier to carry out tests for non-generic services than for generic services.

A generically typed procedure interface usually involves more implementation effort within the procedure (method) to avoid runtime errors. Therefore, use the following principle when providing generic interfaces: as little generic typing as possible and as much generic typing as necessary. You should use specific generic types, such as numeric or csequence, instead of any or data, for example, if services are involved that are supposed to process numeric values or character strings.

Generic typing can be a pitfall if you are not aware that you have used generic typing instead of complete typing, because only the technical type properties are checked when an actual parameter is connected, but no component names, for example. This can lead to different behavior than expected.


Note

These rules for typing also apply to field symbols.

Bad Example

The example in the following source code shows the trap you can fall into, particularly when working with table types, if the table key is not completely specified in their declaration (in a program or in the ABAP Dictionary). Due to the missing key specification, the table type that is used to type the formal parameter of sort_itab is generic. While the first static sorting is successful, the second SORT statement fails and triggers a runtime error. For the dynamic component specification, the properties of the actual parameter apply to the formal parameter, and the actual parameter does not have the col2 component (this can also be tracked in the ABAP Debugger).

CLASS class DEFINITION.
  PUBLIC SECTION.
    TYPES: BEGIN OF struc,
              col1 TYPE type1,
              col2 TYPE type2,
           END OF struc,
           itab TYPE STANDARD TABLE OF struc.
    METHODS: main,
             sort_itab CHANGING c_itab TYPE itab.
ENDCLASS.
CLASS class IMPLEMENTATION.
  METHOD main.
    TYPES: BEGIN OF struc,
             col1 TYPE type1,
             col3 TYPE type2,
           END OF struc.
    DATA itab TYPE STANDARD TABLE OF struc
              WITH NON-UNIQUE KEY col1 col3.
              ...
     sort_itab( CHANGING c_itab = itab ).
  ENDMETHOD.
  METHOD sort_itab.
     SORT c_itab BY col1 col2.
     SORT c_itab BY ('COL1') ('COL2'). "<- Runtime error!
  ENDMETHOD.
ENDCLASS.

Good Example

The following source code shows a very simple correction of the typing in the above example. Because the primary table key is completely specified, the used type is no longer generic, and the dynamic sorting functions like the static sorting.

...
itab TYPE STANDARD TABLE OF struc
     WITH NON-UNIQUE KEY col1 col2.
...