Tuesday, March 19, 2013

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.

REPORT  zsr_test NO STANDARD PAGE HEADING.

DATA v_return TYPE i.

*----------------------------------------------------------------------*
*       CLASS cl_ret_para DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_ret_para DEFINITION.
  PUBLIC SECTION.
    METHODS m_ret_para IMPORTING i_inp1 TYPE i
                                 i_inp2 TYPE i
                       RETURNING value(return) TYPE i.
ENDCLASS.                    "cl_ret_para DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_ret_para IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
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

START-OF-SELECTION.
  DATA obj TYPE REF TO cl_ret_para.
  CREATE OBJECT obj.
  CALL METHOD obj->m_ret_para
    EXPORTING
      i_inp1 = 3
      i_inp2 = 6
    RECEIVING
      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.

6 comments:

virrat said...

Good One.....

Anonymous said...

SAP Success Factors Real Time Hands on Training in Chennai...

Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286

Both Classroom/Online Training is Available!!!!!!

Unknown said...



Best SAP Success Factors Training Institute in Chennai
http://thecreatingexperts.com/sap-successfactors-training-in-chennai/
Best SAP MM Training in Chennai
http://thecreatingexperts.com/sap-mm-training-in-chennai/
Best SAP SD Training in Chennai
http://thecreatingexperts.com/sap-sd-training-in-chennai/
http://thecreatingexperts.com/sap-hr-training-in-chennai/
Best SAP FICO Training in Chennai
http://thecreatingexperts.com/sap-fico-training-in-chennai/
Best SAP ABAP Training in Chennai
http://thecreatingexperts.com/sap-abap-training-in-chennai/
Best SAP BASIS Training in Chennai
http://thecreatingexperts.com/sap-basis-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

Camelliacanan said...

This was really nice information in which where we have to focus with the site. By placing with our targeted keywords and LSI keywords in the content we can get more crawling value right. But apart from this i have one doubt why people are telling by placing the number in title will be a boosting technique i don't know.

SAP HR Training in Chennai

SAP SD Training in Chennai

SAP Basis Training in Chennai

Unknown said...
This comment has been removed by the author.
Unknown said...


1. To make the example clearer and emphesize the point, you should really use the same input parameters and have the method display the same output. Replace class implementation and the rest of the program after the class implementation with the code below.

2. When you give examples of something that can be used in different ways, why don't you explain what the difference is and when to use each, advantages and disadvantages. Not much point of the half-done posts.

CLASS cl_ret_para IMPLEMENTATION.
METHOD m_ret_para.
return = i_inp1 * i_inp2.
ENDMETHOD. "m_ret_para
ENDCLASS. "cl_ret_para IMPLEMENTATION

START-OF-SELECTION.

DATA: obj TYPE REF TO cl_ret_para.

" 1. CALL METHOD format:
" Two ways of creating object 1.1 and 1.2
" 1.1 create object using CREATE OBJECT
* CREATE OBJECT obj.
" 1.2 create object using keyword NEW
obj = NEW cl_ret_para( ).

CALL METHOD obj->m_ret_para " Why would we do it this way?
EXPORTING
i_inp1 = 10
i_inp2 = 20
RECEIVING
return = v_return.
WRITE:/ 'Using CALL METHOD format: v_return =', v_return.


" 2. Assign result to a variable format:
v_return = obj->m_ret_para( i_inp1 = 10 i_inp2 = 20 ). " Why would we do it this way?
WRITE:/ 'Assign result to a variable format: v_return =', v_return.


" 3. Move result to a variable format:
MOVE obj->m_ret_para( i_inp1 = 10 i_inp2 = 20 ) TO v_return. " Why would we do it this way?
WRITE:/ 'Move result to a variable format: v_return =', v_return.