6. Calling and Defining SubroutinesCalling and Defining Subroutines
REPORT ztest.
* Global Data
TABLES customers.
DATA tmp type i.
* Subroutine Calls
PERFORM routine1.
PERFORM routine2.
* Subroutine
FORM routine1.
DATA tmp1 type p. “Local data
write tmp.
ENDFORM.
FORM routine2.
DATA tmp2(10). “Local data
…..
ENDFORM.
8. Call by ValueCall 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.
10. Call by ReferenceCall by Reference
Data: a3.
a3 = ‘A’.
PERFORM routine2 USING a3.
.…...
FORM routine2 USING f3.
f3 = ‘X’.
ENDFORM.
11. Call by Value and Result
a4
Memory Space(Subroutine)
f4
CopyCopy
12. Call by Value and ResultCall by Value and Result
a4,a5.
A’.
A’.
RM routine3 USING a4 a5.
routine3 CHANGING VALUE(f4) f5. “f5 : call by ref
‘X’.
‘X’.
ORM.
13. Passing Structure as ParametersPassing Structure as Parameters
TABLES sflight.
SELECT * FROM sflight.
PERFORM subproc USING sflight.
ENDSELECT.
FORM subproc USING rec LIKE sflight.
WRITE: / rec-carrid.
ENDFORM.
14. Passing Internal Table as ParametersPassing Internal Table as Parameters
TA: tab LIKE sflight OCCURS 0 WITH HEADER L
FORM sub TABLES tab.
15. Passing Internal Table as ParametersPassing Internal Table as Parameters
FORM sub TABLES tab1 STRUCTURE tab.
LOOP AT tab1.
WRITE: / tab1-carrid.
ENDLOOP.
ENDFORM.
18. STOP StatementSTOP 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.
20. Function Module
Function Group
Function Library
- Admin
- Import/Export Parameter
- Source Code
- Main Program
- Documentation
21. 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
22. 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
25. 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
28. Function Module : Source CodeFunction Module : Source Code
FUNCTION Z_FMTEST.
result = number1 ** number2.
ENDFUNCTION.
29. Program Example IProgram 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.
32. Function ModuleFunction Module
Function Z_CAL01.
if number1 > 9 and number2 > 9.
raise invalidnumber.
else.
result = number1 ** number2.
endif.
ENDFUNCTION.
33. Example II : ExceptionsExample 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.
35. EXCEPTIONS VS AT SELECTION-SCREEN
NCTION Z_CAL01.
number1 > 9 and number2 > 9.
raise invalidnumber.
lse.
result = number1 ** number2.
ndif.
DFUNCTION.
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 <
endif.
START-OF-SELECTION.
CALL FUNCTION ‘Z_CAL01’.
…..
VS
49. Error class
•Catch system-exceptions conversion_errors
ingle error
•Catch system-exceptions convt_no_number
All catchable runtime error
•Catch system-exceptions others = 1.
CATCH Error TypeCATCH Error Type
50. CATCH StatementCATCH Statement
eport ztest.
Data num type I.
atch system-exceptions conversion_errors = 1.”oth
Move ‘abc’ to num. “runtime error: convt_no_numbe
ndcatch.
f sy-subrc = 1.
Write: / ‘Assign wrong data type to variable: num’
ndif.
51. CATCH StatementCATCH 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.
52. CATCH StatementCATCH 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.
53. CATCH StatementCATCH 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.
54. CATCH in Function Module
Function Z_CAL.
if number1 > 9 and number2 > 9.
raise invalidnumber.
else.
result = number1 ** number2.
endif.
ENDFUNCTION.
Function Z_CAL.
CATCH SYSTEM-EXCEPTIONS OTHERS = 1.
RESULT = NUMBER1 ** NUMBER2.
ENDCATCH.
IF SY-SUBRC = 1.
RAISE invalidnumber.
ENDIF.
ENDFUNCTION.