banner ads

Method can be called from the same Method

A method can call itself by Call Method statement inside the implementation of the method. Inside there method will have to be declared directly as CALL METHOD METH. That means object will not be mentioned here. Important point is that the method must have an EXIT point to get outside of the method. Otherwise the method will fall in an infinite calling/looping.

Below is a program where we create a numeric table. Here we are getting the number by input parameter and multiplying it with the count. This count is initially 0 and it is gradually increased by 1. And the multiplied value is stored in a public variable and then it is written. Hence we are having the numeric table of input value. Inside the method we have called an Exit point. If the count is greater than 20 then it will get outside from the method otherwise it will continue for calling the method.

REPORT  zsr_test NO STANDARD PAGE HEADING.

PARAMETERS p_num TYPE i.

*----------------------------------------------------------------------*
*       CLASS cls DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.
  PUBLIC SECTION.
    DATAcount TYPE i,
          value TYPE i.
    METHODS m_tab.
ENDCLASS.                    "cls DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.
  METHOD m_tab.
    count = count + 1.
    IF count GT 20.
      EXIT.
    ELSE.
      value = count * p_num.
      WRITE: / p_num, '*'count'='value.
      CALL METHOD m_tab.
    ENDIF.
  ENDMETHOD.                    "m_tab
ENDCLASS.                    "cls IMPLEMENTATION

START-OF-SELECTION.
  DATA obj TYPE REF TO cls.
  CREATE OBJECT obj.

  WRITE: /10 'Table of ', p_num, ':',
         /10 '-----------------------'.
  SKIP.
  CALL METHOD obj->m_tab.

The program generates the following output.


No comments