Upload
valentini400
View
226
Download
1
Embed Size (px)
Citation preview
Modularization Technique
Modularization techniques are used to reuse the block of statements in a program or from other programs
There are 2 types:
1) SOURCE CODE MODULARIZATION: These will not have interfaceMacros (it’s not used now)Include: it will not execute directly, but can include in other programsSyntax:INCLUDE <Name of the Include(prg name of type include)>
2) PROCEDURES: These will have the interfaceSub routinesFunction modules
Subroutines: These are used to re-use the lines of code within the program and outside the program.
It is having 2 components1) Calling the subroutine2) Definition
Calling will call the subroutine to execute the lines of code inside itNote: Calling may be along with input & output parameters or may notSyntax:PERFORM <Name of the subroutine>.OrPERFORM <Name of the subroutine> TABLES <Itab> USING <F1>ORPERFORM <Name of the subroutine> USING <f1>ORPERFORM <Name of the subroutine> CHANGING<V> .Note:F1 is called as actual parameter
Definition will have lines of code which are need to be reusedSyntax:
FORM <Name of subroutine>......ENDFORM
(OR)
FORM <Name of subroutine> USING <P1> ...ENDFROM.
Note: P1 is called as formal parameter
Subroutines are 2 types: 1) Internal subroutine 2) External subroutineIf definition & calling are in same program is called as internal subroutine
If calling is in our program & definition is in other program is called as external subroutineSyntax:PERFORM <Name of subrot> IN PROGRAM <Program name>.ORPERFORM <Name of subrot> IN PROGRAM <Program name> USING <F>.
Input can be passed to subroutine in 3 ways
Pass by reference: The address of the current parameter is passed. Ex: PERFORM data_extract USING P_BUKRS
Pass by Value: When the subroutine is called, the formal parameters are copies of the current parameters, with their own storage location. Ex: PERFORM data_extract USING ‘0005’.
Pass by Value and Result : The formal parameters have a separate storage location. At the end of the subroutine, the value of the formal parameter is passed to the storage location of the current parameter assigned to it.
Ex: PERFORM Data_extract CHANGING i_tab.
Example 1
Step 1:
Step 2:
Step 3:
Step4
Example 2:Step1
Step 2
Step 3
Note: Definition of the subroutine will start
at the last line of the program
USING: it is the command which is used to give the input to subroutine
TABLES: it is the command which is used to take the input OR to give the output in terms of multiple records
CHANGING: it is the command to take the input and gives the output in the same internal table after some changes in that.
Note: Subroutines can access the data within the program and from other program, but it cannot access the data from outside the server
FUNCTION MODULE
1) Function modules also are used to reuse the block of statements2) Function modules can be tested individually without attaching to
program like subroutine3) Function modules are 2 types
Normal FM: it can access the data within the server
Remote FM: it can access the data outside the server also
4) Function Group will have all the related function modulesWhenever we are calling FM, other function modules in that function group will be loaded.
5) SE37 is the T.tcode to create the FM & function group
Note: Creation of the function group is covered in DDIC at table maintenance generator
Components of a FM1) ATTRIBUTES tab: This is used to maintain the kind of function
module either Normal or Remote an all the information like who created, when created etc are available
2) IMPORTS tab: This is used to define the parameters to give the input(Receive from program, this displaying at EXPORTING tab in program) to FM source code
3) EXPORTS tab: This is used to give the output from FM (it is displaying at IMPORTING in program)
4) CHANGING tab: This is used to give the input to FM and get the output from FM in the same int table or variable
5) TABLES tab: This is used to take the input or give the output from FM in terms of multiple records.
6) EXCEPTIONS tab: This is used to handle the errors. It’s just working like message class RAISE statement is used to raise the exceptions Maintained here
7) SOURCE CODE tab: This is used to write the required lines of code (logic)
FUNCTION MODULE Creation
Go to SE37
Click on Create button
Click on ‘Imports’ tab
Provide the parameters
Click on ‘Export’ tab and provide the work area or variable
Note: as we cannot define the structure here, we need to create the structure in SE11 with required fields
Example
Click on ‘TABLES’ tab
Note: Here also need to create structure in SE11 like below
Click on ‘Exceptions’ tab
Click on ‘Source code’ tab and write the logic
IF NOT P_BUKRS IS INITIAL.SELECT SINGLE BUKRS BUTXT ORT01 FROM T001 INTO WA WHERE BUKRS = P_BUKRS. IF SY-SUBRC = 0. SELECT KUNNR BUKRS ERDAT FROM KNB1 INTO TABLE I_TAB WHERE BUKRS = P_BUKRS. ENDIF. ELSE.RAISE NO_DATA. ENDIF
SAVE--CHECK--ACTIVATE
Test the FM
Enter the company code----Click on ‘Execute’
Now the int table will be populated if there is a data
Both WA & int table are filled hence FM is working fine
How to Call the FM in a Program
1) Go to SE38Create the program2) Place the cursor where ever to call the FM
3) Click on pattern & enter the FM name
Now pass the data to FM
Now process the data with loop........... Endloop.
REPORT ZREP_DEMO5.
DATA:BEGIN OF WA_T001, BUKRS LIKE T001-BUKRS, BUTXT LIKE T001-BUTXT, ORT01 LIKE T001-ORT01, END OF WA_T001, BEGIN OF WA_KNB1, KUNNR LIKE KNB1-KUNNR, BUKRS LIKE KNB1-BUKRS, ERDAT LIKE KNB1-ERDAT, END OF WA_KNB1, IT_KNB1 LIKE TABLE OF WA_KNB1.
PARAMETERS P_COCD LIKE WA_T001-BUKRS. CALL FUNCTION 'ZFM_DEMO' EXPORTING p_bukrs = P_COCD IMPORTING WA = WA_T001 TABLES i_tab = IT_KNB1* EXCEPTIONS* NO_DATA = 1* OTHERS = 2 .IF sy-subrc <> 0.* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.
write:/ 'Company CODE', WA_T001-BUKRS.WRITE:/ 'Company Name', WA_T001-BUTXT.WRITE:/ 'City', WA_T001-ORT01.ULINE.loop at it_knb1 INTO wa_knb1. write:/ wa_knb1-KUNNR, 15 WA_KNB1-BUKRS, 25 wa_knb1-erdat. clear wa_knb1. endloop.
Difference between Soubroutine & FM
Subroutine Function module
1) Exceptions cannot be handled.
2) Documentation cannot be given.
3) They are local to the program except external subroutines.
4) We cannot have the parameters as optional.
5) No sy-subrc is returned.
1) Exceptions can be handled.2) Documentation can be
given.3) They are global and are
stored centrally in the repository.
4) We can have parameters as optional.
5) Sy-subrc is returned