ABAP T06-T07-001 Modularization
ABAP T06-T07-001 Modularization
Modularization
Agenda
1. Part 1 - Include
2. Part 2 - Create a Subroutine using global data
3. Part 3 - Subroutine using parameters USING, CHANGING
4. Part 4 - Subroutine using parameters TABLES (obsolete)
5. Part 5 - External Subroutine (obsolete)
6. Part 6 - Introducing to Function Module
7. Part 7 - Calling a Function Module
8. Part 8 - Creating a Function Module
What is
Modulization?
• Programmer must try to write the code
that easy to read, self-contained.
• Try to split large complex code into
smaller, simpler code.
• Do that by putting small tasks into
module.
• You can concentrate on your module,
not distract by others.
• Easy to test, easy to debug
• Real unit testing
• Easy to re-use anywhere
• After 1, 2 years people who read your
code won’t say: “stupid code”.
***Design First***
❖ Don’t start to write your code first.
❖ Start to design your program first.
❖ Simple design is enough: pseudo code, flow chart
❖ When you really feel: “Okay”, start writing your code
❖ The outline of your design should be the starting of few first modules in your
program
Include
Modularization Part 1
INCLUDE programs
• Globally available in SAP system
• Its purpose is for modularizing source code
• Simple to define
• No parameters
Using INCLUDE
Create a Subroutine
using global data
Modularization Part 2
What are procedure?
• Procedure in other programming language, e.g., COBOL, Pascal, etc.
• Similar to Function in C
• Small block of code that your main program will call
• Subroutine: mainly local procedure
• Functional Module: external procedure
• You can pass parameters
* Today you can use Class / Method to replace subroutine and Function Module
What are Subroutines?
Perform Calculate_tax
Calculate tax
Subroutine
Calculate_tax
Defining and Calling Subroutines
» Defining a Subroutine:
FORM subroutine.
...
...
ENDFORM.
» Calling a Subroutine:
PERFORM subroutine.
» Examples:
PERFORM CALCULATE_TAX.
FORM CALCULATE_TAX.
CLEAR ITAB.
MOVE: F1 TO ITAB-FL1.
APPEND ITAB.
ENDFORM.
Subroutine using parameters
USING, CHANGING
Modularization Part 3
Global Data has many Disadvantages
Advantages:
• Anyone can edit global data at anytime.
Disadvantages:
• Hard to read in a large program
• Hard to control the life cycle of data variable
• Possible of naming clashing.
Parameters Example
Current Parameters
Pass By Reference
USING Pass By Value
*- Select-options:
*-- Internal table with HEADER LINE
*-- Include 4 fields:
*-- ++ SIGN - CHAR 1 (I/E)
*-- ++ OPTION - CHAR 2 (EQ,BT,NE,GT,...)
*-- ++ LOW - same TYPE as the field after FOR
*-- ++ HIGH - same TYPE as the field after FOR
*-- Purpose: Create a screen field that support complex condition
Subroutine using Select-Options (Demo)
*- Create Range Table
*- TYPE RANGE OF ...
TYPES: TY_R_AIRLINE TYPE RANGE OF SPFLI-CARRID.
*- Range Table
*-- Internal table with HEADER LINE
*-- Include 4 fields:
*-- ++ SIGN - CHAR 1 (I/E)
*-- ++ OPTION - CHAR 2 (EQ,BT,NE,GT,...)
*-- ++ LOW - data type of TYPE RANGE OF
*-- ++ HIGH - data type of TYPE RANGE OF
ENDFORM.
ENDFORM.
Introduction to
Function Modules
ABAP Course - Modularization Part 6
Introduction to Function Modules
➢ High Reusability
STRING_CONCENATE/ READ_CALENDAR/
CURRENCY_CONVERSION/
ACCOUNT_CHECK/ ...
➢ Clear import and
➢ export parameters
➢ Search
➢ Create
Application ➢ Change
➢ Document
➢ Test ➢ Exception
Environment handling
Presentation Other
server Systems
Mainframe
RFC with
C-interface RFC RFC RFC
AP AP
R/3 R/3
RFC System System
DB DB
System
Call a Function Module
Modularization Part 7
Calling a Function Module
Function Builder
Creating a Function Module 1
ABAP Function Builder: Initial Screen
Tools
Create
ABAP Workbench
Attributes
Function Builder Import/Export Parameters
Table Parameters/Exceptions
Documentation
Source code
DEMO: Y_XXX_GET_AIRLN_NM
FUNCTION Y_XXX_GET_AIRLN_NM.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IM_AIRLINE) TYPE S_CARR_ID
*" EXPORTING
*" VALUE(EX_AIRLN_INFO) TYPE SCARR
*" EXCEPTIONS
*" NOT_FOUND
*"----------------------------------------------------------------------
ENDFUNCTION.
Global Data / Local Memory
Global Data
L<gr> TOP
FUNCTION-POOL <gr>.
DATA: X.
TABLES: ... .
ENDFUNCTION.
Add Source Code to Function Module 2
FUNCTION Y_XXX_READ_COUNTRY_NAME.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IM_COUNTRY_CODE) TYPE LAND1
*" EXPORTING
*" VALUE(EX_COUNTRY_NAME) TYPE LANDX
*" EXCEPTIONS
*" NOT_FOUND
*" COUNTRY_LIST_IS_EMPTY
*"----------------------------------------------------------------------