You are on page 1of 9

Modularization in ABAP

Modularization is breaking down the application code into smaller units, so that it is easy
to maintain. Suppose if we want to implement the same logic like adding two numbers in
several places of the same program, then write the logic inside a modularization unit and
call the modularization unit where ever we want to add two numbers.

Even if we want to declare the same variables in multiple programs or use the same logic
in different programs then code the common part in the modularization unit and use it in
different programs.

Advantages of Modularization are as follows.

 Easier to read, maintain and debug.


 Eliminates redundancy of code and increases reusability of code .

Different types of Modularization units called from ABAP programs.

 Macros – If you want to reuse the same set of statements more than once in a
program, you can include them in a macro. You can only use a macro within the
program in which it is defined.
 Include Programs – Include programs allow you to use the same source code in
different programs. They are mainly used for modularizing source code and have
no parameter interface.
 Subroutines – Subroutines are normally called internally i.e. called  from the
same program in which it is declared. But subroutines can also be called from
external programs.
 Function Modules – Function Modules are stored in the central library and can
be called from any program. Even the function modules(RFC enabled) can be
called from non-SAP systems.
 Methods – Methods used ABAP object oriented programming

Different types of Modularization units called from ABAP runtime.

 Event Blocks – Event blocks begin with a event code and ends with next
processing block.
 Dialog Modules – Dialog Modules are used in PBO and PAI events of Module
Pool programs.

Include programs are not standalone programs and cannot be executed independently.
They can be used as a container for ABAP source code. They are used to organize the
ABAP source code into small editable units which can be inserted at any place in other
ABAP programs using the INCLUDE statement. Include programs can be used in
different programs. The include statement copies the contents of the include program into
the main program.
Include programs have no parameter interface and they cannot call themselves.

While creating the INCLUDE program using the ABAP editor choose program type as
"I" i.e. INCLUDE program in the program attributes.

Syntax for Include program is as follows.

INCLUDE <Include Program Name>

Source code of ZINCLUDE_DATA.

DATA: g_name(10) TYPE c.

Source code of ZINCLUDE_WRITE.

WRITE:/ 'Inside include program'.

Source code of main program.

REPORT zmain_program.
INCLUDE zinclude_data.

WRITE:/ 'Main Program'.

INCLUDE zinclude_write.

Output

Subroutines are procedures that we define in an ABAP program and can be called from
any program. Subroutines are normally called internally, i.e. called from the same
program in which it is defined. But it is also possible to call a subroutine from an external
program. Subroutines cannot be nested and are usually defined at the end of the program.

A subroutine can be defined using FORM and ENDFORM statements.

FORM <subroutine name>.


...

ENDFORM.

A subroutine can be called using PERFORM statement.

PERFORM <subroutine name>.

Example Program.

PERFORM sub_display.
WRITE:/ 'After Perform'.

*&---------------------------------------------------------------------*
*& Form sub_display
*&---------------------------------------------------------------------*
FORM sub_display.
WRITE:/ 'Inside Subroutine'.
ENDFORM. " sub_display

Output
Subroutines can call other subroutines and may also call themselves. Once a subroutine
has finished running, the control returns to the next statement after the PERFORM
statement.

We can terminate a subroutine by using the EXIT or CHECK statement.

EXIT statement can be used to terminate a subroutine unconditionally. The control


returns to the next statement after the PERFORM statement.

PERFORM sub_display.
WRITE:/ 'After Perform Statement'.

*&---------------------------------------------------------------------*
*& Form sub_display
*&---------------------------------------------------------------------*
FORM sub_display.
WRITE:/ 'Before Exit Statement'.
EXIT.
WRITE:/ 'After Exit Statement'. " This will not be executed
ENDFORM. " sub_display

Output

CHECK statement can be used to terminate a subroutine conditionally. If the logical


expression in the CHECK statement is untrue, the subroutine is terminated, and the
control returns to the next statement after the PERFORM statement.

DATA: flag TYPE c.


DO 2 TIMES.
PERFORM sub_display.
ENDDO.
WRITE:/ 'After Perform Statement'.

*&---------------------------------------------------------------------*
*& Form sub_display
*&---------------------------------------------------------------------*
FORM sub_display.
WRITE:/ 'Before Check Statement'.

CHECK flag NE 'X'.


WRITE:/ 'Check Passed'.
flag = 'X'.
ENDFORM. " sub_display

Output

Messages in ABAP
Messages are usually used to tell the user what is going on. The following types of
messages are available in ABAP.

A Terminatio The message appears in a dialog box, and the program terminates. When the
n user has confirmed the message, control returns to the next-highest area
menu.
E Error Depending on the program context, an error dialog appears or the
program terminates.
I Status The message appears in a dialog box. Once the user has confirmed the
message, the program continues immediately after the MESSAGE
statement.
S Error The program continues normally after the MESSAGE statement, and the
message is displayed in the status bar of the next screen.
W Warning Depending on the program context, an error dialog appears or the
program terminates.
X Exit No message is displayed, and the program terminates with a short dump.
Program terminations with a short dump normally only occur when a
runtime error occurs.

The syntax for issuing a message is as follows.


MESSAGE <message> TYPE <message type>.

We can issue a status message as follows. Status message will be displayed in the status
bar. After the message is displayed the program continues after the MESSAGE statement.

MESSAGE 'This is a status message' TYPE 'S'.

Information message will be displayed in a dialog box. Once the user has confirmed the
message, the program continues immediately after the MESSAGE statement.

MESSAGE 'This is an information message' TYPE 'I'.

Error message in report programs will be displayed in the status bar and when the user
press enter, the program terminates.

MESSAGE 'This is an error message' TYPE 'E'.

Warning message behaves similar to error message in report programs.

Exit Message – No message is displayed, and the program terminates with a short dump.
Short dumps can be viewed in t-code ST22.

MESSAGE 'This produces short dump' TYPE 'X'.


Termination Message appears in a dialog box, and the program terminates. When the
user has confirmed the message, control returns to the next-highest area menu.

MESSAGE 'This is termination message' TYPE 'A'.

Instead of hardcode the message text in the program we can maintain the message text in
text symbols. In order to maintain text symbols use the menu path Goto->Text Elements-
>Text Symbols in ABAP editor.
In the text symbols screen we can maintain the messages with a 3 character identifier.

Then use the following message statement.

MESSAGE text-001 TYPE 'I'.

Related posts:

You might also like