You are on page 1of 39

IBM Global Services

Modularization Techniques

Modularization Techniques |

Dec-2008

© 2005 IBM Corporation

IBM Global Services

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

2

Modularization Techniques |

Dec-2008

© 2005 IBM Corporation

IBM Global Services

What Are Subroutines ?
REPORT YEXAMPLE1.

REPORT YEXAMPLE2.

Calculate tax

Call calculated_tax

Calculate tax

Call calculated_tax

Subroutine
Calculate_tax

3

Modularization Techniques |

Dec-2008

© 2005 IBM Corporation

REPORT YEXAM2.IBM Global Services Modularization REPORT YEXAM1. Form sub 4 Modularization Techniques | FunctionModule Call ABAP Function Builder Function module ZZCALC Dec-2008 © 2005 IBM Corporation . REPORT YEXAM3. Perform sub(YDEMOSB1) Call Function ZZCALC Perform sub Form sub External Call REPORT YEXAMS1.

5 Modularization Techniques | Dec-2008 © 2005 IBM Corporation . . .IBM Global Services Actual / Formal Parameters Main Program 99 1 a1 a2 a3 Call Calculate_tax Transfer values of fields a1 a2 Get value of field a3 Actual parameters a1 a2 a3 Parameter transfer ine t u o r Sub Formal parameters f1 f2 f3 Subroutine calculate_tax 99 1 f1 f2 f3 f3 = f1 +f2 . .

IBM Global Services Methods of Passing Parameters Pass by Value a1 tine u o r b Su f1 Pass by Value and Result a1 Pass by Reference a1 Subro u tine f1 ine t u o r Sub f1 6 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .

PERFORM <name> USING <a1> <a2> <a3> <a4>.Pass by Value(1) & Reference(2) REPORT Y170D091. . .… .IBM Global Services Declaring and Calling a Subroutine . 7 Modularization Techniques | Dec-2008 © 2005 IBM Corporation . DATA: . . . <statements> Pass by value Pass by reference ENDFORM. . FORM <name> USING VALUE(<f1>) VALUE(<f2>) <f3> <f4>.

f 3 =“A”. .. . .. . 8 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .IBM Global Services Declaring and Calling a Subroutine . . . . FORM <name> USING VALUE(<f1>) Pass by value VALUE(<f2>) CHANGING VALUE(<f3>). PERFORM <name> USING <a1> <a2> CHANGING<a3>. DATA: . Pass by value and result .Pass by Value and Result(3) REPORT YSDEM000. . ENDFORM.

. . . Local Data name1 type name1.IBM Global Services Global versus Local Data report y170dm86. end of tab. <statements> Global Data * ------------------------------------------------------------------------------------------------* * FORM SUB1 * * ------------------------------------------------------------------------------------------------* form sub1. . <statements> perform sub1. “ <----------. name type name1. . 9 Modularization Techniques | Dec-2008 © 2005 IBM Corporation . data: num(9) value ‘999999999’. <statements> endform. data: id type id. . . local: num . .local data id type id.local data . “ <----------. data: begin of tab .

otherwise not recognized Last set value from inside subroutine LOCAL Last set value from main program Last set value from main program Last set value from main program 10 Modularization Techniques | Dec-2008 © 2005 IBM Corporation . otherwise not recognized Re-initializes based on local DATA statement STATICS Whatever the local STATICS statement initializes If declared globally. returns to old value.IBM Global Services Local Data in Subroutines Statement Between FORM… ENDFORM Value Upon Entering Subroutine First Time Value Upon Value Upon Returning to Main Entering Program Subroutine Next Time DATA Whatever the local DATA statement Initializes If declared globally. returns to old value.

WRITE: / WA_TAB-COUNTRY. PERFORM SUB1 USING WA_TABNA. . .IBM Global Services Passing Structures as Parameters REPORT Y170D095. ZTAB structure . . . 11 Modularization Techniques | Dec-2008 © 2005 IBM Corporation . This is passing the DATA: IT_TAB TYPE STANDARD TABLE OF TABNA. TABNA work area DATA: WA_TAB TYPE TABNA. FORM SUB1 USING WA_TAB TYPE TABNA. WA_TAB-NAME1. only DATA: FLAG. This is passing the PERFORM SUB1 USING IT_TAB[]. ENDFORM.

DATA : X TYPE C. DATA: IT_TAB TYPE STANDARD TABLE OF TY_TAB. . DATA: WA_TAB TYPE TY_TAB. END OF TY_TAB. F1 TYPE TABNA-COUNTRY.IBM Global Services Passing Internal Tables as Parameters REPORT Y170D093. ENDFORM . . . 12 Modularization Techniques | Dec-2008 © 2005 IBM Corporation . LOOP AT IT_TAB INTO WA_TAB. ENDLOOP. WRITE: / WA_TAB-F1 WA_TAB-F2. F2 TYPE TABNA-NAME1. . PERFORM SUB1 USING IT_TAB[] X. TYPES : BEGIN OF TY_TAB. . FORM SUB1 USING IT_TAB TYPE TY_T_TAB Z TYPE C. TYPES: TY_T_TAB TYPE STANDARD TABLE OF TY_TAB.

