Application Development and Automation 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: 
Read only

Recursion!

Former Member
0 Likes
3,247

I was so disappointed when i knew that i cannot implement recursion in abap as the normal way as the rest of the programming languages.

Some developers says that recursion can be implemented using loops, but still i don't why isn't it supported?!

Could you please help??

1 ACCEPTED SOLUTION
Read only

Aashish28
Contributor
0 Likes
2,608

Hiii,

       It's not true you can implement recursion , not as it is like c , c++ or java see my example with subroutine -

.

REPORT  ZRECURSION.

  PARAMETERS : p_fno TYPE i OBLIGATORY.

  START-OF-SELECTION.

  DATA : l_fno TYPE i.
  DATA : n TYPE i VALUE 1.

  l_fno = p_fno.

  PERFORM get_factorial CHANGING l_fno.

  WRITE : n.
*&---------------------------------------------------------------------*
*&      Form  GET_FACTORIAL
*&---------------------------------------------------------------------*
*       text : Factorial
*----------------------------------------------------------------------*
FORM GET_FACTORIAL  CHANGING P_L_ZROUN.

IF p_l_zroun = 0.
EXIT.
ELSE.

   p_l_zroun = p_l_zroun - 1 .
   n = n + n *  p_l_zroun .

   PERFORM get_factorial CHANGING P_L_ZROUN .

ENDIF.

ENDFORM.                    " GET_FACTORIAL

13 REPLIES 13
Read only

Former Member
0 Likes
2,608

I'm pretty sure that FM's can be recursive.  I wonder however how soon you would blow up the stack.

Neal

Read only

0 Likes
2,608

Thanks Neal, I know that i can do it using FM & loops, but i was wondering why its not a recursive function call like C++ or Java

Read only

Former Member
0 Likes
2,608

Hi Ahmed,

A simple recursion example in C, a factorial program of any number.

unsigned int factorial(unsigned int n) {

    if (n == 0) {

        return 1;

    } else {

        return n * factorial(n - 1);

    }

}

Neal is right. You can implement the same using FMs / program module / class method, depends on your requirement.

Recursion or recursive loop just a algorithm/concept of repetition of same logic/instruction executed based on certain rule/condition.

For the implementation of recursion, it is very depends to individual case.

Can you please tell what you are trying to achieve in SAP using ABAP?

Perhaps we can help further from there if you can tell further more on requirement.

Thanks.

regards,

Xiang Li

Read only

0 Likes
2,608

Hi Xiang,

FAGL_011PC table has to fields, Parent and Child.

What i want to do is to read all the children of each parent node using recursion call.

(its a tree)

Thanks.

Read only

0 Likes
2,608

I'm thinking that what you actually mean here is successors, not children.  In your first diagram, B only has 3 children but it has 5 successors.  I forget what you call the endnodes, but it has 4 of these.

If you want the children, recursion is a waste of time.

Could you give a little more clarification?

Neal

Read only

0 Likes
2,608

Hi Ahnmed,

I would need further input from you as below:

What is your expected result ?Can simulate the outcome for both tree?

Also, is there any way to determine the level of each note? example which field in that table determine the level.

Is there anyway to determine previous node and next node?

Thanks.

regards,

Xiang Li

Read only

former_member209120
Active Contributor
0 Likes
2,608

Hi Ahmed,

In ABAP we can implement  recursion using FM's,

See this link it may help you.

http://scn.sap.com/thread/1035419

Read only

Aashish28
Contributor
0 Likes
2,609

Hiii,

       It's not true you can implement recursion , not as it is like c , c++ or java see my example with subroutine -

.

REPORT  ZRECURSION.

  PARAMETERS : p_fno TYPE i OBLIGATORY.

  START-OF-SELECTION.

  DATA : l_fno TYPE i.
  DATA : n TYPE i VALUE 1.

  l_fno = p_fno.

  PERFORM get_factorial CHANGING l_fno.

  WRITE : n.
*&---------------------------------------------------------------------*
*&      Form  GET_FACTORIAL
*&---------------------------------------------------------------------*
*       text : Factorial
*----------------------------------------------------------------------*
FORM GET_FACTORIAL  CHANGING P_L_ZROUN.

IF p_l_zroun = 0.
EXIT.
ELSE.

   p_l_zroun = p_l_zroun - 1 .
   n = n + n *  p_l_zroun .

   PERFORM get_factorial CHANGING P_L_ZROUN .

ENDIF.

ENDFORM.                    " GET_FACTORIAL

Read only

Former Member
0 Likes
2,608

Hi Ashish,

I made some modification to your program. It is working now.

So, proven recursion is possible using ABAP, depends on your logic and the way you implement it.

Developer need to understand how the recursion algorithm works to implement it.

REPORT  YRECURSION.

   PARAMETERS : p_fno TYPE i OBLIGATORY.

   START-OF-SELECTION.

   DATA : l_fno TYPE i.

   DATA : l_result TYPE i.

   DATA : n TYPE i VALUE 1.

   l_fno = p_fno.

   PERFORM get_factorial USING l_fno CHANGING l_result.

   WRITE : l_result.

  FORM GET_FACTORIAL USING P_L_ZROUN CHANGING p_l_result.

  DATA : l_result TYPE i.

  DATA : l_fno TYPE i.

  IF p_l_zroun = 0.

    p_l_result = 1.

  ELSE.

    l_fno = p_l_zroun - 1 .

    PERFORM get_factorial USING l_fno CHANGING l_result .

    P_L_result = p_l_zroun * l_result.

  ENDIF.

  ENDFORM.                    " GET_FACTORIAL

regards,

Xiang Li

Read only

0 Likes
2,608

Hiii ,

           FYI it is already in working condition   about recursion yes i already mentioned in my previous post

Read only

Former Member
0 Likes
2,608

Thanks ASHISH

Read only

Former Member
0 Likes
2,608

Hi Ashish,

I understand yours is in working condition but not so close to recursion implementation, as in your program, the calculation being done by accumulate using global variable.

That's why i modified from your program to match to the recursion implementation as in the recursion example in C I provided earlier.

In conclusion we can implement the recursion logic in ABAP.

Again it is just a concept of logic how to get thing done. Unlikely it is has any dependent to any particular programming language to implement them.

Hope this make sense to you too.

Thanks.

regards,

Xiang Li

Read only

Former Member
0 Likes
2,608

Thanks for your help guys