Former Member

# Recursion in ABAP

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++?

Thanks and Regards,

Vidya.

10|10000 characters needed characters exceeded

### Related questions

• Posted on Sep 08, 2008 at 07:21 AM

check this recursion program example

10|10000 characters needed characters exceeded
• Former Member

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

• Posted on Sep 08, 2008 at 07:22 AM

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_no1 = w_temp - 1.
w_no2 = w_temp - 2.
PERFORM fibo USING w_no1 CHANGING p_w_res.
PERFORM fibo USING w_no2 CHANGING p_w_res.
ENDIF.                               " END IF
ENDFORM.                               " END FORM```

Regards

Pavan

10|10000 characters needed characters exceeded
• Posted on Sep 08, 2008 at 07:24 AM

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,

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 08, 2008 at 07:31 AM

Hi Vidya,

Check this example for recursion:

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

Regards,

Chandra Sekhar

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 08, 2008 at 08:14 AM

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.

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 08, 2010 at 06:10 AM

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