In the latest ABAP releases, a more direct syntax for returning values from functional methods is introduced. The requirement to assign a value to a returning parameter before exiting is now replaced by a single statement. By using this enhancement, unnecessary local variables and assignment lines are successfully avoided. A much cleaner and more expressive code structure is achieved by passing the result directly via the RETURN command. Consequently, the overall verbosity of method implementations is significantly reduced for developers.
CLASS zcl_return_example DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
" RETURNING: the result is returned in a single variable (7.40+ widespread use)
METHODS get_greeting
IMPORTING iv_name TYPE string
RETURNING VALUE(rv_text) TYPE string.
" The larger of two numbers - the result is returned directly by RETURNING
METHODS max_of_two
IMPORTING iv_a TYPE i
iv_b TYPE i
RETURNING VALUE(rv_max) TYPE i.
" RETURN.: early exit; when the RETURNING variable is assigned, the method ends
METHODS describe_sign
IMPORTING iv_number TYPE i
RETURNING VALUE(rv_desc) TYPE string.
ENDCLASS.
CLASS zcl_return_example IMPLEMENTATION.
METHOD get_greeting.
RETURN |Hi, { iv_name }!|.
ENDMETHOD.
METHOD max_of_two.
" Expression assignment (the result goes again to the RETURNING variable)
RETURN COND #( WHEN iv_a >= iv_b THEN iv_a ELSE iv_b ).
ENDMETHOD.
METHOD describe_sign.
IF iv_number < 0.
RETURN 'Negative'.
ENDIF.
IF iv_number = 0.
RETURN 'Zero'.
ENDIF.
RETURN 'Positive'.
ENDMETHOD.
ENDCLASS.