. . ENDFORM Modularization Techniques | Roll Area TABNA T001 T005 T001G Y170D108 Y170D109 Storage Allocation Dec-2008 © 2005 IBM Corporation . . . . . . START-OF-SELECTION . . T001 TYPE T001. . REPORT Y170D096. . . T001G TYPE T001G. T001 TYPE T001. DATA: TABNA TYPE TABNA. PERFORM SUB1(Y170D097) . . . . 13 REPORT Y170D097. START-OF-SELECTION. . FORM SUB1 . .IBM Global Services External Subroutines PERFORM <name>(<prog name>) USING . . . . NEW-PAGE. . . DATA : TABNA TYPE TABNA. . . T005 TYPE T005. . . .

14 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .IBM Global Services Demonstration  Creating a subroutine in a program to modularize the logic.

IBM Global Services Practice  Creating a subroutine in a program to modularize the logic. 15 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .

..IBM Global Services Function Modules Function Builder Program 1 CALL FUNCTION ‘FM_02’ ... Program 2 FM Group : XYZ FM_03 . CALL FUNCTION ‘FM_02’ .. FM_04 . FM_02 . 16 Modularization Techniques | Dec-2008 © 2005 IBM Corporation ..... FM Group : FIBU FM_01 . Program 3 CALL FUNCTION ‘FM_02’ ......

FM Group : XYZ FM_03 .. FM_04 . FM_02 ... Section V … Global Data Section VI … Main Program 17 Modularization Techniques | Dec-2008 © 2005 IBM Corporation ...IBM Global Services Function Module Parts FM -02 Section I … Administration 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 FM Group : FIBU FM_01 ....

IBM Global Services Creating a Function Group 18 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .

IBM Global Services Function Module 19 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .

IBM Global Services Interfaces Program XYZ. Call function ‘Y_DEMO_FUNC’ Import Changing Export Tables Exceptions 20 Modularization Techniques | Function Module Y_DEMO_FUNC Dec-2008 © 2005 IBM Corporation .

IBM Global Services Import/Export Parameter Interface ‘LIKE’ Default value Pass parameter by reference Parameter name ‘TYPE’ 21 Modularization Techniques | Flag parameter as optional Dec-2008 © 2005 IBM Corporation .

IBM Global Services Table Parameters/Exceptions Interface & Documentation ‘TYPE REF TO’ Flag parameter as optional Parameter name ‘TYPE’ 22 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .

23 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .IBM Global Services Function Module Source Code function y_demo_function_module. *” --------------------------------------------------------------------------------------*” Local interface: IMPORTING *” REFERENCE(FIELD1) TYPE COUNTRY *” EXPORTING *” VALUE(FIELD3) TYPE NAME1 *” TABLES *” TAB STRUCTURE TABNA *” CHANGING *” VALUE(FIELD2) TYPE ID *” EXCEPTIONS *” INVALID_DATA *” SYSTEM_ERROR *” *” -------------------------------------------------------------------------------------endfunction.

RAISING INVALID_OPERATOR.. IF...IBM Global Services Exceptions INVALID_OPERATOR Function Module . . ENDIF. * MESSAGE. .. RAISE INVALID_OPERATOR. .. 24 Modularization Techniques | Dec-2008 © 2005 IBM Corporation ..

result = operand1 . result = operand1 / operand2. if operand2 <> 0.Raising Exceptions case operator. raise division_by_zero. result = operand1 + operand2. endcase. when ’+’. when ’ * ’ result = operand1 * operand2. else.operand2. endif. when ’/’. when ’-’. raise invalid_operator.IBM Global Services Example . when others. 25 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .

IBM Global Services Calling a Function Module report ymodemo message-id yj. operator TYPE C. data: result type p decimals 2. 26 Modularization Techniques | Dec-2008 © 2005 IBM Corporation . parameters: operand1 type i. call function ’Y_CALCULATOR’ exporting operand1 = operand1 operator = operator operand2 = operand2 importing result = result exceptions invalid_operator =1 division_by_zero = 2 others = 3. operand2 TYPE operand1.

endcase. case sy-subrc. message e007. when 2. message e001. message e005. when 1. 27 Modularization Techniques | Dec-2008 © 2005 IBM Corporation . when 3.IBM Global Services Calling a Function Module (Contd.) exceptions invalid_operator = 1 division_by_zero = 2 others = 3.

