Académique Documents
Professionnel Documents
Culture Documents
Modularization
Modularization
Subroutine
START-OF-SELECTION. Perfrom routine1. Perform routine2. Perform routine2. Form routine1. select * from customers into table tab. Endform. Form routine2. loop at tab. write: / tab-id,tab-name. endloop. Endform.
Modularization
Avoid redundancy Make your program easy to read & improve their structure Re-use Program components
Call by Value
a1 Copy f1
Memory Space(Subroutine)
Call by Value
Data: a1,a2. a1 = A. a2 = A. PERFORM routine1 USING a1 a2. .... FORM routine1 USING VALUE(f1) VALUE(f2). f1 = X. f2 = X. ENDFORM.
Call by Reference
a3 Address Passing f3
Memory Space(Subroutine)
Call by Reference
Data: a3. a3 = A. PERFORM routine2 USING a3. .... FORM routine2 USING f3. f3 = X. ENDFORM.
Memory Space(Subroutine)
External Subroutines
REPORT RSAAA10F. TABLES: sflight. .. PERFORM cal(RSAAA10B).
REPORT RSAAA10B. TABLES sflight. .. FORM cal. .. ENDFORM.
EXIT Statement
DATA tmp TYPE I. tmp = 4. PERFORM a. WRITE tmp.
FORM a. EXIT. tmp = 99. ENDFORM.
STOP Statement
DATA tmp TYPE I. START-OF-SELECTION. tmp = 4. PERFORM a. WRITE tmp. END-OF-SELECTION. tmp = 0. write tmp. FORM a. STOP. go to END-OF-SELECTION tmp = 99. ENDFORM.
Function Module
Function Module
Function Group Function Library - Admin - Import/Export Parameter - Source Code - Main Program - Documentation
Function Group
When you create a function module, you must assign it to function group The function group is the main program in which a function module is embedded The function group is a program type F,and not executable The entire function group is loaded in a program the first time that you call a function module that belongs to it
Function Group
is a container for function modules When a function module is called,the entire function group is loaded into the session of the program Function group is used to define global data for function modules A DATA statement in the global memory of a function group is shared by all the function modules that belong to that function group
Function Module
is a code that can be called from any ABAP program,therefore making it a globally accessible object ABAP program pass data to function module from import parameters or internal tables Function module receives data from a program,process the information in its own code, and then sends back information in the export parameters or internal tables
Function Module
Program Example I
REPORT ztest. PARAMETERS: no1 TYPE I, no2 TYPE I. DATA result TYPE I. START-OF-SELECTION. CALL FUNCTION Z_FMTEST EXPORTING number1 = no1 number2 = no2 IMPORTING result = result.
write: / result.
EXCEPTIONS
Function Module
Function Z_CAL01. if number1 > 9 and number2 > 9. raise invalidnumber. else. result = number1 ** number2. endif. ENDFUNCTION.
Example II : Exceptions
REPORT ztest. PARAMETERS: no1 TYPE I, no2 TYPE I. DATA result TYPE I. START-OF-SELECTION. CALL FUNCTION Z_CAL01 EXPORTING number1 = no1 number2 = no2 IMPORTING result = result EXCEPTIONS invalidnumber = 1. IF sy-subrc <> 0. write: / Please enter number < 10. ELSE. write: / result. ENDIF.
Exercise : Exceptions
ABAP Program
Function Module
EXCEPTIONS VS AT SELECTION-SCREEN
FUNCTION Z_CAL01. if number1 > 9 and number2 > 9. raise invalidnumber. else. result = number1 ** number2. endif. ENDFUNCTION. REPORT ztest. Parameters: no1 type i, no2 type i. At selection-screen if no1 > 9 and no2 > 9. message e000(38) with Please enter no < 10. endif. START-OF-SELECTION. CALL FUNCTION Z_CAL01. ..
VS
Optional
Function Module
ABAP Program
Example : Structure
Example : Structure
Function Group
Function Module : Z_FMTEST
Function Group
Exercise
Catch Statement
CATCH Statement
Syntax Catch system-exceptions <error type> = <n>. <ABAP statement generate runtime error> . Endcatch. if sy-subrc = <n>. ... endif.
CATCH Statement
Report ztest. Data num type I. Catch system-exceptions conversion_errors = 1.others Move abc to num. runtime error: convt_no_number Endcatch. If sy-subrc = 1. Write: / Assign wrong data type to variable: num. Endif.
CATCH Statement
Report ztest. Data num type I. Catch system-exceptions others = 1. Move abc to num. Endcatch. If sy-subrc = 1. Write: / Assign wrong data type to variable: num. Endif.
CATCH Statement
Report ztest. PARAMETERS: NUM1 TYPE I, NUM2 TYPE I. DATA RESULT TYPE I. START-OF-SELECTION. CATCH SYSTEM-EXCEPTIONS COMPUTE_INT_ZERODIVIDE = 1. RESULT = NUM1 / NUM2. ENDCATCH. IF SY-SUBRC = 1. WRITE: /'Divide by zero'. ELSE. WRITE: / RESULT. ENDIF.
CATCH Statement
Report ztest. PARAMETERS: NUM1 TYPE I, NUM2 TYPE I. DATA RESULT TYPE I. START-OF-SELECTION. CATCH SYSTEM-EXCEPTIONS OTHERS = 1. RESULT = NUM1 / NUM2. ENDCATCH. IF SY-SUBRC = 1. WRITE: /'Divide by zero'. ELSE. WRITE: / RESULT. ENDIF.
ABAP Practice