SlideShare a Scribd company logo
1 of 55
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

What's hot

Sap abap interview questions
Sap abap interview questionsSap abap interview questions
Sap abap interview questionskssr99
 
Abap data dictionary
Abap data dictionaryAbap data dictionary
Abap data dictionarySmartGokul4
 
Oops abap fundamental
Oops abap fundamentalOops abap fundamental
Oops abap fundamentalbiswajit2015
 
Sap Abap Reports
Sap Abap ReportsSap Abap Reports
Sap Abap Reportsvbpc
 
Object oriented approach to ALV Lists in ABAP
Object oriented approach to ALV Lists in ABAPObject oriented approach to ALV Lists in ABAP
Object oriented approach to ALV Lists in ABAPNoman Mohamed Hanif
 
Introducing enhancement framework.doc
Introducing enhancement framework.docIntroducing enhancement framework.doc
Introducing enhancement framework.docKranthi Kumar
 
500+ SAP ABAP INTERVIEW QUESTIONS WITH ANSWERS
500+ SAP ABAP INTERVIEW QUESTIONS WITH ANSWERS500+ SAP ABAP INTERVIEW QUESTIONS WITH ANSWERS
500+ SAP ABAP INTERVIEW QUESTIONS WITH ANSWERSIICT Chromepet
 
Oo abap-sap-1206973306636228-5
Oo abap-sap-1206973306636228-5Oo abap-sap-1206973306636228-5
Oo abap-sap-1206973306636228-5prakash185645
 
Table maintenance generator and its modifications
Table maintenance generator and its modificationsTable maintenance generator and its modifications
Table maintenance generator and its modificationsscribid.download
 
ABAP Event-driven Programming &Selection Screen
ABAP Event-driven Programming &Selection ScreenABAP Event-driven Programming &Selection Screen
ABAP Event-driven Programming &Selection Screensapdocs. info
 
Call transaction method
Call transaction methodCall transaction method
Call transaction methodKranthi Kumar
 
ABAP for Beginners - www.sapdocs.info
ABAP for Beginners - www.sapdocs.infoABAP for Beginners - www.sapdocs.info
ABAP for Beginners - www.sapdocs.infosapdocs. info
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialscesarmendez78
 
Dialog Programming Overview
Dialog Programming OverviewDialog Programming Overview
Dialog Programming Overviewsapdocs. info
 

What's hot (20)

Sap abap interview questions
Sap abap interview questionsSap abap interview questions
Sap abap interview questions
 
Abap data dictionary
Abap data dictionaryAbap data dictionary
Abap data dictionary
 
Oops abap fundamental
Oops abap fundamentalOops abap fundamental
Oops abap fundamental
 
Sap Abap Reports
Sap Abap ReportsSap Abap Reports
Sap Abap Reports
 
Object oriented approach to ALV Lists in ABAP
Object oriented approach to ALV Lists in ABAPObject oriented approach to ALV Lists in ABAP
Object oriented approach to ALV Lists in ABAP
 
Sap abap material
Sap abap materialSap abap material
Sap abap material
 
Sap abap
Sap abapSap abap
Sap abap
 
Introducing enhancement framework.doc
Introducing enhancement framework.docIntroducing enhancement framework.doc
Introducing enhancement framework.doc
 
500+ SAP ABAP INTERVIEW QUESTIONS WITH ANSWERS
500+ SAP ABAP INTERVIEW QUESTIONS WITH ANSWERS500+ SAP ABAP INTERVIEW QUESTIONS WITH ANSWERS
500+ SAP ABAP INTERVIEW QUESTIONS WITH ANSWERS
 
Oo abap-sap-1206973306636228-5
Oo abap-sap-1206973306636228-5Oo abap-sap-1206973306636228-5
Oo abap-sap-1206973306636228-5
 
Table maintenance generator and its modifications
Table maintenance generator and its modificationsTable maintenance generator and its modifications
Table maintenance generator and its modifications
 
ABAP Event-driven Programming &Selection Screen
ABAP Event-driven Programming &Selection ScreenABAP Event-driven Programming &Selection Screen
ABAP Event-driven Programming &Selection Screen
 
Call transaction method
Call transaction methodCall transaction method
Call transaction method
 
ABAP for Beginners - www.sapdocs.info
ABAP for Beginners - www.sapdocs.infoABAP for Beginners - www.sapdocs.info
ABAP for Beginners - www.sapdocs.info
 
Abap dictionary 1
Abap dictionary 1Abap dictionary 1
Abap dictionary 1
 
SAP Adobe forms
SAP Adobe formsSAP Adobe forms
SAP Adobe forms
 
Bapi step-by-step
Bapi step-by-stepBapi step-by-step
Bapi step-by-step
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorials
 
07.Advanced Abap
07.Advanced Abap07.Advanced Abap
07.Advanced Abap
 
Dialog Programming Overview
Dialog Programming OverviewDialog Programming Overview
Dialog Programming Overview
 

Viewers also liked

Chapter 05 adding structures1
Chapter 05 adding structures1Chapter 05 adding structures1
Chapter 05 adding structures1Kranthi Kumar
 
Lecture02 abap on line
Lecture02 abap on lineLecture02 abap on line
Lecture02 abap on lineMilind Patil
 
Chapter 02 abap dictionary objects1
Chapter 02 abap dictionary objects1Chapter 02 abap dictionary objects1
Chapter 02 abap dictionary objects1Kranthi Kumar
 
Chapter 07 abap dictionary changes1
Chapter 07 abap dictionary changes1Chapter 07 abap dictionary changes1
Chapter 07 abap dictionary changes1Kranthi Kumar
 
Chapter 04 abap dictionary tables in relational databases1
Chapter 04 abap dictionary tables in relational databases1Chapter 04 abap dictionary tables in relational databases1
Chapter 04 abap dictionary tables in relational databases1Kranthi Kumar
 
Chapter 06 abap repository information system1
Chapter 06 abap  repository information system1Chapter 06 abap  repository information system1
Chapter 06 abap repository information system1Kranthi Kumar
 
Chapter 01 overview of abap dictionary1
Chapter 01 overview of abap dictionary1Chapter 01 overview of abap dictionary1
Chapter 01 overview of abap dictionary1Kranthi Kumar
 
Chapter 08 abap dictionary objects views1
Chapter 08 abap dictionary objects views1Chapter 08 abap dictionary objects views1
Chapter 08 abap dictionary objects views1Kranthi Kumar
 
Chapter 10 online help & documentation1
Chapter 10 online help & documentation1Chapter 10 online help & documentation1
Chapter 10 online help & documentation1Kranthi Kumar
 
0106 debugging
0106 debugging0106 debugging
0106 debuggingvkyecc1
 
0104 abap dictionary
0104 abap dictionary0104 abap dictionary
0104 abap dictionaryvkyecc1
 

Viewers also liked (20)

Bapi jco[1]
Bapi jco[1]Bapi jco[1]
Bapi jco[1]
 
07 sap scripts
07 sap scripts07 sap scripts
07 sap scripts
 
Corso ABAP OO 01
Corso ABAP OO   01Corso ABAP OO   01
Corso ABAP OO 01
 
Bapi programming
Bapi programmingBapi programming
Bapi programming
 
08 subprograms
08 subprograms08 subprograms
08 subprograms
 
Chapter 05 adding structures1
Chapter 05 adding structures1Chapter 05 adding structures1
Chapter 05 adding structures1
 
Lecture02 abap on line
Lecture02 abap on lineLecture02 abap on line
Lecture02 abap on line
 
cardinality1
cardinality1cardinality1
cardinality1
 
Ale Idoc
Ale IdocAle Idoc
Ale Idoc
 
Chapter 02 abap dictionary objects1
Chapter 02 abap dictionary objects1Chapter 02 abap dictionary objects1
Chapter 02 abap dictionary objects1
 
05 internal tables
05 internal tables05 internal tables
05 internal tables
 
