0% found this document useful (0 votes)
18 views51 pages

ABAP T06-T07-001 Modularization

Uploaded by

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

ABAP T06-T07-001 Modularization

Uploaded by

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

ABAP Course

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?

PROGRAM ZDEM001. PROGRAM ZDEM002.


Perform Calculate_tax
Calculate tax

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

PERFORM CALCULATE_TAX USING FL1 FL2.

FORM CALCULATE_TAX USING F1 F2.


CLEAR ITAB.
MOVE F1 TO ITAB-FL1.
APPEND ITAB. Formal Parameters
ENDFORM.

In this example, parameters are passed by


reference. This is the most common, and most
cost effectively, method of parameter passing.
Using Parameters to Pass Data

Pass By Reference
USING Pass By Value

CHANGING Pass By Value & Result

RAISING Raise an exception (Class-base)

TABLES Pass By Reference


Passing Parameters
a1 a2 a3 a4
PROGRAM <name>. X Y
TABLES: ....
DATA: ....
.
. 1
. 1 2 2
PERFORM <name> USING
<a1> <a2>
<a3> <a4>.
.
FORM <name> USING X Y
VALUE(<f1>)
VALUE(<f2>)
f1 f2
<f3>
<f4>.
<statements> 1 Pass by value
ENDFORM.
2 Pass by reference
*-- BEST PRACTICE with PARAMETER --*
*- 1. Always using TYPE
*- 2. Use USING only for input parameters / Do not change data of USING
*- 3. Use CHANGING for output/changing parameter
*- 4. Should not use ANY TABLE if you are not doing dynamic programming
*- 5. Declare TYPE TABLE and use it instead of ANY TABLE
*- 6. DO NOT use TABLES parameter
Coding Convention (Rules of Coding)
• Global data: GD_* • USING: U_*
• Global work area: GS_* • CHANGING: C_*
• Global internal table: GT_* • TABLES: T_*
• Global field symbols: GFS_* • Selection-Screen Parameter: P_*
• TYPE: TY_* • Selection-Screen Select-Opt: S_*
• TYPE table: TY_T_* • Selection-Screen Radio Button: R_*
• Local data: LD_* • Selection-Screen Checkbox: CB_*
• Local work area: LS_*
• Local internal table: LT_*
• Local field symbols: LFS_*
Subroutine using parameters
TABLES and others
Modularization Part 4
What will we talk about?
1. Subroutine using parameters TABLES
2. Pass internal table with Header Line
3. Moving everything into Subroutine and No more global data
TABLES parameters (Demo)
• Only accept STANDARD TABLE
• Not accept SORTED TABLE / HASHED TABLE
• Use with keyword STRUCTURE <work_area> / <header_line> / <structure in SE11> / <DB table name>
• Use with keyword TYPE STANDARD TABLE / TABLE
• Use with keyword TYPE <custom define table type>
Internal Tables as Passed Parameters
REPORT B170D094
TYPES: BEGIN OF ZSTRUCT,
F1 LIKE ADRC-COUNTRY,
F2 LIKE ADRC-NAME1,
END OF ZSTRUCT,
DATA: TAB TYPE TABLE OF ZSTRUCT.
PERFORM SUB1 USING TAB.

FORM SUB1 TABLES F_TAB.


LOOP AT F_TAB INTO W_TAB.
WRITE: / W_TAB.
ENDLOOP.
MOVE . . . TO W_TAB.
APPEND W_TAB TO LT_TAB.
ENDFORM.
External Subroutine
(obsolete)
Modularization Part 5
External Subroutine (Demo)
1. You should use this for your program
2. Should use Function Module instead
External Subroutine (Demo)
REPORT YXXX_MOD_PART05_REP1. REPORT YXXX_MOD_PART05_REP2.
DATA: GD_TEST_OUT TYPE CHAR40.
DATA: V_COUNT TYPE I.
** You can use this with ABAP OOP
FORM WRITE_DATA USING U_INPUT_1 TYPE STRING PERFORM WRITE_DATA
U_INPUT_2 TYPE STRING IN PROGRAM YXXX_MOD_PART05_REP1
CHANGING C_OUTPUT_1 TYPE CHAR40. USING 'Input Text 1' 'Input Text 2'
V_COUNT += 1. CHANGING GD_TEST_OUT.
WRITE: / 'DATA', V_COUNT, ':', U_INPUT_1, U_INPUT_2.
C_OUTPUT_1 = 'TEST OUTPUT'. ** CANNOT use this with ABAP OOP
ENDFORM. PERFORM WRITE_DATA(YXXX_MOD_PART05_REP1)
USING 'Input Text 1' 'Input Text 2'
CHANGING GD_TEST_OUT.

WRITE: / 'Test Ext Changing: ', GD_TEST_OUT.


Subroutine
using Select-Options
Modularization Bonus
Subroutine using Select-Options (Demo)
1. Range Table vs Select-Options
2. Pass SELECT-OPTIONS into subroutine.
Subroutine using Select-Options (Demo)
*- Create Select-options
*- 1. Use TABLES (obsolete)
*- 2. User dummy variable
DATA: GD_DUMMY_AIRLINE TYPE SPFLI-CARRID.
SELECT-OPTIONS: S_AIRLIN FOR GD_DUMMY_AIRLINE.

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

*-- Purpose: 1. Use feature of SELECT-OPTION without create screen field


*-- 2. Use for subroutine Parameter
Subroutine using Select-Options (Demo)
*- How to pass data into Subroutine
*- 1. Pass data using ANY TABLE / STANDARD TABLE
*- 2. Pass data using RANGE table type
FORM TEST_SUB USING U_R_AIRLIN TYPE ANY TABLE.

