Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Recursion in ABAP

Former Member
0 Kudos

Hi All,

Is there any specific way by which we can achieve recursion in ABAP programming, or is there only the basic call-within-call structure which should contain an exit criteria?

If there is a specific way to achieve recursion, is there a concept of stack, something like we have in C++?

Please help me to understand this better.

Thanks and Regards,

Vidya.

7 REPLIES 7

kesavadas_thekkillath
Active Contributor
0 Kudos

check this recursion program example

Link:https://wiki.sdn.sap.com/wiki/display/Snippets/InverseBOMExplosion

0 Kudos

Thanks for your reply Keshu...I have seen the code and understand it. But in general, can you explain how recusion works in ABAP? Is something like a stack maintained? Can the results of the iterations be used in previous iterations, like it happens in C++?

Thank you all for your replies. I shall try the fibonacci series code and understand how recursion is achieved.

Regards,

Vidya.

Edited by: Vidya D on Sep 8, 2008 9:29 AM

bpawanchand
Active Contributor
0 Kudos

Hi

Check the below subroutine where it is calling the same function and it displays Fibonacci series

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.                               " END IF
ENDFORM.                               " END FORM

Regards

Pavan

Former Member
0 Kudos

Hi,

ABAP is also a programming language like C++. Here we can write subroutines and loops etc stuff.

Logically, Recursion is a loop kind of thing which runs till some condition is met.

Here also you can write a subroutine and pass the parameters to that subroutine with in a loop and endloop.

With this you can achieve the recursion even in abap also. The way of coding is different.

Hope this gives a clear picture.

Regards,

RSS.

Former Member
0 Kudos

Hi Vidya,

Check this example for recursion:

http://www.sap-img.com/abap/recursion-with-loop-checking-in-sap.htm

Regards,

Chandra Sekhar

Former Member
0 Kudos

Recursion can be achieved in ABAP also. Recursion is nothing but to execute a block of code repeatedly until some condition is met.

in ABAP we can use do loop like

do <n> times.

statements.

enddo.

or we can use while statement.

snijdersSAM
Explorer
0 Kudos

Hi,

May be a little late, but nevertheless.

-1-

If your recursion is defined within a database table. And the recursive data is found in that table. (think of tree like structures like a BOM).

In that case you might found it easy to use the CONNECT BY clause offered by oracle (embedded SQL) in order to traverse thrue the structure.

-2-

If your data can be represented in XML cannonical form, i would make use of XPATH expressions to traverse thrue those structures. Even when in the ABAP stack. (serialize <-> deserialize).

-3-

If your data can be represented in ABAP-Objects i would propose using linked lists or tree like structures to traverse thrue the structure.

Kind regards

Stef Snijders