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



Active Contributor
0 Kudos

check this recursion program example


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.



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

Active Contributor
0 Kudos


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



Former Member
0 Kudos


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.



Former Member
0 Kudos

Hi Vidya,

Check this example for recursion:


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.



or we can use while statement.

0 Kudos


May be a little late, but nevertheless.


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.


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


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