banner ads

Returning Parameter of Method

We use returning parameter to get values from a method. Some pre-requisites are there if we use Returning parameter as follows.

1.       If we use Returning parameter then we can’t use Exporting or Changing parameter in that method.
2.       Returning parameter is passed by value only.
3.       Only one Returning parameter can be used in a method.

In the following example we have a method m_ret_para which contains one Returning parameter and it is called by Value (name of returning parameter). We already have two import parameters and we are importing two different values into this method. Now to capture the returning value we need to have a variable and it is v_return here.

We are calculating the value of the return with parameter name inside the method. After that at the time of calling it needs to be received into the variable v_return. We also have different formats of receiving of the returning parameter.


DATA v_return TYPE i.

*       CLASS cl_ret_para DEFINITION
    METHODS m_ret_para IMPORTING i_inp1 TYPE i
                                 i_inp2 TYPE i
                       RETURNING value(returnTYPE i.
ENDCLASS.                    "cl_ret_para DEFINITION

*       CLASS cl_ret_para IMPLEMENTATION
  METHOD m_ret_para.
    return = i_inp1 * i_inp2.
    WRITE: / i_inp1, '*', i_inp2, '='return.
  ENDMETHOD.                    "m_ret_para
ENDCLASS.                    "cl_ret_para IMPLEMENTATION

  CALL METHOD obj->m_ret_para
      i_inp1 = 3
      i_inp2 = 6
      return = v_return.

  v_return = obj->m_ret_para( i_inp1 = 10 i_inp2 = 20 ).
  WRITE: / 'Calling from outside v_return =', v_return.

  MOVE obj->m_ret_para( i_inp1 = 12 i_inp2 = 21 ) TO v_return.
  WRITE: / 'Calling from outside by Move =', v_return.

Now at debugging mode we analyze the output. At the first call the import parameters contain its value.

Inside the method the Return populates its calculated value but v_return variable is still initialized.

After come back from the method the variable now contains the Return value.

For the next call the import parameters are populated with their respective values and the Return parameter is already initialized. But the variable still contains its last value.

Then similarly the Return populates its calculated value and after getting back from the method the variable is populated with the new value.

The program generates the following output.

No comments