This is a test message to test the length of the message box.
Login
ABAP RETURN value
Created by Software-Heroes

ABAP - RETURN value

1373

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.

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:

ABAP Documentation - RETURN


Included topics:
New ABAPRETURN
Comments (1)



And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.


ABAP Deep Dive - FOR (Loops)

Category - ABAP

Let's take a closer look at the FOR loop. How does it work? What do I have to consider and what can I do with it?

04/14/2023

ABAP Deep Dive - Table access (internal)

Category - ABAP

In this article, let's take a look at table access to internal tables and how they replace READ TABLE.

02/03/2023

ABAP Developer still relevant

Category - ABAP

In this article we look at whether ChatGPT can already replace an ABAP developer or whether it can be used as a help in everyday life.

01/06/2023

ABAP - FINAL

Category - ABAP

In this article, we'll take a look at the new FINAL language construct, how it works, and what you can do with it.

12/23/2022

ABAP Deep Dive - VALUE

Category - ABAP

In this article we want to look at the value statement again in all its forms and how you can use it in your daily work.

11/11/2022