Chapter 07 abap dictionary changes1
Chapter 07 abap dictionary changes1Chapter 07 abap dictionary changes1
Chapter 07 abap dictionary changes1
 
Chapter 04 abap dictionary tables in relational databases1
Chapter 04 abap dictionary tables in relational databases1Chapter 04 abap dictionary tables in relational databases1
Chapter 04 abap dictionary tables in relational databases1
 
Chapter 06 abap repository information system1
Chapter 06 abap  repository information system1Chapter 06 abap  repository information system1
Chapter 06 abap repository information system1
 
Chapter 01 overview of abap dictionary1
Chapter 01 overview of abap dictionary1Chapter 01 overview of abap dictionary1
Chapter 01 overview of abap dictionary1
 
data modelling1
 data modelling1 data modelling1
data modelling1
 
Chapter 08 abap dictionary objects views1
Chapter 08 abap dictionary objects views1Chapter 08 abap dictionary objects views1
Chapter 08 abap dictionary objects views1
 
Chapter 10 online help & documentation1
Chapter 10 online help & documentation1Chapter 10 online help & documentation1
Chapter 10 online help & documentation1
 
0106 debugging
0106 debugging0106 debugging
0106 debugging
 
0104 abap dictionary
0104 abap dictionary0104 abap dictionary
0104 abap dictionary
 

Similar to Abap function module help

Modularization & Catch Statement
Modularization & Catch StatementModularization & Catch Statement
Modularization & Catch Statementsapdocs. info
 
Lecture11 abap on line
Lecture11 abap on lineLecture11 abap on line
Lecture11 abap on lineMilind Patil
 
Complete reference to_abap_basics
Complete reference to_abap_basicsComplete reference to_abap_basics
Complete reference to_abap_basicsAbhishek Dixit
 
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpuDhaval Jalalpara
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxKhurramKhan173
 
Subroutines rev01 fa16
Subroutines rev01 fa16Subroutines rev01 fa16
Subroutines rev01 fa16John Todora
 
ERP Magazine April 2018 Issue 1
ERP Magazine April 2018 Issue 1 ERP Magazine April 2018 Issue 1
ERP Magazine April 2018 Issue 1 Rehan Zaidi
 
ERP Magazine April 2018 - The magazine for SAP ABAP Professionals
ERP Magazine April 2018 - The magazine for SAP ABAP ProfessionalsERP Magazine April 2018 - The magazine for SAP ABAP Professionals
ERP Magazine April 2018 - The magazine for SAP ABAP ProfessionalsRehan Zaidi
 
3 modularisation and bdc
3 modularisation and bdc3 modularisation and bdc
3 modularisation and bdcSiva Kumar
 
modularization-160202092213 (1).pdf
modularization-160202092213 (1).pdfmodularization-160202092213 (1).pdf
modularization-160202092213 (1).pdfSreeramBaddila
 
Modularisation techniques new
Modularisation techniques newModularisation techniques new
Modularisation techniques newJeet Thombare
 
Functions part1
Functions part1Functions part1
Functions part1yndaravind
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2yndaravind
 

Similar to Abap function module help (20)

Modularization & Catch Statement
Modularization & Catch StatementModularization & Catch Statement
Modularization & Catch Statement
 
Lecture11 abap on line
Lecture11 abap on lineLecture11 abap on line
Lecture11 abap on line
 
Functions
FunctionsFunctions
Functions
 
Complete reference to_abap_basics
Complete reference to_abap_basicsComplete reference to_abap_basics
Complete reference to_abap_basics
 
Call
CallCall
Call
 
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Subroutines rev01 fa16
Subroutines rev01 fa16Subroutines rev01 fa16
Subroutines rev01 fa16
 
ERP Magazine April 2018 Issue 1
ERP Magazine April 2018 Issue 1 ERP Magazine April 2018 Issue 1
ERP Magazine April 2018 Issue 1
 
ERP Magazine April 2018 - The magazine for SAP ABAP Professionals
ERP Magazine April 2018 - The magazine for SAP ABAP ProfessionalsERP Magazine April 2018 - The magazine for SAP ABAP Professionals
ERP Magazine April 2018 - The magazine for SAP ABAP Professionals
 
