METHODS - RETURNING

Syntax

METHODS meth [ABSTRACT|FINAL]
  [IMPORTING parameters [PREFERRED PARAMETER p]]
   RETURNING VALUE(r) typing
  [{RAISING|EXCEPTIONS} exc1 exc2 ...].

Addition:

... RETURNING VALUE(r) typing

Effect

This statement declares a functional instance method meth. For additions ABSTRACT, FINAL, IMPORTING, RAISING and EXCEPTIONS, the same applies as for general instance methods.

Addition

... RETURNING VALUE(r) typing

Effect

Instead of additions EXPORTING and CHANGING, a functional method has an addition RETURNING, which exactly defines a formal parameter r as a return value. The return value must be passed by value with VALUE.

For the typing of return value r with typing, the same rules apply as for the typing of IMPORTING , EXPORTING and CHANGING parameters, except that a return value must always be fully typed: In typing, you cannot specify one of the generic types (see Typing Syntax).

Functional methods can be used at read positions of statements, at which a data object is expected, with the following syntax:

meth( )
meth( a )
meth( p1 = a1 P2 = a2 ... )

The full typing of the return value determines the data type of the operand. The meaning of the syntax is identical to that of the short form of CALL METHOD. When executing such a statement, the functional method is called and the content of the return value replaces the content of the operand.

Notes

Example

The functional method factorial of this example has a return value fact of type i. The COMPUTE statement shows the call of the method at an operand position.

CLASS math DEFINITION.
  PUBLIC SECTION.
    METHODS factorial
       IMPORTING n TYPE i
       RETURNING value(fact) TYPE i.
ENDCLASS.

CLASS math IMPLEMENTATION.
  METHOD factorial.
    fact = 1.
    IF n = 0.
      RETURN.
    ELSE.
      DO n TIMES.
        fact = fact * sy-index.
      ENDDO.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

DATA oref TYPE REF TO math.
DATA result TYPE i.

CREATE OBJECT oref.
COMPUTE result = oref->factorial( 4 ).