0% found this document useful (0 votes)
34 views40 pages

Modularization Techniques

The document discusses different techniques for modularizing code in ABAP, including macros, includes, subroutines, and function modules. It describes what each technique is used for and how it can help make code more reusable, readable, and maintainable by breaking programs into smaller logical units.

Uploaded by

chandra bgr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views40 pages

Modularization Techniques

The document discusses different techniques for modularizing code in ABAP, including macros, includes, subroutines, and function modules. It describes what each technique is used for and how it can help make code more reusable, readable, and maintainable by breaking programs into smaller logical units.

Uploaded by

chandra bgr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Modularization Techniques

What is Modularization?
 Modularization is breaking down the application code into smaller units, so
that it is easy to maintain and avoid redundancy of code.

 Suppose if we want to implement the same logic in several places of the


same program, then write the logic inside a modularization unit and call
the modularization unit where ever we want.

 Even if we want to declare the same variables in multiple programs or use


the same logic in different programs then code the common part in the
modularization unit and use it in different programs.

 Advantages of Modularization are as follows:


 Easier to read, maintain and debug.
 Eliminates redundancy of code and increases reusability of code .
Types of Modularization units
 Macros
 Include Programs
 Subroutines
 Function Modules
 Methods - Will be explained in OOABAP
Macros
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.
Sample Macro : Output :
Includes
Includes
 Include programs are not standalone programs and
cannot be executed independently.
 They can be used as a container for ABAP source
code.
 Include programs can be inserted at any place in
other ABAP programs using the INCLUDE statement.
 The include statement copies the contents of the
include program into the main program.
 Include programs have no parameter interface and
they cannot call themselves.
While creating the INCLUDE program using the ABAP editor choose
program type as "I" i.e. INCLUDE program in the program
attributes.
Subroutines
Subroutines
 If your program contains several identical or similar statements, you
can avoid redundancy (writing the exact same series of statements
over and over again in one program), through the use of subroutines.

 Subroutines improve the structure of your program (by means of


modularization) and make it easier to read.

 Subroutines can be called from various points in a program.

 Programs are much easier to read or follow when you place all
subroutines together after the main program, in the order they are
most likely to be called.
What Are Subroutines ?
REPORT YEXAMPLE1. REPORT YEXAMPLE2.

Calculate tax Call calculated_tax

Calculate tax Call calculated_tax

Subroutine
Calculate_tax
Creating Subroutines
 Use the keyword PERFORM to call a subroutine. Use the
keywords FORM and ENDFORM to define a subroutine. A
maximum of 30 character is allowed to name a subroutine.
The name must appear after the PERFORM and after the
corresponding FORM keyword.

 Syntax :
PERFROM SUBR.
FORM SUBR.

ENDFORM.
Methods of Passing Parameters
Pass by Value a1

uti ne
Subro
Pass by Value and Result
f1
a1
Subro
utine

Pass by Reference a1 f1

u tine
Subro
f1
Methods of Passing Parameters
When using parameters, they must be passed to the subroutine using one of the following
three methods.
 Pass by value: When the subroutine is called, a copy of the value of the data variable
is passed to the formal parameter. The formal parameter has its own storage location
(memory).
 Pass by value and result: In addition to the above, at the end of the subroutine, the
value of the formal parameter is passed back to the actual parameter assigned to it.
 Pass by reference: The formal parameters do not have a separate storage location.
Instead, the address of the actual parameter is passed to the formal parameter.
Changes to the value of the formal parameter are immediately reflected back in the
actual parameters.

 The additional keyword USING allows you to specify the data variables to be
passed as parameters. [Example: FORM subr USING f1 f2 …]

 Each formal parameter must have a corresponding actual parameter.


Declaring and Calling a Subroutine - Pass by
Value(1) & Reference(2)
REPORT Y170D091.
When specifying formal DATA: .…
.
parameters: .
 You have the option of .
