ABAP - RETURN value
After all these years, the “real” return in ABAP has finally arrived. In this article we will show you how it works and what it can do.
Table of contents
With the last release, the return statement in ABAP was expanded to include an important function that many ABAP developers have probably been waiting for for ages. In this article we will examine and test the extension.
Introduction
With Release 2023 (7.58), the RETURN statement was expanded to include a new function to set the return value in functional methods (RETURNING). If we want to currently set a value and then leave the logic, we have to use two statements.
rd_result = 20.
RETURN.
This methodology is primarily used where errors occur during processing and the method needs to be exited prematurely.
Simple
In the first case, let's look at a method with simple implementation and simple data types. At 0 we want to terminate the method early, then we execute a RETURN and let and return the result. To check, we introduce one last RETURN.
IF id_number = 0.
RETURN `Early exit.`.
ENDIF.
RETURN |Number was returned: { id_number }|.
RETURN `Dead end`.
To test, we call the method with the values 0, 1 and 2 and have the result output to the console.
The result is as expected, if 0 the method exits early and returns us the sentence "Early Exit". For the other two values, the corresponding number is returned. "Dead end" is never reached because RETURN ends the logic at this point.
Complexe
In a slightly more complex example, let's look at the RETURN with a structure and inline declaration. If it is zero, we return the empty variable; for all other values, we fill the structure with VALUE and pass it to RETURN.
DATA ls_data TYPE ts_structure.
IF id_number = 0.
RETURN ls_data.
ENDIF.
RETURN VALUE #( id = id_number
content = |The new ID is: { id_number }|
value = 'ID' && id_number ).
We execute the method with 0 and 1 and get our expected result back.
Complete example
If you would like to recreate the example on your system, you can find the complete executable class for use here.
CLASS zcl_bs_demo_new_return DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
PRIVATE SECTION.
TYPES:
BEGIN OF ts_structure,
id TYPE i,
content TYPE string,
value TYPE c LENGTH 15,
END OF ts_structure.
METHODS functional_method_call_simple
IMPORTING id_number TYPE i
RETURNING VALUE(rd_result) TYPE string.
METHODS functional_method_call_struct
IMPORTING id_number TYPE i
RETURNING VALUE(rs_result) TYPE ts_structure.
ENDCLASS.
CLASS zcl_bs_demo_new_return IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
out->write( functional_method_call_simple( 0 ) ).
out->write( functional_method_call_simple( 1 ) ).
out->write( functional_method_call_simple( 2 ) ).
out->write( functional_method_call_struct( 0 ) ).
out->write( functional_method_call_struct( 1 ) ).
ENDMETHOD.
METHOD functional_method_call_simple.
IF id_number = 0.
RETURN `Early exit.`.
ENDIF.
RETURN |Number was returned: { id_number }|.
RETURN `Dead end`.
ENDMETHOD.
METHOD functional_method_call_struct.
DATA ls_data TYPE ts_structure.
IF id_number = 0.
RETURN ls_data.
ENDIF.
RETURN VALUE #( id = id_number
content = |The new ID is: { id_number }|
value = 'ID' && id_number ).
ENDMETHOD.
ENDCLASS.
Conclusion
The new RETURN makes returns easier for functional methods, especially since they behave like in other programming languages. This is a relief for us, especially since with ABAP OO the functional methods are increasing and it is becoming clearer for the developer.
Source: