You are on page 1of 31

Modularizing ABAP/4 Programs

Modularizing ABAP/4 Programs

Objective:
The following section explains :
• Include Programs
• Macros
• Subroutines
• Passing Data
• Terminating Subroutines
• Field Symbols
• Clusters
Modularizing ABAP/4 Programs
Each ABAP/4 program has a modular structure.

ABAP/4 programs are divided into:

· Processing blocks that are controlled by events when the program is a


report program ( program of type 1).

· Modules that are controlled by the screen flow logic when the program
is an dialog program (module pool of type M). You can modularize either
your source code or tasks which are to be performed repeatedly.
Source Code Modularization

To modularize source codes the following two methods apply:

a. Include programs

b. Macros

To modularize tasks the following two methods apply:

a. Subroutine

b. Functional modules
Include Programs

If you want to use the same sequence of statements in several programs,


you can code them once in an include program.

Example - In the case of lengthy data declarations.

How to create include programs ?

How to use Include programs ?


Defining and Calling Macros

You can create callable modules of program code within your ABAP/4
program by defining macros.

To define a macro which contains part of the source code, you use the
DEFINE statement as follows:

Syntax:
DEFINE <macro>.
<statements>
END-OF-DEFINITION.

This defines the macro <macro>. You must specify complete statements
between DEFINE and END-OF-DEFINITION.
Subroutines

Subroutines are program modules which can be called from ABAP/4


programs.

There are two types of subroutines.

· Internal subroutines:
The source code of internal subroutines is in the same ABAP/4 program
as the calling procedure (internal call).

· External subroutines:
The source code of external subroutines is in an ABAP/4 program other
than the calling procedure (external call).
Defining Subroutines

A subroutine is a block of code introduced by FORM and concluded by


ENDFORM. To define a subroutine, use the following syntax:

Syntax:
FORM <subr> [<pass>].
<statement block>
ENDFORM.

<subr> - The name of the subroutine.


<pass> - You specify how to pass data to and from the
subroutine {mainly for external call}.
Calling Subroutines

1. Calling Internal Subroutines:

To call an internal subroutine, use the PERFORM statement as follows:


Syntax:
PERFORM <subr> [<pass>].

2. Calling External Subroutines:

To call an external subroutine, use the PERFORM statement as follows:


Syntax:
PERFORM <subr>(<prog>) [<pass>] [IF FOUND].
Calling Subroutines

Note:

· In the case of internal subroutines, you do not have to use the <pass>
option, because the subroutine can access all data objects declared in
the main ABAP/4 program.

· In the case of external subroutines, you must decide whether to use the
<pass> option or declare data objects in common parts of the memory.
Passing Data

Passing Data Between Calling Programs and Subroutines:

1. Declaring Data as Common Part

2. Passing Data by Parameters


Declaring Data As Common Part

To declare data objects as common part, use the DATA statement as


follows:

Syntax:

DATA: BEGIN OF COMMON PART [<name>],


<data declaration>,
..............
END OF COMMON PART [<name>].

You can use several common parts in one program. In this case, you
must assign a name <name> to each common part. If you use only one
common part per program, the name <name> is optional.
Passing Data By Parameters

You can pass data between calling programs and subroutines by using
parameters.
· Parameters which are defined during the definition of a subroutine with
the FORM statement are called formal parameters.
· Parameters which are specified during the call of a subroutine with the
PERFORM statement are called actual parameters.

Syntax:
PERFORM <subr>[(<prog>)] [TABLES <actual table list>]
[USING <actual input list>]
[CHANGING <actual output list>]

FORM <subr> [TABLES <formal table list>]


[USING <formal input list>]
[CHANGING <formal output list>]
Different Methods Of Passing Data

Calling by Reference : During a subroutine call, 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,
the field contents in the calling program also change.

Calling by Value : During a subroutine call, the formal parameters are


created as copies of the actual parameters. The formal parameters have
memory of their own. Changes to the formal parameter have no effect on
the actual parameter.

Calling by Value and Result : During a subroutine call, the formal


parameters are created as copies of the actual parameters. The formal
parameters have their own memory space. Changes to the formal
parameters are copied to the actual parameter at the end of the
subroutine.
Passing By Reference

To pass data between calling programs and subroutines by reference,


use USING or CHANGING for the <pass> option of the FORM and
PERFORM statements as follows:

Syntax:
PERFORM... [USING <ai1> ... <ain>] [CHANGING <ao1> ... <aon>] …
FORM ..... [USING <fi1> ... <fin>] [CHANGING <fo1> ... <fon>] ...

You specify the formal and actual parameters in the list behind USING
and CHANGING without any addition.
Passing By Value

To ensure that an input parameter is not changed in the calling program,


even if it is changed in the subroutine, you can pass data to a subroutine
by value. For this, use USING for the <pass> option of the FORM and
PERFORM statements as follows:

Syntax:
PERFORM... USING .......<aii> ..
FORM ..... USING ...VALUE(<fii>) ..
Passing By Value And Result

If you want to return a changed output parameter from a subroutine to the