. DATA: “Global DATA *User-defined include files L<gr>UXX L<gr>U01 FUNCTION FA. INCLUDE L<gr> U02.IBM Global Services Program Organisation SAPL <gr> *System-defined include files L<gr>TOP INCLUDE L<gr>TOP. L<gr>U02 INCLUDE L<gr> U01. FUNCTION FB. INCLUDE L<gr>UXX FUNCTION-POOL <gr> Message-ID ZZ. 28 Modularization Techniques | Dec-2008 © 2005 IBM Corporation . . .

. Dec-2008 © 2005 IBM Corporation . . .IBM Global Services 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. ENDFUNCTION. FORM SUB2 USING . . . . . . . . ENDFORM. . . . ENDFORM. . 29 Modularization Techniques | FUNCTION . . . . . . . PERFORM SUB1 USING . . .’ Subroutines ABAP program L<gr> F01 Call FORM SUB1 USING . .

. 30 Modularization Techniques | Subroutines L <gr> F01 FORM SUB1 USING. MOVE X TO . Dec-2008 © 2005 IBM Corporation . . . DATA: X TYPES: . . . . DATA: . TO X. ENDFUNCTION. . ENDFORM. . . . MOVE. . Program FUNCTION . . . DATA: ..IBM Global Services Global Data / Local Memory in the Function Group Global Data L <gr> TOP FUNCTION-POOL <gr>.

IBM Global Services Test Environment Import parameters Tables FM: Y170 DEMO Does it work? Export Parameters 31 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .

IBM Global Services Managing Function Modules Administration 32 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .

. .IBM Global Services Remote Function Call (RFC) Program CALL FUNCTION. ENDFUNCTION. . . . . .. . . . RFCLid External Program. . . . External program Function Group ABAP FUNCTION-POOL . . IMPORTING.) 33 Modularization Techniques | Dec-2008 © 2005 IBM Corporation . . RfcOpen (. EXPORTING.) RfcCallReceive (... .. . . . TABLES. EXCEPTIONS. . Function Function Module Module FUNCTION REMOTE_CALL . . . . RAISE ERROR.) RfcClose (. DESTINATION. .

IBM Global Services Displaying Function Module 34 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .

“ Yes Flag no(1) type c value ‘N’. ‘Date is :’(002). ‘Report Name:’(003) . sy-datum. “ Text elements cannot clash with report_name = ‘Include Demo’(502). “ No Flag. PERFORM sub_print_report_heading. ‘Report ID:’(004). “ Number of errors that occurred * The use of START-OF-SELECTION is discussed in the interactive reporting * Usually it is not required. “ those in includes * All the code for the report would be here * Errors variables will be set depending on if errors occurred * At the end If errors = ‘E’. sy-repid. * Write rest of report Write : / / / / ‘Report for:’(001) . *---------------------------------------------------------------------* * YINCLUDE_TECHNIQUE_SUBROUTINES *---------------------------------------------------------------------* FORM sub_print_report_heading. “Flag if errors have occurred Error_num type I value 0. “ Holds company report is for Report_id type sy-repid. * Set variables for this report company_name = ‘Demo Company’(501). unless using includes in this manner START-OF-SELECTION. “ Holds SAP report ID Report_name(30) type c. “ Holds IBM description Errors(1) type c value ‘E’. *---------------------------------------------------------------------* * YINCLUDE_TECHNIQUE_GLOBAL_DATA *---------------------------------------------------------------------* CONSTANTS : yes(1) type c value ‘Y’. report_name. company_name.IBM Global Services Modularization: The Include Technique REPORT yinclude_technique. Include yinclude_technique_global_data. Data : company_name(50) type c. Include yinclude_technique_subroutines. ENDFORM. “SUB_PRINT_REPORT_HEADING 35 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .

36 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .IBM Global Services Demonstration  Creating a function module and using it in a program.

IBM Global Services Practice  Creating a function module and using it in a program. 37 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .

 Parameters passed to the subroutine using one of the following three methods. Pass by value and result. 38 Modularization Techniques | Dec-2008 © 2005 IBM Corporation . Pass by reference.  The additional parameter DESTINATION is required for the remote call of a Function Module. which saves developers from creating redundant code and increases programming efficiency. Pass by value. Specification of a function group is required when creating a function module.  Function groups are collections of logically related functions modules.  The benefit of function modules lies in their reusability. a function module stored centrally in the Function Builder.IBM Global Services Summary  Redundancy in program can be avoided using subroutines.  Subroutines are used in three different ways: internal call. external call.

IBM Global Services Questions  Why subroutine is used in the program?  What is function module? What is the advantage of using it in the program?  What are the different parameters of a function module?  What is function group?  How a function module can be remote enabled? 39 Modularization Techniques | Dec-2008 © 2005 IBM Corporation .