SELECT * FROM SPFLI


INTO TABLE @DATA(LT_SPFLI)
WHERE CARRID IN @U_R_AIRLIN[].

ENDFORM.

FORM TEST_SUB_2 USING U_R_AIRLIN TYPE TY_R_AIRLINE.

LOOP AT U_R_AIRLIN[] INTO DATA(LS_DATA).


" TODO: Modify it
ENDLOOP.

SELECT * FROM SPFLI


INTO TABLE @DATA(LT_SPFLI)
WHERE CARRID IN @U_R_AIRLIN[].

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

➢ Remote Function Call


Remote Function Call

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 Module How to call a


Maintenance Function Module
FM_02 CALL FUNCTION
Interface 'FM_02'
Import EXPORTING ...
Export IMPORTING ...
Tables TABLES ...
Exceptions (EXCEPTIONS ...)
Program
Documentation
Administration
Interfaces
Exception Processing

CALL FUNCTION ‘STRING_SPLIT’


EXPORTING DELIMITER = ‘-’
STRING = TEXT
IMPORTING HEAD = HEAD
TAIL = TAIL
EXCEPTIONS NOT_FOUND = 01
FUNCTION STRING_SPLIT. TOO_LONG = 02
... OTHERS = 03.
IF ... CASE SY-SUBRC.
RAISE NOT_FOUND. WHEN 1. .... .
ENDIF WHEN 2. .... .
WHEN 3. .... .
ENDCASE.
Examples of a Function Module
❖ Check following Standard Function Modules:
1. POPUP_TO_CONFIRM
2. CONVERT_TO_LOCAL_CURRENCY
3. CONVERT_TEMPERATURE
4. READ_CUSTOMERS
Demo 5
1. Try to use POPUP_TO_CONFIRM to output a popup like this.

2. Write which choice user clicked to screen.


Creating a Function
Module
Modularization Part 8
Function Module Name
❖ The names you use for your own
function modules should begin with
Y_* or Z_*.
❖ SAP has reserved this naming range
specially for customers' own use.
❖ Example:
YXXX_* is incorrect name
Y_XXX_* is correct name
Naming Rule
❖ Function Group name: YXXX_MOD_FGRP01
❖ Function Module name: Y_XXX_*
❖ XXX is your trainee number id
e.g. Y_21001_GET_AIRLN_NM
Y_21001_CREATE_AIRLN
Y_21001_GET_COUNTRY_LIST
Y_21001_READ_COUNTRY_NM
Function Groups
Tools
ABAP Workbench

Function Builder
Creating a Function Module 1
ABAP Function Builder: Initial Screen

Function module Z_FUNC_DEMO1

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
*"----------------------------------------------------------------------

* Get data from table SCARR (SINGLE)


SELECT SINGLE * FROM SCARR
INTO EX_AIRLN_INFO
WHERE CARRID = IM_AIRLINE.

* Check result, if not found then raise NOT_FOUND


IF SY-SUBRC <> 0.
RAISE NOT_FOUND.
ENDIF.

ENDFUNCTION.
Global Data / Local Memory
Global Data

L<gr> TOP
FUNCTION-POOL <gr>.
DATA: X.
TABLES: ... .

Function module Subroutines

L<gr> U01 L<gr> F01


FUNCTION ... . FORM SUB1 USING ... .
DATA:... . DATA:... .
MOVE X TO ... . MOVE ... TO X.
ENDFUNCTION. ENDFORM.
Demo: a chains of Function Module
* Using includes of Function Group, we can:
1. Reuse subroutine
2. Reuse structure type, other data types, etc.
3. Form a chains of Function Module, that we must call in a specific order
Add Source Code to TOP Include
FUNCTION-POOL YXXX_MOD_FGRP01. "MESSAGE-ID ..

* INCLUDE LYXXX_MOD_FGRP01D... " Local class definition

DATA: GT_COUNTRY_LIST TYPE STANDARD TABLE OF T005T.


Add Source Code to Function Module 1
FUNCTION Y_XXX_GET_COUNTRY_LIST.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IM_LANGUAGE) TYPE SY-LANGU DEFAULT 'EN'
*" TABLES
*" T_COUNTRY_LIST STRUCTURE T005T OPTIONAL
*" EXCEPTIONS
*" NOT_FOUND
*"----------------------------------------------------------------------

* Get all data from T005T by inputted Language


SELECT * FROM T005T
INTO TABLE GT_COUNTRY_LIST
WHERE SPRAS = IM_LANGUAGE.

* Check error raise NOT_FOUND


IF SY-SUBRC <> 0.
RAISE NOT_FOUND.
ELSE.
T_COUNTRY_LIST[] = GT_COUNTRY_LIST[].
ENDIF.

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
*"----------------------------------------------------------------------

* Check if itab Country List is empty or not


* if it is empty raise COUNTRY_LIST_IS_EMPTY
IF GT_COUNTRY_LIST IS INITIAL.
RAISE COUNTRY_LIST_IS_EMPTY.
ENDIF.

* Read internal table Country List into EX_COUNTRY_NAME


* if not found then raise NOT_FOUND
READ TABLE GT_COUNTRY_LIST INTO DATA(LS_COUNTRY)
WITH KEY LAND1 = IM_COUNTRY_CODE.
IF SY-SUBRC = 0.
EX_COUNTRY_NAME = LS_COUNTRY-LANDX.
ELSE.
RAISE NOT_FOUND.
ENDIF.
ENDFUNCTION.
Thank you
Q&A

You might also like