3 modularisation and bdc
3 modularisation and bdc3 modularisation and bdc
3 modularisation and bdc
 
modularization-160202092213 (1).pdf
modularization-160202092213 (1).pdfmodularization-160202092213 (1).pdf
modularization-160202092213 (1).pdf
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
User defined functions.1
User defined functions.1User defined functions.1
User defined functions.1
 
Function in C
Function in CFunction in C
Function in C
 
Modularisation techniques new
Modularisation techniques newModularisation techniques new
Modularisation techniques new
 
Functions part1
Functions part1Functions part1
Functions part1
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
 
Functions
FunctionsFunctions
Functions
 

More from Kranthi Kumar

Creating simple comp
Creating simple compCreating simple comp
Creating simple compKranthi Kumar
 
Controllers and context programming
Controllers and context programmingControllers and context programming
Controllers and context programmingKranthi Kumar
 
Binding,context mapping,navigation exercise
Binding,context mapping,navigation exerciseBinding,context mapping,navigation exercise
Binding,context mapping,navigation exerciseKranthi Kumar
 
Web(abap introduction)
Web(abap introduction)Web(abap introduction)
Web(abap introduction)Kranthi Kumar
 
Chapter 07 debugging sap scripts
Chapter 07 debugging sap scriptsChapter 07 debugging sap scripts
Chapter 07 debugging sap scriptsKranthi Kumar
 
Chapter 06 printing sap script forms
Chapter 06 printing sap script formsChapter 06 printing sap script forms
Chapter 06 printing sap script formsKranthi Kumar
 
Chapter 05 sap script - configuration
Chapter 05 sap script - configurationChapter 05 sap script - configuration
Chapter 05 sap script - configurationKranthi Kumar
 
Chapter 04 sap script - output program
Chapter 04 sap script - output programChapter 04 sap script - output program
Chapter 04 sap script - output programKranthi Kumar
 
Chapter 02 sap script forms
Chapter 02 sap script formsChapter 02 sap script forms
Chapter 02 sap script formsKranthi Kumar
 

More from Kranthi Kumar (20)

Exercise in alv
Exercise in alvExercise in alv
Exercise in alv
 
Dynamic binding
Dynamic bindingDynamic binding
Dynamic binding
 
Data binding
Data bindingData binding
Data binding
 
Creating simple comp
Creating simple compCreating simple comp
Creating simple comp
 
Creating messages
Creating messagesCreating messages
Creating messages
 
Creating a comp
Creating a compCreating a comp
Creating a comp
 
Controllers and context programming
Controllers and context programmingControllers and context programming
Controllers and context programming
 
Context at design
Context at designContext at design
Context at design
 
Binding,context mapping,navigation exercise
Binding,context mapping,navigation exerciseBinding,context mapping,navigation exercise
Binding,context mapping,navigation exercise
 
Alv for web
Alv for webAlv for web
Alv for web
 
Web(abap introduction)
Web(abap introduction)Web(abap introduction)
Web(abap introduction)
 
Abap faq
Abap faqAbap faq
Abap faq
 
Crm technical
Crm technicalCrm technical
Crm technical
 
control techniques
control techniquescontrol techniques
control techniques
 
Chapter 07 debugging sap scripts
Chapter 07 debugging sap scriptsChapter 07 debugging sap scripts
Chapter 07 debugging sap scripts
 
Chapter 06 printing sap script forms
Chapter 06 printing sap script formsChapter 06 printing sap script forms
Chapter 06 printing sap script forms
 
Chapter 05 sap script - configuration
Chapter 05 sap script - configurationChapter 05 sap script - configuration
Chapter 05 sap script - configuration
 
Chapter 04 sap script - output program
Chapter 04 sap script - output programChapter 04 sap script - output program
Chapter 04 sap script - output program
 
Chapter 02 sap script forms
Chapter 02 sap script formsChapter 02 sap script forms
Chapter 02 sap script forms
 
sap script overview
sap script overviewsap script overview
sap script overview
 

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.