Académique Documents
Professionnel Documents
Culture Documents
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.
· 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
a. Include programs
b. Macros
a. Subroutine
b. Functional modules
Include Programs
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
· 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
Syntax:
FORM <subr> [<pass>].
<statement block>
ENDFORM.
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
Syntax:
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>]
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
Syntax:
PERFORM... USING .......<aii> ..
FORM ..... USING ...VALUE(<fii>) ..
Passing By Value And Result
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..
PROGRAM FORM_TEST.
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.
Example:
Cont..
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
Note :
The memory is released again when you leave the transaction.
Storing Data Objects In ABAP/4 Memory
Syntax:
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>.