ABAP Keyword Documentation → ABAP - Reference → Declarative statemnts → Typing → Checking Typing
Functions and Expressions as Actual Parameters
Functions and expressions can be passed to input parameters of methods as actual parameters in the method call, when creating an object, or when raising exceptions. In this case, special rules apply both for checking the typing and for passing the parameters.
Other versions: 7.31 | 7.40 | 7.54
Checking Typing
- A numerical function, a description function, or an arithmetic expression can be attached to any numerically typed input parameter.
- A bit function or a bit expression can be bound to any byte-type typed formal parameter.
- A string function or a string expression can be bound to any character-type typed formal parameter.
Calculation Type and Calculation Length
- The calculation type of an arithmetic expression is derived from the involved operands of the expression and the typing of the formal parameter, provided it is fully typed. If the formal parameter is typed generically, then only the operands of the expression are evaluated.
- The calculation length of a bit expression is the length of the largest operand.
Passing Parameters
When binding a function or an arithmetic expression, the parameters are always passed by value, even if the formal parameter is defined as to be passed by reference.
Fully Typed
With a static method call, if required, the return value of the function, or the result of the arithmetic expression is converted to the type of the formal parameter and then passed.
Note
With the dynamic method call the same rules apply as with the static call. However, the handling during runtime is time-consuming. Therefore, in a dynamic call you should use help variables instead of functions or arithmetic expressions if possible.
Generically Typed
- In a function, the formal parameter takes on the type of the return value.
- With an arithmetic expression, the formal parameter takes over the calculation type determined by
the operand. If the calculation type is
p
, the number of decimal places is determined by the accuracy required by the calculation and therefore depends on the values of the operands.
- In a bit expression, the formal parameter is set to type
x
in the calculation length determined by the operands.
- In a string expression, the formal parameter is set to the type
string
, if the typing is fully generic. If the typing is not fully generic, the length is set to the length of the result of the string expression.
Example
The functional method m1
is called twice for each assignment to result
.
The first call is executed in an arithmetical expression, which is passed as an actual parameter in
the second call. In the first call of each assignment, the formal parameter p1
has type p
of length 16. The number of decimal places is 0 in the first assignment,
14 in the second, and 1 in the third assignment. In the second call, the formal parameter p1
has the type decfloat34
in each assignment, because the calculation type of the arithmetical expression is determined by the return value of m1
.
CLASS c1 DEFINITION.
PUBLIC SECTION.
CLASS-METHODS m1 IMPORTING p1 TYPE numeric
RETURNING value(ret) TYPE decfloat34.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD m1.
ret = p1.
ENDMETHOD.
ENDCLASS.
DATA num1 TYPE p DECIMALS 2 VALUE '2.00'.
DATA num2 TYPE p DECIMALS 2 VALUE '1.00'.
DATA result TYPE decfloat34.
START-OF-SELECTION.
result = c1=>m1( sqrt( 4 ) + c1=>m1( num1 / 2 ) ).
result = c1=>m1( sqrt( 4 ) + c1=>m1( num1 / 3 ) ).
result = c1=>m1( sqrt( 4 ) + c1=>m1( num2 / 2 ) ).