BC170_2.09.1
Modularization Techniques
BC170_2.09.2
Objectives
• The participants will be able to:
–
Create Subroutines
–
Describe the various methods for passing
Parameters to Subroutines
–
Create Function Modules
–
Call Function Modules from ABAP
Programs
BC170_2.09.3
REPORT YDEMOSB8.
Call calculated_tax
Call calculated_tax
Subroutine
Calculated_tax
Calculate tax
Calculate tax
REPORT YDEMOSB7.
What Are Subroutines
BC170_2.09.4
Modularization
REPORT YDEMOSB1.
Form sub
ABAP Function Builder
Function module
ZZCALC
REPORT YDEMOSB2.
Perform sub(YDEMOSB1
)
REPORT YDEMOSB1.
Perform sub
Form sub
REPORT YDEMOSB3.
Call Function ZZCALC
External
Call
FunctionMod
ule
Call
BC170_2.09.5
99
1
a1
a2
a3
Call Calculate_tax
Transfer values of fields a1 a2
Get value of field a3
Actual parameters a1 a2 a3
Formal parameters f1 f2 f3
Subroutine calculate_tax
99 1
f1
f2
f3
f3 = f1 +f2
. . . .
Actual / Formal Parameters
Parameter transfer
Parameter transfer
Main
Main
Program
Program
Subro
utine
Subro
utine
BC170_2.09.6
a1
a1
f1
f1
Subroutin
e
Pass by Value and Result
Pass by Value and Result
Subro
utine
Pass by Value
Pass by Value
a1
a1
f1
f1
Subro
utine
Pass by
Pass by
Reference
Reference
a1
a1
f1
f1
Methods of Passing Parameters
BC170_2.09.7
REPORT Y170D091.
TABLES: ....
DATA: .…
.
.
.
PERFORM
<name> USING
<a1> <a2>
<a3> <a4>.
.
.
.
FORM <name> USING
VALUE(<f1>)
VALUE(<f2>)
<f3>
<f4>.
ENDFORM.
Declaring and Calling a
Subroutine - Pass by Value(1) &
Reference(2)
Pass by reference
Pass by value
BC170_2.09.8
Pass by value
Declaring and Calling a
Subroutine - Pass by Value and
Result(3)
REPORT YSDEM000.
TABLES: ... .
DATA: ... .
.
.
.
PERFORM
<name> USING
<a1> <a2>
CHANGING<a3>.
.
.
.
FORM
<name> USING
VALUE(<f1>)
VALUE(<f2>)
CHANGING VALUE(<f3>).
.
.
.
f 3 =“A”.
.
.
.
ENDFORM.
Pass by value and result
BC170_2.09.9
Global versus Local Data
report
y170dm86.
data: num(9) value ‘999999999’.
data:
cust_id like kna1-kunnr,
name like kna1-name1.
. . . <statements>
perform sub1.
. . . <statements>
* --------------------------------------------------------------------------------- ----------------*
*
FORM SUB1
*
* ------------------------------------------------------------------------------------------------------------------------------------- -----------------*
form sub1.
data: begin of tab occurs 10,
“ <----------- local data
cust_id like kna1-kunnr,
name1
like kna1-name1,
end of tab.
local: num
.
“ <----------- local data
. . . <statements>
endform.
Global Data
Local Data
BC170_2.09.10
Statement Value Upon Entering Value Upon Value
Upon Entering
between Subroutine First Time Returning to
Subroutine Next
FORM…
Main Program
Time
ENDFORM
DATA
Whatever the local If declared Re-
initializes based
DATA statement globally, returns on local
DATA
initializes
to old value,
statement
otherwise not
recognized
STATICS Whatever the local If declared Last set
value from
STATICS statement globally, returns inside
subroutine
initializes
to old value,
otherwise not
recognized
LOCAL
Last set value from Last set value Last set
value from
main program from main main
program
program
Local Data in Subroutines
BC170_2.09.11
REPORT Y170D095.
TABLES: KNA1.
DATA: ZTAB LIKE KNA1.
DATA: FLAG.
.
.
PERFORM SUB1 USING KNA1.
PERFORM SUB1 USING ZTAB.
.
.
.
Passing Structures as
Parameters
FORM SUB1 USING
REC STRUCTURE KNA1.
WRITE: / REC-LAND1,
REC-NAME1.
ENDFORM.
This is passing the
KNA1 work area only
This is passing
the ZTAB
structure
BC170_2.09.12
Passing Internal Tables as
Parameters
REPORT Y170D093.
DATA : BEGIN OF TAB OCCURS 10,
F1 LIKE KNA1-LAND1,
F2 LIKE KNA1-NAME1,
.
.
.
END OF TAB.
DATA : X.
.
.
.
PERFORM SUB1 TABLES TAB
. USING X.
.
.
FORM SUB1
TABLES FRED STRUCTURE TAB
USING Z.
LOOP AT FRED.
WRITE: / FRED-F1
FRED-F2.
ENDLOOP.
ENDFORM .
BC170_2.09.13
Roll Area
Storage Allocation
External Subroutines
REPORT Y170D096.
TABLES : KNA1,
T001, T005.
.
.
.
START-OF-SELECTION.
.
.
.
PERFORM
SUB1(Y170D097) . . . .
.
.
.
REPORT Y170D097.
TABLES : KNA1,
T001, T001G.
.
.
.
START-OF-SELECTION
.
.
.
FORM SUB1 . . . .
.
.
.
NEW-PAGE.
.
.
.
ENDFORM
PERFORM <
name
>(<prog name>) USING . . .
KNA1
T001
T005
T001G
Y170D108
Y170D109
BC170_2.09.14
Function Modules
Function
Builder
FM_01 ...
FM_02 ...
FM Group : FIBU
Program 1
CALL
FUNCTION
‘FM_0’
. . .
Program 3
CALL FUNCTION
‘FM_02’
. . .
FM_03 ...
FM_04 ...
FM Group : XYZ
Program 2
CALL
FUNCTION
‘FM_02’
. . .
BC170_2.09.15
Function Module Parts
FM_01 ...
FM_02 ...
FM Group :
FM Group :
FIBU
FIBU
FM_03 ...
FM_04 ...
FM Group :
FM Group :
XYZ
XYZ
Section I … Attributes
Section II … Interface
Chapter 1 … Import
Chapter 2 … Changing
Chapter 3 … Export
Chapter 4 … Tables
Chapter 5 … Exceptions
Section III … Documentation
Section IV … Source Code
Section V … Global Data
Section VI … Main Program
FM -02
BC170_2.09.16
Creating a Function Group
BC170_2.09.17
Attributes
BC170_2.09.18
Interfaces
Program XYZ.
Call function
‘Y_DEMO_FUNC’
Function Module
Y_DEMO_FUNC
Import
Changing
Export
Tables
Exceptions
BC170_2.09.19
Import/Export Parameter
Interface
Parameter name Default value
Pass parameter
by value
Flag parameter
as optional
‘Ref. Field’
LIFNR LIKE LFA1-LIFNR
‘TYPE’
BC170_2.09.20
Table Parameters/Exceptions
Interface & Documentation
Parameter name
‘LIKE’
Flag parameter
as optional
‘TYPE’
BC170_2.09.21
function y_demo_function_module.
--------------------------------------------------------------------------------------------------
Local interface:
IMPORTING
REFERENCE(FIELD1) LIKE KNA1-NAME1
EXPORTING
VALUE(FIELD3) LIKE KNA1-LAND1
TABLES
TAB STRUCTURE KNA1
CHANGING
VALUE(FIELD2) LIKE KNA1-KUNNR
EXCEPTIONS
INVALID_DATA
SYSTEM_ERROR
-------------------------------------------------------------------------------------------------
endfunction.
*”
*”
*”
*”
*”
*”
*”
*”
*”
*”
*”
*”
*”
*”
Function Module Source Code
BC170_2.09.22
Exceptions
. . .
IF...
RAISE INVALID_OPERATOR.
*(Or use) MESSAGE. . . RAISING
* INVALID_OPERATOR.
ENDIF.
Function Module
INVALID_OPERATOR
BC170_2.09.23
case operator.
when
’+’.
result = operand1 + operand2.
when
’
-
’.
result = operand1 - operand2.
when
’
/
’.
if operand2 <> 0.
result = operand1 / operand2.
else.
raise division_by_zero.
endif.
when
’
’
result = operand1 operand2.
when
others.
raise invalid_operator.
endcase.
*
*
Example - Raising Exceptions
BC170_2.09.24
Calling a Function Module
report ymodemo message-id yj.
parameters:
operand1 type i,
operator,
operand2 like operand1.
data:
result type p decimals 2.
call function ’Y_CALCULATOR’
exporting
operand1
= operand1
operator
= operator
operand2
= operand2
importing
result
= result
exceptions
invalid_operator= 1
division_by_zero= 2
others
= 3.
BC170_2.09.25
exceptions
invalid_operator= 1
division_by_zero= 2
others
= 3.
case sy-subrc.
when 1.
message
e001.
when 2.
message
e005.
when 3.
message
e007.
endcase.
Calling a Function Module
(Continued)
BC170_2.09.26
Program Organization
FUNCTION FA.
L<gr>U01
L<gr>U01
FUNCTION FB.
L<gr>U02
L<gr>U02
FUNCTION-POOL <gr>
Message-ID ZZ.
DATA: “Global DATA
L<gr>TOP
L<gr>TOP
L<gr>UXX
INCLUDE L<gr>
U01.
INCLUDE L<gr>
U02.
.
.
.
*System-defined include files
*User-defined include files
INCLUDE L<gr>TOP.
INCLUDE L<gr>UXX
SAPL <gr>
SAPL <gr>
BC170_2.09.27
Subroutine Includes for
Function Groups
FORM SUB1 USING . . . .
.
.
.
ENDFORM.
FORM SUB2 USING . . . .
.
.
.
ENDFORM.
.
.
.
FUNCTION . . .
.
.
.
PERFORM SUB1 USING . . .
.
.
.
ENDFUNCTION.
•
System-defined include files INCLUDE L<gr> TOP.’ Global Data
•
User defined include files INCLUDE L<gr> F01.’ Subroutines
ABAP program L<gr> F01
Call
Main Program
BC170_2.09.28
FORM SUB1 USING...
DATA: . . .
MOVE. . . TO X.
ENDFORM.
Subroutines
L <gr> F01
Global Data / Local Memory in
the Function Group
FUNCTION . . .
DATA: . . .
MOVE X TO . . .
ENDFUNCTION.
Program
Global Data
FUNCTION-POOL <gr>.
DATA: X
TABLES: . . .
L <gr> TOP
BC170_2.09.29
Test Environment
Export Parameters
Import parameters
FM: Y170 DEMO
Does it work?
Tables
BC170_2.09.30
Managing Function Modules
Attributes
BC170_2.09.31
Remote Function Call (RFC)
External program
External program
External Program. . .
RfcOpen (. . .)
RfcCallReceive (. . .)
RfcClose (. . .)
Program
Program
CALL FUNCTION. . .
DESTINATION. . .
EXPORTING. . .
IMPORTING. . .
TABLES. . .
EXCEPTIONS. . .
Function Group
Function Group
FUNCTION-POOL . . . .
Function Module
Function Module
Function Module
Function Module
FUNCTION REMOTE_CALL
. . .
RAISE ERROR.
. . .
ENDFUNCTION.
ABAP
\/4
RFCLid
BC170_2.09.32
Displaying Function Module
BC170_2.09.33
Modularization:
The Include Technique
BC170_2.09.34
Summary
• The participants should be able to:
–
Create Subroutines
–
Describe the various methods for passing
Parameters to Subroutines
–
Create Function Modules
–
Call Function Modules from ABAP
Programs