Parameter Interface of Procedures
Formal parameters are input parameters, output parameters, input/output parameters, or return values. Several obsolete table parameters also exist. Formal parameters are either generic or fully typed. Pass by reference or pass by value can be specified for most formal parameters. Pass by value is mandatory for some formal parameters.
Class-based exceptions can be declared using
RAISING for all procedures
function modules, and
subroutines), and can then be
propagated from the procedure.
can also be used in methods and function modules to define non-class-based exceptions, which can then
be raised in the procedure using
MESSAGE ... RAISING.
Pass by Reference or Pass by Value
When deciding whether to use pass by reference or pass by value for a formal parameter, you must compare the performance and robustness of each transfer type.
In ABAP, pass by reference always leads to better performance since no local data object has to be stored and no data transport is necessary when the procedure is called. Therefore, for performance reasons, pass by reference is usually preferable, unless explicit or implicit write access exists to an input parameter in the procedure or you want to ensure that an input/output parameter or an output parameter is returned only if the procedure ends without any errors. In such cases, pass by value is mandatory; this is so that the assigned actual parameter is not simultaneously modified in the caller when write access exists for a formal parameter. For performance reasons, only parameters of 100 bytes or less should be passed in these cases, whenever possible.
Also note the following when using pass by reference:
- In subroutines, write
access to an input parameter defined using
USINGis possible without a syntax error being produced (as is the case with input parameters of methods or function modules defined using
- An output parameter that is passed by reference acts like an input/ output parameter; in other words, if read access to an output parameter exists in the procedure before the value of that parameter is changed, this value is not initial, unlike with pass by value, but is the same as the current value of the actual parameter in the caller.
- If a procedure is stopped because of an error (that is, if it is stopped for a reason other than
reaching its last statement or
CHECK) all actual parameters that are passed by reference retain the value of the assigned formal parameter that that parameter had when the program was stopped. In pass by value, no values are passed to actual parameters when a procedure terminates.
Procedures and their calls have to be programmed so that these kinds of errors do not occur.
To summarize, pass by reference is always preferable when performance is an issue, while pass by value is more suitable in situations where robustness and data consistency are more important. These factors must be taken into account in each individual case when you decide which pass type to use with which type of parameter.
- When strings or internal tables of the same type are passed by value, table sharing comes into force between the data object created locally and the data object transferred, similarly to with an assignment. However, table sharing only happens if the row type of the internal table permits it. This means that, when you pass strings and internal tables, the performance benefits of pass by reference over pass by value may be negated by sharing (in certain circumstances).
- Only pass by reference can be specified for the obsolete table parameters.
- Pass by value is mandatory for the return value of functional methods, the output parameters of events in ABAP Objects, and all formal parameters of RFC-enabled function modules and update function modules (pass by value is also used implicitly with table parameters).
- A local data object is generated for formal parameters passed by reference that are not bound to an actual parameter during the call (as for pass by value).
- The result of the typing check when passing actual parameters to formal parameters is independent of the pass type. In a
pass by value, the check for
pass by reference
is always carried out, even though this is stricter than necessary in individual cases. For example,
a special reference variable cannot be passed to a general typed
CHANGINGparameter, even if pass by value is defined for this parameter.