Modularization Techniques
Modularization Techniques
Modularization Techniques
Types of Modularization
techniques in SAP ABAP programming
The concept of modularization is dividing the main program into sub-programs for better readability
and re-usability. Modularization can be implemented in the following ways.
Include Programs.
Sub-routines.
Function Modules.
Include Programs :
Sub-routines are also sub-programs in SAP ABAP which contains certain re-usable
statements.
Most of the times we use sub-routines for re-usability inside the program.
We may use sub-routines for external re-usability.
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.
Best practice is to use subroutine at end of program.
1. Internal Subroutines
2. External Subroutines
In case of Internal; Form and Perform can exist in a same program, In case of External; Form and
Perform can exists in Different programs.
Possibility 1:
Define (form .. endform) and Call (perform) in a Executable program (ZSUBEXE). Then create another
Executable program (ZSUBEXE1) and call perform subrout(zsubexe).
*&---------------------------------------------------------------------*
*& Form sub_display
*&---------------------------------------------------------------------*
FORM sub_display.
WRITE:/ 'Welcome to Inside Subroutine World'.
ENDFORM. " sub_display
Possibility 2:
Create a Subroutine pool Program and call it in Executable one. (Subroutine pool is collection of
Subroutines, Not possible to Execute directly)
PERFORM sub_display.
*&---------------------------------------------------------------------*
*& Form sub_display
*&---------------------------------------------------------------------*
FORM sub_display.
WRITE:/ 'Before Exit Statement'.
EXIT.
WRITE:/ 'After Exit Statement'. " This will not be executed
ENDFORM. " sub_display
While Defining Subroutine ( FORM ENDFORM ) if parameter passed those are called Formal
Parameters.
While Calling Subroutine ( PERFORM ) if parameter passed those are called Actual Parameters.
DATA: x type i value 6000,
y type i value 1000,
z type i.
"Defining Subroutine
Form SUB USING value(X) value(Y) value(Z). “pass by value/call by value
x = 4000.
y = 5000.
z = x + y.
write:/ Z.
endform.
START-OF-SELECTION.
" Calling Subroutine
PERFORM SUB using x y z.
z = x + Y.
write:/ z.
O/P is : 9000 and 7000 because in call by value Initial value can be consider when come out
of Subroutine.
DATA: x type i,
y type i,
z type i.
Form SUB USING x y z. “ pass by Reference/ call by reference
x = 4000.
y = 5000.
z = x + y.
write:/ Z.
endform.
START-OF-SELECTION.
PERFORM SUB using x y z.
z = x + Y.
write:/ z.
O/P is : 9000 and 9000 because in call by Reference Changed value can be consider at
outside of Subroutine.
In pass by value once the process comes out of the routine (form…endform) it take the Initial value
whatever define in data statement as VALUE.
In pass by reference value was not changed once the process comes out of the routine (form…
endform).
Syntax :
**DEFINING SUBROUTINE
FORM abc USING p1_a type any
p2_a type any
CHANGING p3_a type any.
ENDFORM.
**CALLING SUBROUTINE
PERFORM abc USING p1 p2 CHANGING p3 .
**IN THE ABOVE SYNTAX p1 p2 p3 ARE ACTUAL PARAMETERS AND p1_a p2_a p3_a ARE FORMAL
PARAMETERS
REPORT ZSUBROUTINE_PARAM1.
DATA: num1 TYPE i,
num2 TYPE i,
sum TYPE i.
num1 = 2.
num2 = 4.
PERFORM addit USING num1 num2 CHANGING sum. " Actual parameters
num1 = 7.
num2 = 11.
PERFORM addit USING num1 num2 CHANGING sum. " Actual parameters
FORM addit USING add_num1 TYPE I " Formal parameters
add_num2 TYPE i
CHANGING add_sum TYPE i.
add_sum = add_num1 + add_num2.
PERFORM out USING add_num1 add_num2 add_sum.
ENDFORM.
FORM out USING out_num1 TYPE i
out_num2 TYPE i
out_sum TYPE i.
WRITE: / 'Sum of', out_num1, 'and', out_num2, 'is', out_sum.
ENDFORM.
Function Modules
These are also sub-programs which contains set of reusable statements for better readability
and re-usability .
Function modules contains parameter interface, that is importing and exporting parameters.
These Function Modules can be executed independently.
Function modules contains exceptions to catch certain type of errors.
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.
Function Modules are 3 types:
1. Normal FM
2. RFC enabled FM
3. Update FM
Change parameter can be used for one record(work area) as Tables can only be used for multiple
well as multiple records(internal tables). records(internal tables).
Can be used for input and output. Can be used for input and output.
When we PASS lv_var by VALUE , the actual value of lv_var is copied into VAR.
When we PASS lv_var by REFERENCE , the reference or the memory address of
lv_var is passed to the Function module. So VAR and lv_var will refer to the same
memory address and have the same value.
Example: In Import screen if we take i_vbeln type vbrk-vbeln and provide the Default
value as 90005183 and click F2 for check, system throw an error “Default entry is not type-
compatible with parameter i_vbeln” . To overcome this error we need to Tick the Check box
‘PASS VALUE’. Then it will take the value from VBRK.
Pass value is used to provide default value to the Input. Example function module: Zsample
To know better we see below example
Program name: ZTEST_FM_ITAB_COPY in this program used Function Modules are:
ZTEST_NP_PASS_BY_VALUE ; ZTEST_NP_PASS_BY_REFERENCE