ABAP Keyword Documentation → ABAP Programming Guidelines → Robust ABAP → Modularization units
Pass By Reference for Output Parameters
Other versions: 7.31 | 7.40 | 7.54
Background
When parameters are passed to a procedure by reference, this procedure directly uses the data object
that has been passed as a parameter. Its value is consequently determined by the calling program of
the procedure. You must be particularly aware of this behavior for EXPORTING
parameters, whose value is # in contrast to the pass by value method # not initialized when the procedure
is called. After the procedure has started, an output parameter that was passed by reference has the value of the supplied actual parameter.
Rule
Use Output Parameters with the Pass by Reference Method Correctly
Do not evaluate EXPORTING
parameters that are passed by reference in a procedure
(method) until a value has been explicitly assigned.
Details
Because the value of an output parameter that has been passed by reference is undefined from the procedure#s perspective, it cannot be evaluated within the procedure in a useful manner. Therefore, no assumptions can be made regarding the content of the parameter until the first value has been assigned to it.
If such a parameter is an internal table or a string, a simple write access is not sufficient. First, an initialization must be implemented. For example, if you want to insert new lines in an internal table that is supposed to be output by reference, its current content needs to be deleted first. Due to passing by reference, you cannot be sure that the table is actually empty when the procedure is started. The same applies to strings that are filled using concatenation operations within the procedure.
Note
If the described properties are to be exploited for writable parameters that have been passed by reference in a procedure
(method), that
is, a read access is to be implemented prior to the write access or an existing dynamic data object is to be extended, you must select the
appropriate formal parameter type, that is, input/output parameter (CHANGING
parameter).
Exception
Strictly speaking, you must only initialize optional output parameters that have been passed by reference
if the parameter is connected to an actual parameter when called. This can be determined using the
IS SUPPLIED query. The obsolete query, IS REQUESTED
, in contrast, should no longer be used.
Example
The following source code shows how an internal table that, for performance reasons, is implemented
by reference is returned. For this reason, it cannot be declared as a RETURNING
parameter. The tabular output parameter is explicitly initialized at the beginning of the method before new lines are inserted.
PUBLIC SECTION.
CLASS-METHODS get_some_table
EXPORTING e_some_table TYPE table_type.
ENDCLASS.
CLASS class IMPLEMENTATION.
METHOD get_some_table.
DATA new_line LIKE LINE OF e_some_table.
CLEAR e_some_table.
...
INSERT new_line INTO TABLE e_some_table.
...
ENDMETHOD.
ENDCLASS.