Modularization Techniques

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

What is a Modularization in SAP ABAP Programming?

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 :

 These are sub-programs which contains a set of re-usable statements.


 These programs cannot be executed directly.
 These include programs must be embedded inside a main program for execution.
 These programs doesn’t contain parameters or select-options.
 Include programs allow you to use the same source code in different programs.

Syntax : INCLUDE <program name>.

Example of using include programs :

TYPES : BEGIN OF TY_MARA,


MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MEINS TYPE MARA-MEINS,
MBRSH TYPE MARA-MBRSH,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.
SELECT MATNR, MTART, MEINS, MBRSH FROM MARA INTO TABLE IT_MATA.
LOOP AT IT_MARA INTO WA_MARA .
WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MTART.
ENDLOOP.

To make the above program with includes, follow below steps.

INCLUDE ZDATA_DECLERATIONS. "DOUBLE CLICK ON ZDATA_DECLERATIONS AND CREATE A INCLUDE


AND ADD DATA DECLERATIONS AND ACTIVATE
INCLUDE ZMAIN_LOGIC. " DOUBLE CLICK ON ZMAIN_LOGIC , CREATE A INCLUDE AND ADD THE
REMAINING LOGIC (FROM SELECT....TO ....ENDLOOP).

Sub-routines in SAP ABAP.

 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.

SUBROUTINES are 2 types:

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.

Examples for Sub routine:

Possibility 1:

Define (form .. endform) and Call (perform) in a Executable program (ZSUBEXE). Then create another
Executable program (ZSUBEXE1) and call perform subrout(zsubexe).

and create one another Executable program (ZSUBEXE2) to reuse perform subrout(zsubexe).

PERFORM sub_display. “ Double click on it.

WRITE:/ 'After Perform'.

*&---------------------------------------------------------------------*
*& 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)

Subroutine pool Program name: ZSUBROUTINE Executable Program name: ZSUBROUTINE1.

EXIT statement in SUBROUTINES:

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

Passing Parameters in Subroutines:

To pass parameters SAP key words are USING and CHANGING

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.

Program Name: ZSUBROUTINE_PARAM (Z*_PARAM1, Z*_PARAM2, ZSUBROUTINE_PARAM3)

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.

USING is a key word to pass parameters


Value is a syntax for call by value
Three Formal Parameters are X Y Z

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.

Difference between Call by value and Call by reference:

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.

Programs : ZSUBROUTINE_PARAM2, ZSUBROUTINE_PARAM3

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

Function Modules V/S Sub-routines


The main difference between Function Modules and sub-routines is sub-routines dosen`t contain
any exceptions and sub-routines are mainly used for local modularization where as function modules
contains exceptions and used for global modularization.

Examples for Function Modules are : GUI_DOWNLOAD, GUI_UPLOAD.


T-code for Function Module explorer is SE37 .
Function Group in SAP ABAP
Function group is a container of Function modules, every Function Module must be saved in a
Function group. T-codes for Function Modules and Function groups are SE37 OR SE80. We can
save N number of Function Modules in a Function Group.

Import: These are input parameters of a Function Module.


Export: These are output parameters of a Function Module.
Changing: These are parameters which acts as both importing and exporting parameters to a
Function Module .
Tables: These are internal tables which also acts as importing and exporting parameters.(out dated)
Exceptions: Exceptions in Function Modules are used to catch certain type of errors.

Changing Parameters Table Parameters

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.

Function Module name : ZFUNCTION_MODULE_TEST ; ZFM_TABLES ; ZSAMPLE

What is the difference between PASS BY VALUE and PASS BY REFERENCE?

CALL FUNCTION 'DEMO_FM'


EXPORTING
VAR = lv_var.

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

VIEW_MAINTENANCE_CALL is a Function Module which is act like a Table maintenance generator.


If we don’t have Access for SE11 or SM30 then we use this Function module.

You might also like