SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

Object Oriented ABAP - Use of RETURNING parameters in method

Object Oriented ABAP - Use of RETURNING parameters in method

To get some values from a method , one can use the EXPORTING, CHANGING or RETURNING parameters.

If one uses RETURNING parameters, the following restrictions apply:-

  1. No EXPORTING/CHANGING parameters can be used for the method.
  2. Only one RETURNING parameter can be used.
  3. RETURNING parameters are only passed by value.

This program demonstrates the use of RETURNING parameters and the various ways to call a method with RETURNING parameter to get the value into some variable.

Method M1 in class C1 have two input parameters(INPUT1 and INPUT2), which are used to derive value for RETURNING parameter , RESULT.

The program demonstrates various syntaxes that can be used to call a method of this kind.


REPORT zstp_oo_abap.

DATA : w_num TYPE i.

CLASS c1 DEFINITION .
  PUBLIC SECTION.
    METHODS : m1 IMPORTING input1        TYPE i
                           input2        TYPE i
                 RETURNING VALUE(result) TYPE i .
ENDCLASS.

CLASS c1 IMPLEMENTATION.
  METHOD  : m1.
    result = input1 * 2 + input2.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA : obj1 TYPE REF TO c1 .
  CREATE OBJECT obj1.
* Syntax 1
  CALL METHOD obj1->m1
    EXPORTING
      input1 = 5
      input2 = 4
    RECEIVING
      result = w_num.
  WRITE:/5 w_num .
* Syntax 2
  w_num = obj1->m1( input1 = 10 input2 = 20 ).
  WRITE:/5 w_num .
* Syntax 3
  MOVE obj1->m1( input1 = 2 input2 = 3 ) TO w_num .
  WRITE:/5 w_num .

 

Output -

image

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in