SlideShare a Scribd company logo
ABAP Chapter 5
 Modularization
 Catch Statement
Modularization
Modularization
 Internal Subroutine
Call
 External Subroutine
Call
 Function Module
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
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.
Call by Value
a1
Memory Space(Subroutine)
f1
Copy
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.
Call by Reference
a3
Memory Space(Subroutine)
f3
Address Passing
Call by ReferenceCall by Reference
Data: a3.
a3 = ‘A’.
PERFORM routine2 USING a3.
.…...
FORM routine2 USING f3.
f3 = ‘X’.
ENDFORM.
Call by Value and Result
a4
Memory Space(Subroutine)
f4
CopyCopy
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.
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.
Passing Internal Table as ParametersPassing Internal Table as Parameters
TA: tab LIKE sflight OCCURS 0 WITH HEADER L
FORM sub TABLES tab.
Passing Internal Table as ParametersPassing Internal Table as Parameters
FORM sub TABLES tab1 STRUCTURE tab.
LOOP AT tab1.
WRITE: / tab1-carrid.
ENDLOOP.
ENDFORM.
External SubroutinesExternal Subroutines
REPORT RSAAA10F.
TABLES: sflight.
…..
PERFORM cal(RSAAA10B).
REPORT RSAAA10B.
TABLES sflight.
…..
FORM cal.
…..
ENDFORM.
EXIT StatementEXIT Statement
DATA tmp TYPE I.
tmp = 4.
PERFORM a.
WRITE tmp.
FORM a.
EXIT.
tmp = 99.
ENDFORM.
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.
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
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
Function Group : SE37
Function Group : SE80
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
Function Module : SE37
Function Module
Function Module : Source CodeFunction Module : Source Code
FUNCTION Z_FMTEST.
result = number1 ** number2.
ENDFUNCTION.
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.
Exercise : Function Module
?
ABAP Program
Function Module
EXCEPTIONS
Function ModuleFunction Module
Function Z_CAL01.
if number1 > 9 and number2 > 9.
raise invalidnumber.
else.
result = number1 ** number2.
endif.
ENDFUNCTION.
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.
Exercise : Exceptions
?
ABAP Program
Function Module
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
Optional
ABAP Program
Function Module
Structure in Function Module
Example : Structure
Example : Structure
Internal Table in Function Module
Example : Internal Table
Example : Internal Table
Function Group
Function Group : ZGRP00
Function Module : Z_FMTEST
Function Module : Z_CAL01
Function Group
Function Module in Function
Group
Exercise
 Display current month name
using function module
Catch Statement
•Syntax
Catch system-exceptions <error type> = <n>.
<ABAP statement – generate runtime error> .
Endcatch.
if sy-subrc = <n>.
...
endif.
CATCH StatementCATCH Statement
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
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.
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.
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.
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.
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.
ABAP Practice

More Related Content

PPT
Module pool programming
PPT
Alv theory
PDF
Sap Abap Reports
PDF
SAP ABAP data dictionary
PPTX
Reports
PDF
Abap reports
PDF
Ooabap notes with_programs
PPT
ABAP Open SQL & Internal Table
Module pool programming
Alv theory
Sap Abap Reports
SAP ABAP data dictionary
Reports
Abap reports
Ooabap notes with_programs
ABAP Open SQL & Internal Table

What's hot (20)

PDF
500+ SAP ABAP INTERVIEW QUESTIONS WITH ANSWERS
PPTX
CDS Views.pptx
PDF
Bapi step-by-step
DOC
1000 solved questions
PPTX
Abap data dictionary
PPT
Ab1011 module pool programming
PPT
0104 abap dictionary
PDF
Sap abap tutorials
PPT
abap list viewer (alv)
PPTX
Bdc BATCH DATA COMMUNICATION
PPTX
Object oriented approach to ALV Lists in ABAP
PPT
BATCH DATA COMMUNICATION
DOCX
Field symbols
PPTX
Sap abap
PPT
08.Abap Dialog Programming Overview
PPT
ABAP Object oriented concepts
PPT
SAP ABAP - Needed Notes
PPTX
SAP Adobe forms
PPT
List Processing in ABAP
PPTX
SAP Modularization techniques
500+ SAP ABAP INTERVIEW QUESTIONS WITH ANSWERS
CDS Views.pptx
Bapi step-by-step
1000 solved questions
Abap data dictionary
Ab1011 module pool programming
0104 abap dictionary
Sap abap tutorials
abap list viewer (alv)
Bdc BATCH DATA COMMUNICATION
Object oriented approach to ALV Lists in ABAP
BATCH DATA COMMUNICATION
Field symbols
Sap abap
08.Abap Dialog Programming Overview
ABAP Object oriented concepts
SAP ABAP - Needed Notes
SAP Adobe forms
List Processing in ABAP
SAP Modularization techniques
Ad

Viewers also liked (20)

