Intro to ABAP - Chapter 09
Description
sap abap introduction, very specific,welcome download
Document Sample


Modularization Techniques
BC170_2.09.1
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.2
What Are Subroutines
REPORT YDEMOSB7. REPORT YDEMOSB8.
Calculate tax Call calculated_tax
Calculate tax Call calculated_tax
Subroutine
Calculated_tax
BC170_2.09.3
Modularization
REPORT YDEMOSB1. REPORT YDEMOSB2. REPORT YDEMOSB3.
Perform sub
Perform sub(YDEMOSB1) Call Function ZZCALC
Form sub
External FunctionModule
Call Call
REPORT YDEMOSB1. ABAP Function Builder
Function module
Form sub ZZCALC
BC170_2.09.4
Actual / Formal Parameters
Main 99 1
Program a1 a2 a3
Call Calculate_tax
Transfer values of fields a1 a2
Get value of field a3
Actual parameters a1 a2 a3
Parameter transfer
Formal parameters f1 f2 f3
Subroutine calculate_tax
99 1
f1 f2 f3
f3 = f1 +f2 . . . .
BC170_2.09.5
Methods of Passing Parameters
Pass by Value a1
Pass by Value and Result
f1
a1
Pass by Reference a1 f1
f1
BC170_2.09.6
Declaring and Calling a Subroutine -
Pass by Value(1) & Reference(2)
REPORT Y170D091.
TABLES: ....
DATA: .…
.
.
.
PERFORM <name> USING
<a1> <a2>
<a3> <a4>.
.
.
.
FORM <name> USING
VALUE(<f1>)
VALUE(<f2>)
Pass by value
<f3>
<f4>. Pass by reference
BC170_2.09.7
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>) Pass by value
VALUE(<f2>)
CHANGING VALUE(<f3>).
.
Pass by value and result
.
.
f 3 =“A”.
.
.
.
BC170_2.09.8
ENDFORM.
Global versus Local Data
report y170dm86.
data: num(9) value ‘999999999’.
data: cust_id like kna1-kunnr, Global Data
name like kna1-name1.
. . . <statements>
perform sub1.
. . . <statements>
* --------------------------------------------------------------------------------- Local Data ----------------*
* 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.
BC170_2.09.9
Local Data in Subroutines
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
BC170_2.09.10
Passing Structures as Parameters
This is passing the KNA1
REPORT Y170D095.
work area only
TABLES: KNA1.
DATA: ZTAB LIKE KNA1.
DATA: FLAG.
.
.
PERFORM SUB1 USING KNA1. This is passing the
PERFORM SUB1 USING ZTAB. ZTAB structure
.
.
.
FORM SUB1 USING
REC STRUCTURE KNA1.
WRITE: / REC-LAND1,
REC-NAME1.
ENDFORM.
BC170_2.09.11
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.12
External Subroutines
PERFORM <name>(<prog name>) USING . . .
REPORT Y170D096. REPORT Y170D097.
TABLES : KNA1, TABLES : KNA1,
T001, T005. T001, T001G.
. .
. . Roll Area
. .
KNA1
START-OF-SELECTION. START-OF-SELECTION
. . T001
. .
. . T005
PERFORM FORM SUB1 . . . . T001G
SUB1(Y170D097) . . . . .
. Y170D108
.
. . Y170D109
. NEW-PAGE.
.
. Storage Allocation
.
ENDFORM
BC170_2.09.13
Function Modules
Function Builder
FM Group : FIBU
FM_01 ...
Program 1 FM_02 ... Program 2
FM Group : XYZ
CALL FM_03 ... CALL
FUNCTION FM_04 ... FUNCTION
‘FM_0’ ‘FM_02’
... Program 3 ...
CALL FUNCTION
‘FM_02’
...
BC170_2.09.14
Function Module Parts
FM -02
Section I … Attributes FM Group : FIBU
Section II … Interface
FM_01 ...
Chapter 1 … Import FM_02 ...
Chapter 2 … Changing
Chapter 3 … Export
Chapter 4 … Tables
Chapter 5 … Exceptions
FM Group : XYZ
Section III … Documentation
FM_03 ...
Section IV … Source Code
FM_04 ...
Section V … Global Data
Section VI … Main Program
BC170_2.09.15
Creating a Function Group
BC170_2.09.16
Attributes
BC170_2.09.17
Interfaces
Program XYZ.
Call function
‘Y_DEMO_FUNC’
Import
Changing
Export
Tables
Exceptions Function Module
Y_DEMO_FUNC
BC170_2.09.18
Import/Export Parameter Interface
Parameter name Default value Flag parameter
as optional
Pass parameter
by value
‘TYPE’ ‘Ref. Field’
LIFNR LIKE LFA1-LIFNR
BC170_2.09.19
Table Parameters/Exceptions
Interface & Documentation
Parameter name
Flag parameter
as optional
‘TYPE’ ‘LIKE’
BC170_2.09.20
Function Module Source Code
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.
BC170_2.09.21
Exceptions
INVALID_OPERATOR
...
Function Module
IF...
RAISE INVALID_OPERATOR.
*(Or use) MESSAGE. . . RAISING
* INVALID_OPERATOR.
ENDIF.
BC170_2.09.22
Example - Raising Exceptions
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.
BC170_2.09.23
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.24
Calling a Function Module (Continued)
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.
BC170_2.09.25
Program Organization
SAPL <gr>
L<gr>TOP
*System-defined include files
FUNCTION-POOL <gr>
INCLUDE L<gr>TOP. Message-ID ZZ.
INCLUDE L<gr>UXX DATA: “Global DATA
*User-defined include files
L<gr>UXX
L<gr>U01 INCLUDE L<gr> U01.
INCLUDE L<gr> U02.
FUNCTION FA. .
L<gr>U02 .
.
FUNCTION FB.
BC170_2.09.26
Subroutine Includes for Function
Groups
Main Program
• 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
FORM SUB1 USING . . . . FUNCTION . . .
. .
. .
. .
ENDFORM. PERFORM SUB1 USING . . .
FORM SUB2 USING . . . . .
. .
. .
. ENDFUNCTION.
ENDFORM.
.
.
.
BC170_2.09.27
Global Data / Local Memory in the
Function Group
Global Data
L <gr> TOP
FUNCTION-POOL <gr>.
DATA: X
TABLES: . . .
Program Subroutines
FUNCTION . . . L <gr> F01
DATA: . . . FORM SUB1 USING...
MOVE X TO . . . DATA: . . .
ENDFUNCTION. MOVE. . . TO X.
ENDFORM.
BC170_2.09.28
Test Environment
Import parameters Tables
FM: Y170 DEMO
Does it work?
Export Parameters
BC170_2.09.29
Managing Function Modules
Attributes
BC170_2.09.30
Remote Function Call (RFC)
Program
ABAP \/4
Function Group
CALL FUNCTION. . . FUNCTION-POOL . . . .
DESTINATION. . .
EXPORTING. . . Function Module
IMPORTING. . .
TABLES. . . FUNCTION REMOTE_CALL
EXCEPTIONS. . . ...
RAISE ERROR.
...
ENDFUNCTION.
External program
RFCLid
External Program. . .
RfcOpen (. . .)
RfcCallReceive (. . .)
RfcClose (. . .)
BC170_2.09.31
Displaying Function Module
BC170_2.09.32
Modularization:
The Include Technique
BC170_2.09.33
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
BC170_2.09.34