SAP FORMAL PARAMETERS OVIEW



Get Example source ABAP code based on a different SAP table
  


VERSION 5 IN

Parameter Interface of Procedures
The parameter interface of a procedure consists of formal parameters and specifies the possible exceptions in the procedure.
ITOC

Formal Parameters
Formal parameters are input parameters , output parameters, input/output parameters, or return values. In addition, there are several obsolete table parameters. Formal parameters are either generic or completely typed. Pass by reference or pass by value can be specified for most formal parameters. Pass by value is mandatory for some formal parameters.

ABAP_PGL
Choose the appropriate formal parameter type
ABAP_PGL_END

Pass by Reference or Pass by Value
Parameters can be passed to procedures either by reference or by value.
Pass by reference In the case of pass by reference, a reference to the actual parameter is passed to the procedure when it is called. The procedure then works with the actual parameter itself. No local data object is created for the actual parameter. Input parameters that are passed by reference cannot be changed in the procedure
BEGIN_SECTION VERSION 5 OUT (exception: USING parameters of subroutines).
END_SECTION VERSION 5 OUT
Pass by value In the case of pass by value, a type-compliant local data object is created as a copy of the actual parameter for the formal parameter. Output parameters and return values are initialized when the procedure is started and input parameters, as well as input/output parameters, are given the value of the actual parameter. A changed formal parameter is only passed to the actual parameter if the procedure is completed without errors.
When deciding whether to use pass by reference or pass by value for a formal parameter, the performance and robustness of each pass-by type must be compared.
In ABAP, pass by reference always has better performance since no local data object is created 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 writes to an input parameter take place in the procedure or if it needs to be ensured that an input/output parameter or an output parameter is returned only if the procedure ends without any errors. In such a case, pass by value is mandatory, to make sure that the assigned actual parameter is not modified simultaneously in the caller when writes are made to 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:
BEGIN_SECTION VERSION 5 OUT
In subroutines, writes can be made to an input parameter defined using USING without a syntax error being produced. In contrast, for input parameters of methods or function modules defined using IMPORTING writes are syntactically forbidden.
END_SECTION VERSION 5 OUT
An output parameter that is passed by reference acts like an input/ output parameter; that is, if read access to an output parameter is carried out in the procedure before the value of that parameter is changed, this value is not initial, unlike with pass by value, but corresponds to the current value of the actual parameter in the caller.
If a procedure is terminated because of an error, that is, if it is terminated for a reason other than reaching its last statement or RETURN, EXIT, or CHECK , all actual parameters that are passed by reference are set to the value of the assigned formal parameter that the parameter was at when the program was terminated. In pass by value, no values are passed to actual parameters when a procedure is terminated.
Procedures and their calls must be programmed in such a way that these kinds of errors do not occur.
To summarize, pass by reference is always preferable when performance is important, whereas pass by value is more secure in situations where the emphasis is on robustness and data consistency. These factors must be considered when deciding which pass-by type to use with which type of parameter.

ABAP_PGL
Choose a suitable pass-by type
ABAP_PGL_END



Latest notes:

When strings or internal tables of the same type are passed by value, table sharing takes effect between the data object created locally and the passed data object, such as in assignments. However, table sharing only occurs if the line type of the internal table allows it. When passing strings and internal tables, the performance drawbacks of pass by value can, under certain circumstances, be lifted by sharing.
Only pass by reference can be specified for the obsolete table parameters.
A local data object is generated for formal parameters that are passed by reference and are not bound to an actual parameter during the call as for pass by value.
There are special rules for defining literals and functions and expressions as actual parameters:
When a literal is bound, its typing is not checked as strictly.
When a function, a calculation expression, a constructor expression, or a table expression is bound, passing is always pass by value, even if the formal parameter is defined as pass by reference.
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 performed, even though this is stricter than necessary in individual cases. For example, a special reference variable cannot be passed to a general typed CHANGING parameter, even if pass by value is defined for this parameter.
NON_V5_HINTS
Pass by value is mandatory for the return value of functional methods, the output parameters of events in ABAP Objects, and for all formal parameters of RFC-enabled function modules and update function modules (pass by value is also used implicitly with table parameters).
ABAP_HINT_END

ABAP_EXAMPLE_VX5
For a method meth, CHANGING parameter p1 is defined for pass by reference and p2 for pass by value. The value of both parameters is changed before an exception is raised. The actual parameter a1 bound to the parameter using pass by reference contains the changed value when the exception is handled. The actual parameter a2 bound to the parameter using pass by value, on the other hand, keeps its value.
ABEXA 01211
ABAP_EXAMPLE_END

Exceptions
Class-based exceptions can be declared using RAISING in all procedures ( methods, function modules
BEGIN_SECTION VERSION 5 OUT , and subroutines
END_SECTION VERSION 5 OUT ), and can then be propagated from the procedure. EXCEPTIONS can also be used in methods and function modules to define non-class-based exceptions, which can then be raised in the procedure using RAISE or MESSAGE ... RAISING.