MODULARIZATION TECHNIQUES
Modularization is the process of breaking the program into multiple blocks.
A block is a set/collection of statements which is defined only once and can be
called / accessed any no. of times.
Advantages of Modularization:
Increases reusability.
Efficient control on the flow of program execution.
Maintenance cost is decreased.
Following are the Modularization techniques:
Subroutines
Include programs
Macros(HR-ABAP)
Function modules
Methods(OOPS ABAP)
Subroutines:
It is a block of statements, which is defined only once and can be called any
number of times.
Subroutine can be created either with parameters or without parameters.
A subroutine is associated with 2 sections.
Defining the subroutine
Calling the subroutine
Subroutine as to be defined at the end of the program.
Whenever a subroutine is called the control jumps to subroutine definition.
There are two types of subroutines.
Internal subroutine
External subroutine
Internal subroutine:
It is defined as well as called from the same program.
Syntax for defining internal/external subroutine:
Form <subroutine> [ parameters list]
statements.
Endform.
Syntax for calling internal subroutine:
perform <subroutine> [parameters list]
External subroutine:
They are defined in external programs and called from other programs.
The external program can be another executable program or a subroutine pool.
Subroutine pool:
It is a repository object which is a collection of one or more external subroutines
Syntax for calling external subroutines:
Syntax 1
Perform <subroutine> in program <external program> [parameters list].
Syntax 2
Perform <subroutine> (<external program>) [parameters list].
Note: External program can be either another executable program or subroutine
pool.
Subroutines with parameters (for data exchange and manipulation)
We can define subroutines with parameters.
For this we use the following keywords.
1. Using
2. Changing
3. Tables
The parameters specified as part of subroutine definition are called as formal
parameters and the parameters specified at the time of calling the subroutine are
called as actual parameters.
Using:
Using is the keyword used as part of subroutine definition and subroutine calling
to deal with standard data types as parameters.
The formal parameters and actual parameters names can be same or different.
The number of formal parameters depends upon the number of actual
parameters.
Subroutines returning values:
For this we can use the keywords changing or tables.
The number of return parameters depends upon number of changing and tables
parameters.
The changing and tables parameters can be used for both passing the values as
well as returning the values.
We can pass parameters to subroutines in two ways.
1. Pass by reference (default).
2. Pass by value.
Pass by reference:
In this, the address of actual parameters are copied to formal parameters. So, any
changes made to the formal parameters variables will automatically reflect the
content of the actual parameters.
Pass by value:
In this, the values of actual parameters are copied to formal parameters. So, any
changes made to the data of formal parameters will not reflect the content of the
actual parameters. For this, we need to use the keyword ‘value’ as part of formal
parameters.
Internal tables as parameters to subroutines:
We need to use tables keyword as part of subroutine call and subroutine
definition to deal with internal tables as parameters to subroutines.
In this case, if we perform the operations like delete and modify on formal
internal table with where clause, we must specify the structure of the formal
internal table and it is done by using the keyword structure as part of subroutine
definition.
In this case, any changes made to the data of the formal internal table, will
automatically reflect actual internal table data.
Internal tables are always passed by reference.
INCLUDE PROGRAMS:
It is a reusable repository object, which can be used for declaring global
variables, subroutines, module definitions and class definitions.
Include programs cannot be executed directly; it needs to be included in other
repository objects.
To use the components of include program inside other repository objects, we
must include the include program by using ‘include’ keyword.
Syntax:
Include <include name>.
It is recommended to include the include program at the beginning of the
repository object.
If the include program contains any block definitions (subroutine or module
definitions or class definitions) and if the include program is included at the
beginning of the executable program, it leads to syntax error “statement is not
accessible”. To avoid this, explicitly we need to handle the event “start-of-
selection” after include statement to indicate the starting point of program
execution.
Start-of-selection event should be handled after the include statement.
Start-of-selection event indicates starting point of program execution.
Function modules:
It is a set of statements which are defined only once and can be called any no. of
times. We can create custom function modules by using function builder tool
(se37).
There are many standard function modules provided by SAP itself. Each Function
Module is designed to perform a task. These function modules can be
called/accessed in different programs by passing values to the appropriate
parameters.
Function modules (custom/standard) are stored inside function group.
Function group is a container of function modules.
Table for function module-> TFDIR (accessed from SE11)
Table for repository objects (executable, include, subroutine pool…) - TADIR
Table for SAP Tables DD02L
Function group:
Before creating custom function module, we need to create or consider a function
group which already exists.
Function group can be created either by se80 (object navigator) / se37.
In SE37, choose the menu ‘GOTO’ function groups create group
Whenever a function group is created, SAP creates two include programs.
A. Include ending with ‘TOP’:
This is called as top include program, which can be used for declaring global
variables, subroutine, module definitions and class definitions.
The components of the ‘top’ include program can be accessed by all the function
modules of that particular function group.
B. Include ending with ‘UXX’
It is reserved for SAP internal use. As part of this include, SAP generates sub
includes (i.e), whenever a new function module is created, a sub include will be
generated on behalf of that function module.
The no of sub includes depends upon the no. of function modules of that function
group.
Syntax for defining function module:
Function <function name>.
Statements.
Endfunction.
Syntax for calling normal function module:
Call function <function name> <parameter list>.
Before calling any function module, we must check the signature of the function
module to understand the parameter types, return values, default values, pass by
value or pass by reference, optional and mandatory parameters.
As part of function modules we have 4 types of parameters:
1. Import
2. Export
3. Tables
4. Changing
Import parameters:
Are the parameters which are received by the function module.
Export parameters:
Are the parameters which are returned by the function module.
Export parameters are always optional.
Changing parameters:
Acts as both import and export, we go for changing parameters when we need
to use same variable for passing the value as well as receiving the return value.
Tables:
Are used for dealing with internal tables as parameters to function modules ,
these internal tables must refer to dictionary structures created in se11.