Sie sind auf Seite 1von 16


Modularization means minimization, if the programs contain the same

or similar blocks of statements or it Is required to process
the same
function several times, we can avoid this redundancy or duplication of
codes by using modularization techniques.

By modularizing the ABAP/4 program

1. We make them easy to read i.e. improves the readability
2. Improve their structure
3. Error handling and maintenance is easy
4. Updating can be done easily
5. Reusability i.e. procedures can be used in other programs as well
6. Encapsulation i.e. hides the internal architecture and data structure
from other classes. It is important to use a series of functions that act
as the means to access the data.
7. Reduces code redundancy i.e. avoids duplication of codes

Different modularization techniques available are:



METHODS: It describes the functions and behavior of classes and their instances in ABAP objects methods
must be defined in classes.
MACROS: Macros designed based on place holder (place holder works like pointers in c language but in case of
place holder changes effect on output.
INCLUDES: If I am defining the same variables in many programs, instead of defining in all programs we have
define all the variables in one program and that program is included or added to the programs whenever
needed thats called include program. It cannot be executed independently it has to include in a program.
A subroutine is a reusable section of code. Its like a mini program that can be called from another point in
your program. Subroutine is generally for local modularization i.e. they are generally called from the program
in which they defined.
We can use subroutine to write functions that are used repeatedly with in a program. You can define
subroutine in any ABAP programs.
Syntax for defining subroutine
FORM (subroutine name) (parameter (Optional))
-----------------------------------------------------------------------------------------------------END FORM.
Syntax for calling subroutines

PERFORM (subroutine name)(parameter)

Subroutines cannot be nested, therefore place subroutine definitions at
the end at the program. One subroutines can call other subroutines and
may also call themselves (recursive). Once a subroutine has finished
running, the calling program carries on processing after the perform
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.

Syntax for calling external subroutines:

Report ztn1811
Perform (S1) (ztn1811)
Report ztn1811

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 statics statement. It is said to be local to
The two types of parameters are:
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.
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.
Ex: Report xyz.
Data F1 value A.
Performs s1 using F1.
Write: / F1.

Form s1 using P1
P1 = X.
Write:/ P1.
End form

Here: Memory address for F1 is 1000 (Example)

F1 = A before calling s1
F1 = X after assignment in s1
P1 P1 is a pointer to memory location 1000
O/p P1= x
F1= x

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.
EX: Report xyz
Data: F1 value A.
Perform s1 using F1.
Write: / F1.

From s1 using value (P1)

P1 =X.
Write: / P1.
End form.
Here: F1 = A before call to s1
F1 = A after assignment in s1
0/P P1 = X
F1 = A

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.

Ex: Report xyz

Data: F1 value A
PERFORM: S1 changing F1
S2 changing F1
End of selection
Write: / F1.
Form s1 changing value (P1)
P1 =B.
Write:/ P1.
End form
Form S2 changing value (P2)
P2 = X.
Write: / P2.
End form
0/P P1 = B
P2 = X
F1 = B
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.

NExt notes

When you modularize source code, you place a

sequence of ABAP statements in a module. Then,
instead of placing all of the statements in your main
program, you just call the module.When the program
is generated, the source code in the modularization
unit is treated as though it were actually physically
present in the main program.
Need of Modularization
Improve the structure of the program.
Easy to read the code
Easy to maintain the code
Avoid redundancy and promotes code reuse

Various Modularization Techniques Use of Macros

Use of include files
Function Modules
Lets look into each of them in detail : SAP- ABAP Macro
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.


DEFINE <macro_name>
Macro Statements
Macros can use Parameters &N where N = 1,2,3...
Example:DATA: number1 TYPE I VALUE 1.
DEFINE increment.
ADD 1 to &1.
Increment number1.

Include Programs
Include Programs are solely for modularizing source code, and have
no parameter interface. Include programs allow you to use the same
source code in different programs. They can be useful if you have
lengthy data declarations that you want to use in different programs.
Include <include program Name>
Points to Note
Include programs cannot call themselves.
Include programs must contain complete statements.

WRITE: / 'User', SY-UNAME,/ 'Date', SY-DATUM.




Subroutines are procedures that you can define in any ABAP program and
also call from any program. Subroutines are normally called internally,
that is, they contain sections of code or algorithms that are used
frequently locally. If you want a function to be reusable throughout the
system, use a function module.
SyntaxFORM <Subroutine> [<pass>].

<Statement block>.

<Subroutine> = Name of the subroutine
<pass> = Parameters being passed
Types of Subroutines
Subroutine defined in same program being called.
Can access all the data objects declared in the main ABAP/4 program.

Subroutine defined outside the program being called.
Need to use the <pass> option or declare data objects in common parts of

Calling a Subroutine Internal Subroutines

PERFORM <subroutine> [<pass>]
<subroutine> = Name of the subroutine
<pass> = Parameters being passed
Data declared in main program is automatically
Calling a Subroutine External Subroutines
PERFORM <subroutine>(<Program>) [<pass>].
PERFORM <subroutine> (<Program>) [<pass>]
PERFORM (<subroutine>) IN PROGRAM
(<Program>) [<pass>] [IF FOUND].
PERFORM <index> OF <subroutine1>
<subroutine2> <subroutine3> [<pass>].

Function Modules
Function Modules are general purpose ABAP/4
routines that anyone can use. Infact , there are a
large number of standard function Modules
Function Modules are organized into Function
Groups: Collections of logically related functions.
A Function module always belongs to a Function
Syntax FUNCTION <function module>



Important information Associated with Function

Import/Changing/Export parameters.
Table Parameters/Exceptions.
Source code - L<fgrp>U01 . <fgrp> is the Function Group
Global Data - L<fgrp>TOP .Global data for the function groupAccessible across function modules in the function group.
Main Program - SAPL<fgrp> . Contains the list of all the
include files for that function group

Call a Function Module

To call a function module, use the CALL FUNCTION statement:


[EXPORTINGf1 = a 1.... f n = a n]

[IMPORTINGf1 = a 1.... f n = a n]

[CHANGING f1 = a 1.... f n = a n]

[TABLES f1 = a 1.... f n = a n]

[EXCEPTIONS e1 = r 1.... e n = r n [ERROR_MESSAGE = r E]

[OTHERS = ro]].

Function groups

Function groups are containers for function modules. Infact, there are a large number of standard
Function Groups. All of the function modules in a function group can access the global data of the
group.Like executable programs (type 1) and module pools (type M), function groups can contain
screens, selection screens, and lists.
Points to Note
FunThe ction Groups cannot be executed.
name of a function group can be up to 26 characters long.
When you create a function group or function module, the main program and include programs are generated
Function groups encapsulate data.
How to create a Function Group
Goto Transaction SE80.
Select Program in the DropDown.
Write the name of the Function Group That you want to create. Generally User made Function groups start with
"Z". e.g. - <Z_FUNCTION_GROUP_NAME> . Hit Enter Key.
Note that The TOP Include is create by default if the user checks the option of creating a TOP include.
How to create a Function Module
Create a function Group (say "ZCAL").
Create a function module, set the attributes like (Function group, Application, Short Text and Process Type) and
Include file "LZCALU01" will have source code of first function module.
Include file "LZCALTOP" will have global data.
Main program "SAPLZCAL" contains

Global data Include file "LZCALTOP"

Function modules include file "LZCALUXX"
User defined Include files "LZCALF..", "LZCALO.." and "LZCALI.."

Define interface parameters and Exceptions

Write the source code
Activate Function Module
Testing the Function Module - Single Test & Debugging
Documenting and Releasing a Function Module