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: 

internal tables in recursive perform

Former Member
0 Kudos
692

Just to clarify

If I create an internal table in a procedure (perform). And if the procedure is recursive. It will create in stack n internal tables, one for every call to the procedure?

TIA

16 REPLIES 16

former_member188685
Active Contributor
0 Kudos
192

Hi ,

It depends up on how You are passig the parameters to the Subroutine(perform).

I mean to say if u are calling this peroform for n number of times , u are not passing any thing to the preform to create the table. in that case it will create only one table and it will be a kind of stack

Regards

vijay

Message was edited by: Vijay Babu Dudla

0 Kudos
192

Sorry I don't understand your reply. The internal table is created inside the procedure, it's not passed as a parameter

0 Kudos
192

I think the internal table will be created with every call of the routine, but I don't think it will be a stack. Since this is a local variable, its scope is limited to the routine and every time the routine is called, it will be initialized.

0 Kudos
192

I will Try to clarify with some pseudocode

Perform x

DATA: begin of recursiveTable

include structure xxx

end of recursiveTable

Loads the table

Loop

Perform x with new parameters

EndLoop

EndForm

As you can see inside the procedure, the internal table is created, loaded and looped. Inside the loop, the procedure calls itself. My question is, when the procedure calls itself and returns. The first table will still exist with its information or the will be destroyed?

0 Kudos
192

Hi U will loose the previos table data.

every time it is getting initialized.

that will definitely removes the data.

regards

vijay

0 Kudos
192

Every time your "perform x with new parameters" is triggered,it refreshes the table.But the problem is you will never be able to come out the loop.It will become an infinite loop.

0 Kudos
192

I checked myself. As every language with recursive capabilities, if a variable is declared locally, and the program calls itself, ABAP creates a stack of variables, when the nested call returns the control to the caller, the original variable exist again.

It's not an infite loop, for simplification I ommited the stop criteria. Every recursiver procedure has to have a stop criteria.

Thanks anyway.

0 Kudos
192

You are right Rodolfo, I just used the same program but moved the itab to local definition... surprise, surprise.. it does stack them up...

Here is the modified program.

REPORT ztest1 .

DATA: counter TYPE i VALUE 1.

WRITE:/ 'Before = ', counter.

PERFORM recursion CHANGING counter.

WRITE:/ 'After = ', counter.

*---------------------------------------------------------------------*
*       FORM recursion                                                *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM recursion CHANGING counter.

  DATA: BEGIN OF itab OCCURS 0,
          counter TYPE i.
  DATA: END OF itab.

  IF counter = 10.
    EXIT.
  ENDIF.

  itab-counter = counter.
  APPEND itab.

  counter = counter + 1.

  PERFORM recursion CHANGING counter.
  LOOP AT itab.
    WRITE:/ itab-counter.
  ENDLOOP.

ENDFORM.

I got the output in the reverse order though as follows

Before =           1 
         9           
         8           
         7           
         6           
         5           
         4           
         3           
         2           
         1           
After =          10  

0 Kudos
192

The only thing to remember is that the internal table itself has only one record for each call of the routine, but there are nine such internal table instances with one record each.

Former Member
0 Kudos
192

Hi There,

No, it does not create a stack of internal tables.

This is how it works:

For each call to the procedure, when the control of the program transfers to the FORM, the instance of the internal table is created, after all the processing is done and the control comes out of the procedure(after the ENDFORM statement), the instance of the internal table is lost(similar to FREE ITAB). And for the next call of the procedure, again an instance of the internal table is created.

Cheers,

Naveen

Former Member
0 Kudos
192

It will not create the Internal table unless the procedure or the subroutine is called.When you are out of this procedure or subroutine,then you loose the access to the table as well as the data in the internal table.

Former Member
0 Kudos
192

Hi

It will not create a stack. Since it is a local internal table , every time you call the perform new internal table gets declared.

Regards

Kalpana

0 Kudos
192

On every new call since the internal table is declared locally , the data gets destroyed .

- Kalpana

0 Kudos
192

As you must have already realized that the scope of local variables is destroyed once the routine is exited. You can do something like this though with global variables.

REPORT ztest1 .

DATA: BEGIN OF itab OCCURS 0,
        counter TYPE i.
DATA: END OF itab.

DATA: counter TYPE i VALUE 1.

WRITE:/ 'Before = ', counter.

PERFORM recursion CHANGING counter.

WRITE:/ 'After = ', counter.
LOOP AT itab.
  WRITE:/ itab-counter.
ENDLOOP.

*---------------------------------------------------------------------*
*       FORM recursion                                                *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM recursion CHANGING counter.


  IF counter = 10.
    EXIT.
  ENDIF.

  itab-counter = counter.
  APPEND itab.

  counter = counter + 1.

  PERFORM recursion CHANGING counter.

ENDFORM.

former_member188685
Active Contributor
0 Kudos
192

Hi,

is this u are looking for ,if that is the case it is getting stacked pplease check this.

data: begin of itab occurs 0,

name(10) type c,

id(10) type c,

end of itab.

parameters: num type i.

do num times.

perform test_form.

enddo.

form test_form.

itab-name = 'vijay'.

itab-id = 'abcdef'.

append itab.

endform.

end-of-selection.

loop at itab.

write:/ itab-name,

itab-id.

endloop.

Former Member
0 Kudos
192

I tried this with simple fields rather than an internal table, but the results are interesting:


REPORT ztest.

DATA number LIKE sy-tabix.

break-point.

PERFORM recursive CHANGING number.
*&---------------------------------------------------------------------*
*&      Form  recursive
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM recursive CHANGING p_number.

  DATA time LIKE sy-uzeit.

  p_number = p_number + 1.
  time     = sy-uzeit.

  IF p_number < 10.
    PERFORM recursive CHANGING p_number.

    WRITE: /001 p_number, time.
  ENDIF.

ENDFORM.                    " recursive

NUMBER always takes the last value, but time is stacked. So I think an internal table created within the form would be stacked.

Rob