calling program only after the subroutine has run successfully, use
CHANGING for the <pass> option of the FORM and PERFORM statements
as follows:

Syntax:
FORM ..... CHANGING ...VALUE(<fii>) ..
PERFORM... CHANGING .......<aii> ..
Example

REPORT FORMTEST.

DATA:
DATE1 TYPE D, DATE2 TYPE T,
STRING1(6) TYPE C, STRING2(8) TYPE C,
NUMBER1 TYPE P DECIMALS 2, NUMBER2 TYPE P,
COUNT1 TYPE I, COUNT2 TYPE I.
PERFORM TYPETEST USING DATE1 STRING1 NUMBER1 COUNT1.
PERFORM TYPETEST USING DATE2 STRING2 NUMBER2 COUNT2.
Example

Cont..

FORM TYPETEST USING NOW


TXT TYPE C
VALUE(NUM) TYPE P
INT TYPE I.
DATA: T.
DESCRIBE FIELD NOW TYPE T.
WRITE: / 'Type of NOW is', T.
DESCRIBE FIELD TXT LENGTH T.
WRITE: / 'Length of TXT is', T.
DESCRIBE FIELD NUM DECIMALS T.
WRITE: / 'Decimals of NUM are', T.
DESCRIBE FIELD INT TYPE T.
WRITE: / 'Type of INT is', T.
ENDFORM.
Example

Example of passing internal tables:

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.


Example
Cont..

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.
Terminating Subroutines

To terminate the processing of a subroutine, you can proceed in a similar


way as for terminating a loop by using the EXIT or CHECK statements.

· Use EXIT to terminate a subroutine unconditionally.


· Use CHECK to terminate a subroutine according to a condition.

If you terminate a subroutine using EXIT or CHECK, the system


terminates the processing of the subroutine at this point, passes the
parameters, and continues with the statement after the PERFORM
statement.
Field Symbols

Field symbols are placeholders or symbolic names for other fields. They
do not physically reserve space for a field, but point to its contents. A
field symbol can point to any data object. The data object to which a field
symbol points is assigned to it after it has been declared in the program.

To declare a Field Symbol, use the statement,


FIELD-SYMBOLS <FS> [<type>|STRUCTURE <s> DEFAULT <wa>].

Typing Field Symbols:


The <type> addition allows you to specify the type of a field symbol.
Syntax:
FIELD-SYMBOLS <FS> <type>
Field Symbols

Example:

DATA: WA(10) VALUE '0123456789'.


DATA: BEGIN OF LINE1,
COL1(3),
COL2(2),
COL3(5),
END OF LINE1.

DATA: BEGIN OF LINE2,


COL1(2),
COL2 LIKE SY-DATUM,
END OF LINE2.
Field Symbols

Cont..

FIELD-SYMBOLS: <F1> STRUCTURE LINE1 DEFAULT WA,


<F2> STRUCTURE LINE2 DEFAULT WA.
WRITE: / <F1>-COL1, <F1>-COL2, <F1>-COL3,
/ <F2>-COL1, <F2>-COL2.

The output is:

012 34 56789
01 2345/67/89
Field Symbols
Static Assign:
The name of the data object you want to assign to a field symbol before
run time.
Syntax:
ASSIGN <f> TO <FS>.
Eg:
FIELD-SYMBOLS: <F1> , <F2> TYPE I.
DATA : NUM TYPE I VALUE 5,
TEXT(10) TYPE C VALUE ‘HELLO’.
ASSIGN TEXT TO <F1>.
ASSIGN NUM TO <F2>.
WRITE: / <F1> , <F2>.

OUTPUT:
HELLO 5
Field Symbols

Dynamic Assign:
The name of the data object you want to assign to a field symbol only at
run time.
Syntax:
ASSIGN (<f>) TO <FS>.
Eg:
PROGRAM SAPMZTST.
PERFORM FORM1(SAPMZTST1). * Calling Subroutine
PROGRAM SAPMZTST1.
FORM FORM1.
FIELD-SYMBOLS <F1>.
DATA : NUM TYPE N VALUE 5.
ASSIGN (NUM) TO <F1>.
WRITE: / <F1>.
ENDFORM.
OUTPUT: 5
Clusters
Data Clusters

You can store data clusters in ABAP/4 memory.


ABAP/4 memory is not dependent on the ABAP/4 program or program
module which generates it during a transaction. This means that an
object stored in ABAP/4 memory can be read again by any ABAP/4
program during the same transaction.

For example, you can pass data between:


· Reports and other Reports called with SUBMIT
· Transactions and Reports
· Programs and Function Modules

Note :
The memory is released again when you leave the transaction.
Storing Data Objects In ABAP/4 Memory

To write data objects from an ABAP/4 program to ABAP/4 memory, you


use the following statement:

Syntax:

EXPORT <f1> <f2> ... TO MEMORY ID <key>.


Reading Data Objects From Memory

To read data objects from ABAP/4 memory into an ABAP/4 program, you
use the following statement:

Syntax:

IMPORT <f1> [TO <g1>] <f2> [TO <g2>] ... FROM MEMORY ID <key>.

You might also like