Skip to Content
Technical Articles
Author's profile photo Vinu Kurian

Recursion – Business Technology Platform (Cloud Platform) ABAP

Definition: Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

How it is achieved on SAP ERP?

                   In SAP Classic ABAP it was achieved using PERFORM (usually used to repeatedly call forms) statement. Let’s Look at the below code to make an understanding.


FORM fibo  USING   value(p_p_num)

           CHANGING p_w_res.

  DATA :

     w_temp TYPE i,                    " Temporary Variable

     w_no1 TYPE i,                     " Number Variable One

     w_no2 TYPE i,                     " Number Variable TWO

     w_add TYPE i.                     " Stores Sum Of Above 2 Variables

  w_temp = p_p_num.

  IF w_temp EQ 1 OR w_temp EQ 2.

    p_w_res = 1.


    w_add = 0.

    w_no1 = w_temp - 1.

    w_no2 = w_temp - 2.

    PERFORM fibo USING w_no1 CHANGING p_w_res.

    w_add = w_add + p_w_res.

    PERFORM fibo USING w_no2 CHANGING p_w_res.

    w_add = w_add + p_w_res.

    p_w_res = w_add.



NB : Above code was found from a comment on blog :,until%20some%20condition%20is%20met.&text=do%20times.,statements.

Commented by : @pavan.bhamidipati

Why it is not possible to do same in Cloud?

                             ABAP on cloud does not support PERFORM statement as well as FORMS, so we should have to think of another way to achieve this. As far as my thoughts and experience in this platform, we could achieve recursion by using either Class -> Method or by using Function Modules. Since each Function Module is a part of Function Group and at the time of execution the entire Function Group (Function Groups contain other Function Modules as well) would be invoked thus taking up more space and time. So I would be recommending classical Class -> Method.

How are we going to do it in Cloud?

Since FORMS are not supported in sap cloud platform, we will be using methods to achieve recursion.

In our solution we will be finding Fibonacci series up to a certain limit, for this we will be having to methods, one which implements IF_OO_ADT_CLASSRUN interface so that we could run the class and see the results in console which will also invoke the recurring method. The recurring method will keep on recurring till a specified condition is met. I have used Changing attribute in method but you could change it to Importing / Exporting according to your requirements.


class ZTRAININGC definition

  create public .
  public section.

    interfaces IF_OO_ADT_CLASSRUN.

    methods RECURSION

      changing PREV type STRING

               NEXT type STRING.

    class-data: IT_FIBONACCI type table of STRING. "Global Table to Store Fibonacci Numbers

    class-data: WA_FIBONACCI like line of   IT_FIBONACCI. "Global Work Area for Internal Table

  protected section.

  private section.

class ZTRAININGC implementation.

  method IF_OO_ADT_CLASSRUN~MAIN. "Parent Method From Which Recurring Method is nitially Called

    data:NEXT type STRING.

    data:PREV type STRING.

    PREV = 0.

    NEXT = 1.



    call method RECURSION  "Initial Call


        PREV = PREV

        NEXT = NEXT.



  method RECURSION. "Recursing Method

    data:TEMP type STRING.

    if NEXT < 100. "Condition for Recursion

      TEMP = PREV.

      PREV = NEXT.

      NEXT = TEMP + PREV.



      call method RECURSION "Recurring Call


          PREV = PREV

          NEXT = NEXT.




Conclusion : Almost all the features and techniques used in classic abap could also be implemented in cloud platform as well in one way or other . Using recursion saves both time and space also reducing the number of lines of the program . I recommend using the Object Oriented Programming in cloud platform . Try to achive maximum code reusability, currently in this blog Iam just discussing one solution which suited me well in few of the business scenarios related to Sales and Distribution.


NB: This blog is subjected to be edited when more convenient ways of code / methods are learnt. There are many methods to achieve recursion in sap cloud platform but I have described the best solution which suited me for many scenarios .

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Felipe Silva
      Felipe Silva

      Hi Vinu,

      Thumbs up for going the OO way, there is a lot of advantages using a good OO design.

      And I didn't knew about IF_OO_ADT_CLASSRUN!

      Thanks for that



      Author's profile photo Frederik Hudak
      Frederik Hudak

      Since each Function Module is a part of Function Group and at the time of execution the entire Function Group (Function Groups contain other Function Modules as well) would be invoked thus taking up more space and time.

      Do you have any resources or measurements which support this statement? I'm not saying you're wrong, I actually want you to be right. It would be another good supporting argument for refactoring old code.

      Author's profile photo Vinu Kurian
      Vinu Kurian
      Blog Post Author

      I have actually read it like :

      Function Groups

      Function groups are containers for function modules.You cannot execute a function group. When you call a function module, the system loads the whole of its function group into the internal session of the calling program. 

      From this blog while learning :