PDF
Bapi jco[1]
PPT
07 sap scripts
PPT
Corso ABAP OO 01
DOC
Bapi programming
PDF
08 subprograms
PPT
Chapter 05 adding structures1
PPT
Lecture02 abap on line
PPT
cardinality1
PPT
Ale Idoc
PPT
Chapter 02 abap dictionary objects1
PDF
05 internal tables
PPT
Chapter 07 abap dictionary changes1
PPT
Chapter 04 abap dictionary tables in relational databases1
PPT
Chapter 06 abap repository information system1
PPT
Chapter 01 overview of abap dictionary1
PPT
data modelling1
PPT
Chapter 08 abap dictionary objects views1
PPT
Chapter 10 online help & documentation1
PPT
0106 debugging
PDF
Sujith ~ cross applications
Bapi jco[1]
07 sap scripts
Corso ABAP OO 01
Bapi programming
08 subprograms
Chapter 05 adding structures1
Lecture02 abap on line
cardinality1
Ale Idoc
Chapter 02 abap dictionary objects1
05 internal tables
Chapter 07 abap dictionary changes1
Chapter 04 abap dictionary tables in relational databases1
Chapter 06 abap repository information system1
Chapter 01 overview of abap dictionary1
data modelling1
Chapter 08 abap dictionary objects views1
Chapter 10 online help & documentation1
0106 debugging
Sujith ~ cross applications
Ad

Similar to Abap function module help (20)

PPT
Modularization & Catch Statement
PPT
Lecture11 abap on line
PPTX
PDF
Complete reference to_abap_basics
PPT
PDF
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
PPT
Basic information of function in cpu
PPTX
Lecture 1_Functions in C.pptx
PPTX
Subroutines rev01 fa16
PDF
ERP Magazine April 2018 - The magazine for SAP ABAP Professionals
PDF
ERP Magazine April 2018 Issue 1
PPT
3 modularisation and bdc
PDF
modularization-160202092213 (1).pdf
PDF
PSPC-UNIT-4.pdf
PPTX
User defined functions.1
PDF
Function in C
PPTX
Modularisation techniques new
PDF
Functions part1
PDF
Fnctions part2
PPT
COBOL- Day 091255554433355664332222777.ppt
Modularization & Catch Statement
Lecture11 abap on line
Complete reference to_abap_basics
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
Basic information of function in cpu
Lecture 1_Functions in C.pptx
Subroutines rev01 fa16
ERP Magazine April 2018 - The magazine for SAP ABAP Professionals
ERP Magazine April 2018 Issue 1
3 modularisation and bdc
modularization-160202092213 (1).pdf
PSPC-UNIT-4.pdf
User defined functions.1
Function in C
Modularisation techniques new
Functions part1
Fnctions part2
COBOL- Day 091255554433355664332222777.ppt

More from Kranthi Kumar (20)

PDF
Exercise in alv
PDF
Dynamic binding
PDF
Data binding
PDF
Creating simple comp
PDF
Creating messages
PDF
Creating a comp
PDF
Controllers and context programming
PDF
Context at design
PDF
Binding,context mapping,navigation exercise
PDF
Alv for web
PDF
Web(abap introduction)
DOC
Abap faq
PDF
Sap abap material
PDF
Crm technical
PDF
control techniques
PPT
Chapter 07 debugging sap scripts
PPT
Chapter 06 printing sap script forms
PPT
Chapter 05 sap script - configuration
PPT
Chapter 04 sap script - output program
PPT
Chapter 02 sap script forms
Exercise in alv
Dynamic binding
Data binding
Creating simple comp
Creating messages
Creating a comp
Controllers and context programming
Context at design
Binding,context mapping,navigation exercise
Alv for web
Web(abap introduction)
Abap faq
Sap abap material
Crm technical
control techniques
Chapter 07 debugging sap scripts
Chapter 06 printing sap script forms
Chapter 05 sap script - configuration
Chapter 04 sap script - output program
Chapter 02 sap script forms

Abap function module help

  • 1. ABAP Chapter 5  Modularization  Catch Statement
  • 3. Modularization  Internal Subroutine Call  External Subroutine Call  Function Module
  • 4. 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. …
  • 5. Modularization  Avoid redundancy  Make your program easy to read & improve their structure  Re-use Program components
  • 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.
  • 7. Call by Value a1 Memory Space(Subroutine) f1 Copy
  • 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.
  • 9. Call by Reference a3 Memory Space(Subroutine) f3 Address Passing
  • 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.
  • 16. External SubroutinesExternal Subroutines REPORT RSAAA10F. TABLES: sflight. ….. PERFORM cal(RSAAA10B). REPORT RSAAA10B. TABLES sflight. ….. FORM cal. ….. ENDFORM.
  • 17. EXIT StatementEXIT Statement DATA tmp TYPE I. tmp = 4. PERFORM a. WRITE tmp. FORM a. EXIT. tmp = 99. 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.
  • 30. Exercise : Function Module ? ABAP Program Function Module
  • 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.
  • 34. Exercise : Exceptions ? ABAP Program Function Module
  • 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
  • 40. Internal Table in Function Module
  • 43. Function Group Function Group : ZGRP00 Function Module : Z_FMTEST Function Module : Z_CAL01
  • 45. Function Module in Function Group
  • 46. Exercise  Display current month name using function module
  • 48. •Syntax Catch system-exceptions <error type> = <n>. <ABAP statement – generate runtime error> . Endcatch. if sy-subrc = <n>. ... endif. CATCH StatementCATCH Statement
  • 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.