Google Search - Blog...........

ABAP - Passing Internal Tables To A Subroutine.

Here is an simple program on how to pass an internal table to an sunbroutine
1)
PROGRAM FORM_TEST.
DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.
DATA ITAB LIKE STANDARD TABLE OF LINE.
PERFORM FILL CHANGING ITAB.
PERFORM OUT USING ITAB.
FORM FILL CHANGING F_ITAB LIKE ITAB.
DATA F_LINE LIKE LINE OF F_ITAB.
DO 3 TIMES.
F_LINE-COL1 = SY-INDEX.
F_LINE-COL2 = SY-INDEX ** 2.
APPEND F_LINE TO F_ITAB.
ENDDO.
ENDFORM.
FORM OUT USING VALUE(F_ITAB) LIKE ITAB.
DATA F_LINE LIKE LINE OF F_ITAB.
LOOP AT F_ITAB INTO F_LINE.
WRITE: / F_LINE-COL1, F_LINE-COL2.
ENDLOOP.
ENDFORM.
The produces the following output:
1 1
2 4
3 9
You can define the types of the formal parameters of the parameter interfaces of procedures as internal tables. In the example, the subroutines FILL and OUT each have one formal parameter defined as an internal table. An internal table without header line is passed to the subroutines. Each subroutine declares a work area F_LINE as a local data object. Were ITAB a table with a header line, you would have to replace ITAB with ITAB[] in the PERFORM and FORM statements.
2)
This example is provided for completeness. The TABLES parameter is only supported for the sake of compatibility and should not be used.
PROGRAM FORM_TEST.
TYPES: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.
DATA: ITAB TYPE STANDARD TABLE OF LINE WITH HEADER LINE,
JTAB TYPE STANDARD TABLE OF LINE.
PERFORM FILL TABLES ITAB.
MOVE ITAB[] TO JTAB.
PERFORM OUT TABLES JTAB.
FORM FILL TABLES F_ITAB LIKE ITAB[].
DO 3 TIMES.
F_ITAB-COL1 = SY-INDEX.
F_ITAB-COL2 = SY-INDEX ** 2.
APPEND F_ITAB.
ENDDO.
ENDFORM.
FORM OUT TABLES F_ITAB LIKE JTAB.
LOOP AT F_ITAB.
WRITE: / F_ITAB-COL1, F_ITAB-COL2.
ENDLOOP.
ENDFORM.
The produces the following output:
1 1
2 4
3 9
In this example, an internal table ITAB is declared with a header line and an internal table JTAB is declared without a header line. The actual parameter ITAB is passed to the formal parameter F_ITAB of the subroutine FILL in the TABLES addition. The header line is passed with it. After the body of the table has been copied from ITAB to JTAB, the actual parameter is passed to the formal parameter F_ITAB of the subroutine OUT using the TABLES addition. The header line F_ITAB, which is not passed, is generated automatically in the subroutine.

1 comment: