04 Modular Ization
04 Modular Ization
Presentation Overview
Objectives
Learn different ways of modularizing code in ABAP programs.
Learn creation and usage of Subroutines.
Learn creation and usage of function modules.
Pre-Requisites
Knowledge of ABAP data dictionary
Knowledge of internal tables.
Knowledge of Simple ABAP program.
Contents
Modularization – Need and Techniques
Macros
Include Programs.
Subroutines.
Function Modules.
Function Groups
Modularization
Need of Modularization
Improve the structure of the program.
Easy to read the code
Easy to maintain the code
Avoid redundancy
Modularization Techniques
Use of Macros
Use of includes
Subroutines
Function Modules
Methods ( OOPS)
What is Modularization?
PERFORM COLLECT_DATA
for personal information
PERFORM COLLECT_DATA
for general information
In the next unit we will see how these modules can be implemented in ABAP
System Demo
Now the Modularization types are selected for all the modules.
Call Module 1 to write
Selection screen Module 1 – Standard Header
a standard report
header
Define a section of source code that you can address using the name macro.
Parameters
Can call Macro from another macro but a macro can’t call itself.
Example of a DEFINITION.
Example.
Output: 2 2
Include Programs
These are TYPE ‘I’ Programs created using the transaction
SE38.
Rules
Can’t run independently.
Must be called from another executable or include program.
Resolved before program generation
After generation, program contains the source code of all include
programs it use.
Example – For an Include File
Example.
REPORT ZTEST_PROGRAM.
INCLUDE ZILX0004.
PROGRAM ZRPM0001.
INCLUDE ZILX0004.
Subroutines
Program modules which can be called from ABAP/4 programs.
Defining subroutines
Block of code between FORM and ENDFORM
FORM <Subroutine> [<pass>].
<Statement block>.
ENDFORM.
<Subroutine> = Name of the subroutine
<pass> = Parameters being passed
Types
Internal
Subroutine defined in same program being called.
Can access all the data objects declared in the main ABAP/4 program.
External
Subroutine defined outside the program being called.
Need to use the <pass> option or declare data objects in common parts of memory.
Subroutines
Calling Subroutines
Internal Subroutines
PERFORM <subroutine> [<pass>]
<subroutine> = Name of the subroutine
<pass> = Parameters being passed
Data declared in main program is automatically available.
External Subroutines
PERFORM <subroutine>(<Program>) [<pass>].
PERFORM <subroutine> (<Program>) [<pass>] [IF FOUND].
PERFORM (<subroutine>) IN PROGRAM (<Program>) [<pass>] [IF FOUND].
PERFORM <index> OF <subroutine1> <subroutine2> <subroutine3> [<pass>].
Up to 256 subroutines are possible.
BY making use of COMMON PART with the DATA statement.
By passing parameters.
Subroutines
Passing Data by Parameters
Formal Parameters
Parameters that are used in FORM statement
Actual Parameters
Parameters that are used in PERFORM statement
Input parameters
Output parameters
Input/Output parameters
FORM <subroutine> [TABLES <formal table list>]
[USING <formal input list>]
[CHANGING <formal output list>]
PERFORM <subroutine> [(<program>)] [TABLES <actual table list>]
[USING <actual input list>]
[CHANGING <actual output list>]
Options TABLES, USING and CHANGING must be in same sequence.
Subroutines
Passing Data by Parameters
Calling by Reference
Address of the actual parameter is transferred to formal parameter.
Formal parameter has no memory of it’s own.
Change in formal parameter reflects in the calling program.
Calling by Value
Formal parameters are created as copies of the actual parameters.
Formal parameters have memory of their own.
Calling by Value and Result
Formal parameters are created as copies of the actual parameters.
Formal parameters have memory of their own.
Change in formal parameters are copied to the actual parameters at the end of subroutine.
Internal tables that are passed by tables are always called by reference.
For calling by reference, USING and CHANGING are equivalent.
Passing by Reference
Passing by Value
Passing by Value and Result.
Function Groups
Overview
General purpose ABAP/4 routines that anyone can use.
Large Number of standard function Groups.
Transaction SE80.
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
Note that The TOP Include is create by default if the user checks the option of creating a TOP
include.
Overview
Function Modules
General purpose ABAP/4 routines that anyone can use.
Large Number of standard function Modules.
Organized into Function Groups: Collections of logically related functions.
A Function Module is always associated with a Function Group.
Transaction SE37.
Called Using “Call Function <function module>” in ABAP/4 program.
Information Associated with Function Module
Administration
Import/Changing/Export parameters.
Table Parameters/Exceptions.
Documentation
Source code – L<fgrp>U01
FUNCTION <function module>
<Statements>
ENDFUNCTION.
Global Data - L<fgrp>TOP
Global data for the function group- Accessible across function modules in the function group.
Main Program - SAPL<fgrp>
Contains the list of all the include files for that function group
Function Modules
Calling subroutines
Can be written at the end of function module in source code.
Include program L<fgrp>F<xx> is used for subroutines across function modules.
Can call any number of external subroutines.
Raising Exceptions
RAISE <exception>
Message … RAISING <exception>
Prefix of the message specifies the message type eg. E for error message. It is
followed by the message number given in SE91 for that message class.
Message i001 with var1 var2. “Here Var1 & Var2 will take &1 and &2 in
Msg class
Message ‘This is Correct’ TYPE ‘I’. “For these messages no need of Msg
class.
Message i000(abc). “Here abc is the Message Class
E : error – interrupts flow of control in PAI , enables user to enter new data , PAI is
processed again, user must correct entry