Skip to Content
author's profile photo Former Member
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++?

Please help me to understand this better.

Thanks and Regards,

Vidya.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

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

    check this recursion program example

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

    Add a comment
    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_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

    Add a comment
    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,

    RSS.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.