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

Subroutines In SAP ABAP.

The process of breaking down a large program into smaller modules is supported by ABAP/4 through subroutine, also called forms.

Subroutines are programs modules, which can be called from ABAP/4 programs. Frequently used parts of program can be put into subroutines and these subroutines can be called explicitly from the program. You use subroutines mainly to modularize and structure your program.

Defining Subroutines

A subroutine is block of code introduced by FORM and concluded by ENDFORM. Following syntax is used to define a form or subroutine:

FORM
. . …..

ENDFORM.

Here parameters is optional. You can have plain subroutine without the parameters for example.

FORM SUB1.
… . .
… .
ENDFORM.


Calling Subroutines

You can call subroutines from program by following statement:

PERFORM [].

Parameters are optional. i.e., you can call subroutine without passing any parameter

Perform SUB1.

Passing Data to Subroutines

When you work with global data in subroutines, you can put a copy of the global data on a local data stack and use it to avoid accidental loss of data (In this way you protect global data.)

You can pass data between calling program and subroutines by using parameters.

• Parameters, which are defined during definition of a subroutine with FORM statement are called ‘formal parameter’.
• Parameters which are specified during the call of a subroutine with the PERFORM statement are called ‘actual parameter’.


Parameters are passed to the FORM either:

• By value
• By Reference
• By value and return.

By Value

Data : a type I value 20.
Perform sub1 using a.
Write a.
FORM sub1 using value (p_a)
P – a = 15
ENDORM.
In this case during subroutine call, the formal parameter are created as copies of actual parameter.
The formal parameters have the memory of their own. Changes made to formal parameter have no effect on the actual parameter.

Like in this case, though value of p_a is changed to 15, it has no effect on ‘a’ which remains as 20.

By Reference

Data: a type I value 20.
Perform sub1 using a.
Write a.
FORM sub1 using value (p_a)
P – a = 15.
ENDORM.
By default system calls all the forms by reference.

In this case, only the address of the actual parameter is transferred to the formal parameters. The formal parameter has no memory of its own. If you change the formal parameter, change is visible in actual parameter also.

By Value and Return

Data : a type I value 20.
Perform sub1 changing a.
FORM sub1 changing value (p_a)
P – a = 15.
ENDORM.
In this case if you change formal parameter, then the value of actual parameter is changed when the control is transferred back to the main program.

Assuming A is declared by DATA statement and has value 20 and subroutine SUB1 is called by passing A.

CALLING FORM VALUE OF A IN PROGRAM VALUE OF A IN FORM
(p_a = 15)
BEFORE CALLING FORM A = 20
A = 20 P_A = 100
BY VALUE
(USING)

AFTER RETURNING FROM FORM A = 20 (changing value of p_a)
A = 20.
BEFORE CALLING FORM A = 20
A = 20 P_A = 100
BY REFERENCE
(USING) AFTER RETURNING FROM FORM A = 20 (changing value of p_a)
A = 100
BE VALUE AND RETURN BEFORE CALLING FORM A = 20
A = 20 P_A = 100
(CHANGING) AFTER RETURNING FROM FORM A = 100 (changing value of p_a)
A = 100.

Passing Table to a Subroutine

You can pass internal tables as parameters USING or CHANGING in the FORM and PERFORM statements. If you want to access the components of the internal table, you must specify the type of the corresponding formal parameter.

You also must distinguish between internal tables with or without header lines. For internal tables with header lies, you must specify the table body by using square brackets [ ], after the table name to distinguish it from the header line.

With internal subroutines, you can use TYPE or LIKE to refer to the internal table you want to pass directly.

You can pass all internal tables as parameters in the list after TABLES in the FORM and PERFORM statements. Internal tables passed with TABLES are always called by reference.

If you pass all internal table with a header line, the table body and the table work area are passed to the subroutine. If you pass an internal table without a header line, a header line is created automatically as a local data object in the subroutine.

PROGRAM ZDEMO
DATA: Begin of itab occurs 0,
Number type I,
end of itab

PERFORM SUB1 TABLES ITAB.

LOOP AT ITAB.
WRITE: / itab-number.
ENDLOOP.

FORM SUB1 TABLES F_ITAB LIKE ITAB [ ].
DO 3 TIMES.
F_itab-number = SY-INDEX.
APPEND F_ITAB.
ENDDO.
ENDFORM.

After starting ZDEMO the output appears as follows:

1
2
3

In this example, an internal table ITAB is declared with a header line. ITAB is passed to the subroutine SUB1, where it is filled using the table work area F_ITAB. And itab is written in main program.

No comments:

Post a Comment