Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
vinu_Kurian
Active Participant
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.

CODE: 
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.

ELSE.

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.

ENDIF.

ENDFORM.

NB : Above code was found from a comment on blog : https://answers.sap.com/questions/4715674/recursion-in-abap.html#:~:text=Recursion%20can%20be%20achi...

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.

Code:
class ZTRAININGC definition
public
final

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.

endclass.
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.

OUT->WRITE( PREV ).

OUT->WRITE( NEXT ).

call method RECURSION "Initial Call

changing

PREV = PREV

NEXT = NEXT.

OUT->WRITE( IT_FIBONACCI ).

endmethod.


method RECURSION. "Recursing Method

data:TEMP type STRING.

if NEXT < 100. "Condition for Recursion

TEMP = PREV.

PREV = NEXT.

NEXT = TEMP + PREV.

WA_FIBONACCI = NEXT.

append WA_FIBONACCI to IT_FIBONACCI.

call method RECURSION "Recurring Call

changing

PREV = PREV

NEXT = NEXT.

endif.

endmethod.

endclass.

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 .
3 Comments
Labels in this area