PERFORM <name> USING
using the VALUE clause to <a1> <a2>
define a pass by value, <a3> <a4>.
[Example: FORM subr .
.
USING (f1) VALUE(F2 .
FORM <name> USING Pass by value
 You can also not use the VALUE(<f1>)
VALUE clause, without it, VALUE(<f2>) Pass by reference
the system will <f3>
<statements>
<f4>.
automatically define a
pass by reference.
ENDFORM.
Declaring and Calling a Subroutine - Pass by
Value and Result(3)
REPORT YSDEM000.
DATA: ... .
.
.
PERFORM <name> USING
<a1> <a2>
CHANGING<a3>.
.
.
FORM <name> USING
VALUE(<f1>) Pass by value
VALUE(<f2>)
Pass by value and result
CHANGING VALUE(<f3>).
.
.
f 3 =“A”.
.
.
ENDFORM.
Declaring and Calling a Subroutine - Pass by
Value and Result(3)
 You can use the CHANGING clause when specifying parameters to define a
pass by value and result. When the subroutine is called, the formal parameter
is assigned its own storage location (memory). At the end of the subroutine (at
ENDFORM), the contents of the formal parameters are automatically passed
back to the actual parameters.

 If the subroutine is terminated by a dialogue message (MESSAGE Ennn) the


value of the formal parameter is not passed to the actual parameter.

 If the result of the CHECK <log.expression> statement is negative in a


subroutine, the value is still returned since processing returns to the main
program via ENDFORM. The same applies to the STOP and EXIT statements.

 See Extended Help on the PERFORM and FORM statements for more help on
the CHANGING addition.
Global versus Local Data
report y170dm86.

data: num(9) value ‘999999999’.

data: id type id, Global Data


name type name1.
. . . <statements>
perform sub1.
. . . <statements>

* ------------------------------------------------------------------------------------------------*
* FORM SUB1 *
* ------------------------------------------------------------------------------------------------*
form sub1.
data: begin of tab , “ <----------- local data
id type id,
name1 type name1, Local Data
end of tab.
local: num . “ <----------- local data
. . . <statements>
endform.
Global versus Local Data
 Global data is any variable or structure that is defined in the main program.

 Local data is any variable or structure that is defined within a subroutine.

 Global data created by statements like DATA and PARAMETERS is recognized


within subroutines. The following would result if you tried to use these words
to define local data:
 DATA: this is one of the approved techniques for defining a local variable,
see the next slide for further information on the impact on the main
program.
 PARAMETER: you cannot use this keyword within a subroutine.

 If a variable is only necessary during a subroutine, then declaring it locally


means you can release the memory storage area when it is no longer necessary.
Local Data in Subroutines
Statement Value Upon Value Upon Value Upon
Between Entering Returning to Main Entering
Subroutine First Program Subroutine Next
FORM…
Time Time
ENDFORM
DATA Whatever the local If declared globally, Re-initializes based
DATA statement returns to old value, on local DATA
otherwise not statement
Initializes
recognized

STATICS Whatever the local If declared globally, Last set value from
STATICS statement returns to old value, inside subroutine
initializes otherwise not
recognized

LOCAL Last set value from Last set value from Last set value from
main program main program main program
Local Data in Subroutines
 DATA: use this option if you want the variable to be re-initialized with the default
value every time it enters the subroutine.
 STATICS: use this option if you want the variable to remember what the value was
the last time it was in the subroutine. It will not be re-initialized based on the
definition.
 LOCAL : It cannot be self-contained within the subroutine. It has to have been
declared globally with a DATA statement first. Since that is the original declaration,
you cannot add length and types and defaults to the LOCAL statement. The idea is
that you can manipulate the variable inside the subroutine, and not affect the
value in the main program. This is like ‘Passing by Value’.

Note :
If you happen to have declared a global variable in the main program with the same
name, the variables are not linked. They behave as if they are two different variables. Returning
to the main program means reverting to the value that the global variable had before it entered
the subroutine.
It is not recommended that you use LOCAL.
Passing Structures as Parameters
REPORT Y170D095.
DATA: IT_TAB TYPE STANDARD TABLE OFThis is passing the
TABNA.
DATA: WA_TAB TYPE TABNA. TABNA work area only
DATA: FLAG.
.
.
PERFORM SUB1 USING WA_TABNA. This is passing the ZTAB
PERFORM SUB1 USING IT_TAB[]. structure
.
.
.

FORM SUB1 USING


WA_TAB TYPE TABNA.
WRITE: / WA_TAB-COUNTRY,
WA_TAB-NAME1.

ENDFORM.
Coding Recommendations
 For good modularization, code that decides whether to execute
a subroutine should be made before the subroutine is called, not
inside the subroutine.

 Coding standards/methodologies emphasize that modularity


both simplifies maintenance and makes the code easier to read.

 Wherever possible, divide the main program into subroutines by


identifying stand-alone processes such as data retrieval, sorting
internal tables, calculating totals, printing report headers. This
makes it easier to implement changes, reduces rework,
and assists debugging.
Summary
 Recall that global data is visible and accessible in subroutines. Using the PERFORM
and FORM statements with no special extensions implies a pass by reference.
 If you would rather not modify the values of the global variables, you can choose
to pass by value. We saw how to do this with a simple variable by using the USING
extension .
 To pass a field string (which you can consider a series of separate variables) by
value, you will use the USING extension again.
 The data structures (such as table work areas, header lines, field strings) are
passed to subroutines as one long string of characters. Within the subroutine, you
can reference the individual fields of the structure. At runtime, however, the
structure description itself is unknown to the System and must be explicitly stated.
 In summary, you must have:
 PERFORM <subroutine>USING <field string>.
 FORM <subroutine> USING <field string formal parameter>
 STRUCTURE <which design the field string should have>.
Function Groups & Function
Modules
Creating a Function Group
 Function groups are collections of logically related functions modules. Specification of a
function group is required when creating a function module.

 Function group names can be up to 26 characters and must begin with a Y or Z. Short text
is a required field.

Open SE37 transaction. Give the function group name and short
Goto -> Function groups -> Create group. text. And click on Save.
Function Modules

Function Builder

FM Group : FIBU
FM_01 ...
Program 1 FM_02 ... Program 2
FM Group : XYZ
CALL FUNCTION CALL FUNCTION
FM_03 ...
‘FM_02’ ‘FM_02’
FM_04 ... ...
...
Program 3

CALL FUNCTION
‘FM_02’
...
Function Modules
 Function modules are general purpose ABAP routines that can be called
from any ABAP program. The benefit of function modules lies in their
reusability, which saves developers from creating redundant code and
increases programming efficiency.

 Function modules are stored centrally in the Function Builder as part of


function groups. These function groups are collections of logically related
functions that share common program context, such as global variables, at
run time. Each function group contains all the functions that perform
similar tasks.

 A group can be assigned to a specific application (FI, HR) or can be


designated for general use (*).
Creating Function Modules
 Function modules can be created from the Repository Browser (SE80) or
from the ABAP Function Builder: Initial Screen(SE37).
 Function module names can be up to 30 characters long and must begin
with either a Y or Z (i.e., Y_ and Z_).
 Only alphanumeric characters and the special character underscore (_) are
allowed.
 When creating a function module, the Administration tab is defaulted to
appear first. Both Function group and short text are required fields in the
Administration screen.
 A function module belongs to a function group.
 All function modules in a function group are managed in one ABAP
program.
Creating Function Modules
Go to SE37 transaction, enter the function
Give a function group name, short text
module name and click create
and click Save.
Import/Export Parameter Interface
 Formal parameters (import, export, etc.) should not be named
the same as R/3 repository objects.
 Import parameters are those variables that will be
imported from the calling program.
 Export parameters are those variables that will be
exported to the calling program.
 Changing parameters are those variables that will be both
imported from and exported to the calling program.

 By default, import/export parameters are passed by value.


Changing parameters are passed by value and result.
Import/Export Parameter Interface
‘TYPE’ Default value

Pass parameter
Parameter name by reference

‘ABAP REFERENCE TYPE’ Flag parameter


as optional
Import/Export Parameter Interface
 Parameter options:
 Type specification - Specify a reference field or structure
from the ABAP dictionary–the type and length of the
actual parameter is checked
 Associated Type - Specify reference type from ABAP
repository
 Default Value - Specify default value for import/changing
parameter
 Optional - Flag import/changing parameter as optional
 Pass value. - Flag parameter as being passed by reference
rather than by value
 Specifying reference fields and structures improves run time of
function modules.
Table Parameters/Exceptions Interface & Documentation

‘TYPE REF TO’

Flag parameter
as optional

Parameter name

‘TYPE’
Table Parameters/Exceptions Interface &
Documentation
 Internal tables can be both exported to and imported from
the calling program using the tables parameter. Internal
tables are passed by reference using this parameter.

 Table parameter options:


 Type specification - Specify a reference structure from
the ABAP dictionary
 Reference Type - Specify reference type from ABAP type
pool which must be declared in the function group
 Optional - Flag table parameter as optional
Table Parameters/Exceptions Interface &
Documentation
 Exception parameters are used to pass return codes
back to the calling program for error handling.
 Exceptions are raised in the function module code
when an error has been detected programmatically.
 Exceptions set SY-SUBRC in the calling program,
enabling programmers to handle multiple errors.
 On this screen, for Exceptions, enter simple short
verbal descriptions for things that can go wrong, or
other situations you will later code to detect.
Function Module Source Code
Calling a Function Module
End Of Slides

You might also like