Method Parameters - Importing, Exporting, Changing and Returning - ABAP OOPS

This article covers the use of method parameters in Object oriented ABAP with a simple program.

Method Parameters - Importing, Exporting, Changing and Returning - ABAP OOPS

METHOD PARAMETERS

The method parameters are used in receiving the data as an input, modify or validate data inside the method and export or return the output in where it is called.

Importing: Importing parameters are input to the method.

Exporting: Exporting parameters are the output of the method.

Changing: If the attributes get changed frequently and if there is a need to avoid overwriting of the already filled value, we need a changing parameter. In short it is Input/Output parameter.

Example: You have created some methods inside a class. Each method appends a record to internal table inside a loop and every time it is getting changed. To append the new records while maintaining the previous one we should declare the internal table as changing parameter.

Returning: A returning parameter is used in performing logical checks in IF and other logical statements. The methods with returning parameters are known as functional methods.

If a returning parameter is used in method following rules will apply:

1) Importing and changing parameters cannot be used in method call.
2) Only one returning parameter should be used.
3) The methods with returning parameters can be used in Assignment (=) as it returns only single value.

Example: IF OBJ->ISTRUE( ) = ‘X’. Here assume that ISTRUE method returns a single Boolean value.

 The following program demonstrates the use of importing, exporting, changing and returning parameters of a method.

Example

REPORT ztest_program.

CLASS lcl_class1 DEFINITION.
  PUBLIC SECTION.
    METHODS:
      istaxable
        IMPORTING iv_salary       TYPE p
        RETURNING VALUE(rv_check) TYPE boolean,

      calculate_tax
        IMPORTING iv_grade  TYPE char3
        EXPORTING ev_tax    TYPE p
        CHANGING  cv_salary TYPE p.

ENDCLASS.


CLASS lcl_class1 IMPLEMENTATION.

  METHOD istaxable.
    IF iv_salary > '500000'.    " If salary > 500000 then taxable
      rv_check = abap_true.
    ENDIF.
  ENDMETHOD.

  METHOD calculate_tax.
    CASE iv_grade.
      WHEN 'A01'.
        ev_tax = cv_salary * 10 / 100.
      WHEN 'A02'.
        ev_tax = cv_salary * 20 / 100.
      WHEN OTHERS.
        ev_tax = cv_salary * 30 / 100.
    ENDCASE.
    cv_salary = cv_salary - ev_tax.
  ENDMETHOD.
ENDCLASS.

DATA: lo_obj1   TYPE REF TO lcl_class1,
      lv_salary TYPE p,
      lv_tax    TYPE p.

START-OF-SELECTION.

  lv_salary = 600000.

  CREATE OBJECT lo_obj1.

*  DATA(lo_obj1) = NEW lcl_class1( ). " New Syntax

*Returning Parameter - importing parameters and changing parameters cannot be used in method call.
*                    - Returns Single Value

 IF lo_obj1->istaxable( iv_salary = lv_salary ) EQ abap_true. 

 
   WRITE: / 'Salary Before Tax:', lv_salary.
   lo_obj1->calculate_tax(  EXPORTING iv_grade  = 'A01'
                            IMPORTING ev_tax    = lv_tax
                            CHANGING  cv_salary = lv_salary   ).
   WRITE:/ 'Salary After Tax:', lv_salary,
         / 'Tax:', lv_tax.
 ENDIF.

In the IF condition of above program we can directly check if the amount is greater than '500000'. The logic is written just to explain the returning parameter.

Output


Post a Comment

Previous Post Next Post