Wednesday, October 13, 2010

Macros

If you want to reuse the same set of statements more than once in a program, you can include them in a macro. For example, this can be useful for long calculations or complex WRITE statements. You can only use a macro within the program in which it is defined, and it can only be called in lines of the program following its definition.

The following statement block defines a macro macro:

DEFINE makro.

statements

END-OF-DEFINITION.

You must specify complete statements between DEFINE and END‑OF‑DEFINITION. These statements can contain up to nine placeholders &1, &2,...., &9). You must define the macro before the point in the program at which you want to use it.

Macros do not belong to the definition part of the program. This means that the DEFINE statement block is not interpreted before the processing blocks in the program. At the same time, however, macros are not operational statements that are executed within a processing block at runtime. When the program is generated, macro definitions are not taken into account at the point at which they are defined. For this reason, they do not appear in the overview of the Structure of the Processing Logic [Structure of the Processing Logic-- see at the last of the discripation].

A macro definition inserts a form of shortcut at any point in a program and can be used at any subsequent point in the program. As the programmer, you must ensure that the macro definition occurs in the program before the macro itself is used. Particular care is required if you use both macros and include programs, since not all include programs are included in the syntax check (exception: TOP include).

To use a macro, use the following form:

makro [p1 p2... p9].

When the program is generated, the system replaces the macro by the appropriate statements and the placeholders &1, &2, …, &9 by the parameter p1, p2, …, p9. You can use macros within macros. However, a macro cannot call itself.


REPORT demo_mod_tech_macros.

DATA: result TYPE i,
n1 TYPE i VALUE 5,
n2 TYPE i VALUE 6.

DEFINE operation.
result = &1 &2 &3.
output &1 &2 &3 result.

END-OF-DEFINITION.

DEFINE output.
write: / 'The result of &1 &2 &3 is', &4.
END-OF-DEFINITION.

operation 4 + 3.
operation 2 ** 7.
operation n2 - n1.

This produces the following output:

The result of 4 + 3 is 7

The result of 2 ** 7 is 128

The result of N2 - N1 is 1

In this example, the two macros operation and output are defined. output is nested in operation. operation is called three times with different parameters. Note how the placeholders &1, &2,... are replaced in the macros.


The following example shows that a macro definition only works in the program lines following its definition. Do not copy it!

Let us have a look at a program with a subroutine test:

PROGRAM macro_test.

...

FORM test.
WRITE '...'.
ENDFORM.

We can rewrite the program by introducing a macro macro:

PROGRAM macro_test.

...

FORM test.

DEFINE macro.
WRITE '...'.
ENDFORM.
END-OF-DEFINITION.

MACRO.

Inserting the macro changes nothing in the generated form of the program. Processing blocks - here a subroutine - are always indivisible. We could also write the program as follows:

PROGRAM macro_test.

...

DEFINE macro.
WRITE '...'.
ENDFORM.
END-OF-DEFINITION.

...

FORM test.

MACRO.

The most essential feature of a macro definition is that it should occur before the macro is used.

___________________________________________________________________

Structure of the Processing Logic

Each ABAP program consists of an introductory program part, a global declaration part and a number of processing blocks which implement different functions depending on the application (procedures, dialog modules, event blocks). Processing blocks are inseparable syntactical modularization units which cannot comprise any other processing block. Normally they contain several ABAP statements which are organized into statement blocks by means of control structures.

The following diagram shows the structure of an ABAP program:

This graphic is explained in the accompanying text

Introductory Program Part

Every independent ABAP program begins with an introductory statement which depends on the program type. Some of these statements have additions which you can use to make program settings.

Declaration Part for Global Data, Classes and Selection Screens

In every ABAP program, the introductory program part is followed by the declaration part for global data, classes, and selection screens. This consists of:

· All declaration statements for global data. Global data is visible in all internal processing blocks. You define it using declarative statements that appear before the first processing block, in dialog modules, or in event blocks. You cannot declare local data in dialog modules or event blocks except the following:

· All selection screen definitions.

· All declarations of internal class definitions and interfaces.

Declaration statements which occur in procedures (methods, subroutines, function modules) form the declaration part for local data in those processing blocks. This data is only visible within the procedure in which it is declared.

Container for Processing Blocks

You can list the processing blocks of an ABAP program in the source code of the program in any sequence you like below the global declaration part as they are not executed consecutively. Only the source code contained in a statement block is executed sequentially.

The following types of processing blocks are available which can be exited prematurely using the RETURN statement.

· Dialog modules (no local data area)

· Event blocks (no local data area, with two exceptions)

· Procedures (methods, subroutines and function modules with their own local data area).

Whereas dialog modules and procedures are enclosed in the ABAP keywords which define them, event blocks are introduced with event keywords and concluded implicitly by the beginning of the next processing block.

All ABAP statements (except declarative statements in the declaration part of the program) are part of a processing block. Non-declarative ABAP statements, which occur between the declaration of global data and a processing block are automatically assigned to the START-OF-SELECTION processing block.

No comments:

Tutorials on SAP-ABAP

Adobe Interactive Forms Tutorials

Business Server Pages (BSP)

Userexits/BADIs

Web Dynpro for ABAP (Step by step procedure for web dynpro,Tutorials on Web Dynpro,)

ALV Tutorials

Blog Archive

goodsites