The document discusses different modularization techniques in ABAP including macros, include programs, subroutines, function groups, and function modules. It provides details on how each technique works, how to define and call them, and how to pass parameters. Modularization makes ABAP programs easier to maintain, reuse code, and avoid redundancy.
2. • modularization makes ABAP
programs easier to read and
maintain, as well as avoiding
redundancy, increasing the
reusability of source code, and
encapsulating data.
4. • 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, and it can only be called
in lines of the program following its definition.
Macros can be useful for long calculations or
complex WRITE statements.
5. • Syntax
DEFINE <macro_name> 'Macro Statements
END-OF-DEFINITION.
Macros can use Parameters &N where N =
1,2,3...
6. • DATA: number1 TYPE I VALUE 1.
• DEFINE increment.
• ADD 1 to &1.
• WRITE &1.
• END-OF-DEFINITION.
• Increment number1.
• WRITE number1.
• Output: 2
7. Include Programs
If you want to use the same sequence of statements in several
programs, you can code them once in an include program.
How to create include programs ?
If you create an include program yourself, you must assign it the
type I in its program attributes. You can also create or change an
include program by double-clicking on the name of the program
after the INCLUDE statement in your ABAP program. If the program
exists, the ABAP Workbench navigates to it. If it does not exist, the
system creates it for you.
How to use Include programs ?
An include program cannot run independently, but must be built
into other programs. Include programs can contain other includes.
8. Example:
Include Program
***INCLUDE STARTTXT.
WRITE: / 'Program started by', SY-UNAME,
/ 'on host', SY-HOST,
'date: ' , SY-DATUM,
'time: ' , SY-UZEIT.
ULINE.
Main Program
PROGRAM SAPMZTST.
INCLUDE STARTTXT.
This could produce the following output:
Program started by SENTHIVEL
on host ds0025 date: 09/07/2004 time: 03:15:40
9. Subroutines
• Subroutines are principally for local
modularization, that is, they are generally
called from the program in which they are
defined. You can use subroutines to write
functions that are used repeatedly within a
program. You can define subroutines in any
ABAP program.
• Subroutines: can be used locally and globally
(external subroutine calls).
10. • . External Subroutines are subroutines that are
called from another program.
• There are two types of subroutines:
•
• 1) Internal subroutine: If the source code or body
of the subroutine will be in the same ABAP/4
program i.e. in the calling program which is called
internal subroutine.
•
• 2) External subroutine: If the source code or body
of the subroutine present other than the calling
program which is called external subroutine. An
external subroutine is one that resides in a
different program that the perform statement
that calls it.
12. • Parameters
• Parameters can be either local or reference to global
variables. The memory for local parameters is allocated
when the subroutine is called & freed when it ends. If
we define variables on the form statement, the
perform statement must pass a value to each of these
variables.
•
• Global variables: A global variable is one that is defined
outside of a subroutine by using the tables or data
statement. It can be accessed from any point in the
program be it inside an event or inside a subroutine.
•
• Local variables: A local variables is a variable that is
defined inside a subroutine using local data or static’s
statement. It is said to be local to subroutine.
13. • Formal parameters: Parameter names that
appear on the form statements are called
formal parameters.
• Ex: FORM s1, using P1 changing P2, P3
• Here P1, P2, P3 are called formal parameters.
• Actual parameters: Parameter names that
appears on the perform statement are called
actual parameters.
• Ex: PERFORM S1 using P1 changing P2, P3.
• Here P1, P2, P3 are called actual parameters.
14. • There are three ways of passing parameters to a subroutine.
• 1) Pass by reference
• 2) Pass by value
• 3) Pass by value & result
• 1) Passing parameters by reference
• During subroutine call, only the address of the actual
parameter is transferred to the formal parameters i.e. pointer
to the original memory or address location is passed. The
formal parameter has no memory of its own & we work with
the fields of the calling program with in a subroutine. So
changes to the variable within the subroutine update the
original memory location immediately i.e. the field contents in
the calling program also changes.
15. • 2. Passing parameters by value :
•
• When you pass a parameters by value, new
memory is allocated for the value i.e. the
formal parameters are created as copies of the
actual parameters, thus formal parameters
have memory of their own i.e. the memory
allocated when the subroutine is called and
freed when the subroutine returns. Therefore
changes to the formal parameters have no
effect on the actual parameters.
16. • 3. Passing parameters by value & result:
•
• Pass by value & result is similar to pass by
reference like Pass by value, a new memory
area is allocated & it frees when the
subroutine ends. When the end form
statement executes, it copies the value of the
local memory area back into the original
memory area changes to the parameter with
in the subroutine are reflected in the original
but not until subroutine returns.
17. • Leaving subroutine
• You can exit out of a subroutine at any time using the
following statements.
• 1) Stop: Immediately leaves the subroutine & goes
directly to the end of selection event.
• 2) Exit: It leaves the loop, subroutine or comes out of the
program & display the result without any condition.
• 3. Check: It also comes or immediately leaves the
subroutine but it depends on logical expression. If it is
false comes out of the loop or subroutine.
18. Function group and Function module
• Function groups are created using transaction
SE37 (function builder). A
• Function Group is realized in the SAP Software
system as a program, containing
• a logically related set of function modules.
• Create the function group by selecting the
menu path Goto -> Function Groups
• -> Create Group.
19. Function Module
• Function modules are created using transaction SE37
(function builder). The standard SAP system has a number
of predefined function modules. Function Modules are
Global ABAP programs created by SAP for reusable
purpose.
• Function modules are best used for carrying out database
updates and communicating with different SAP systems.
•
• In contrast to subroutines, function modules do not need to
be defined in the source of your ABAP program. Function
modules may have both input and output parameters. The
input parameters can be mandatory or optional.
20. • You can also assign default values to
parameters. Function modules also provide
the option of exception handling, which
catches errors when the function
• module executes. You may also test function
modules from transaction SE37 before
including them in your programs.
• Function Modules must begin with a Z_ or a
Y_, and can be created as follows:
21. • The Attributes of the function module should be
reviewed first, as it controls the behavior of the overall
function module.
• The processing type controls the behavior of the
function module.
• Normal Function Module
Used for modularizing code and is not externally visible
to other systems, but globally visible within the same
SAP software system.
Remote Function Module
Visible to external systems, and thus can be called via
the RFC gateway.
22. • Update Function Module
• Denotes the use of the function module for
Asynchronous update purposes.
23. Interface of function module
Import
• Data to be received from the calling program
export
• Data to be returned to the calling program
• Changing
• Data to be received and returned using the
same reference variable
24. • Tables
• Obsolete interface for passing arrays of
information
• Exceptions
• Exceptions , resulting in non zero values of the
System variable SY-SUBRC
• Exceptions are raised using the RAISING
<exception> command in the source code of
the function module.
25. • Note the use of the RAISE statement in the
code above to trigger an exception.
• When the RAISE statement occurs, the
program processing jumps to the
ENDFUNCTION, and the control is passed back
to the calling application.
26. • Testing the Function Module
• The Function Builder has its own test
workbench, and can be accessed using the
• TEST icon, the same one used in the ABAP
Editor.