SlideShare a Scribd company logo
Lecture 11 Subroutines & Function Modules BCO5647 Applications Programming Techniques (ABAP)
Readings & Objectives Readings Keller & Keller   Chapter 5 Section 5.1 & 5.2    Objectives This lecture will Discuss modularisation techniques and how these could be implemented in ABAP Introduce the concept of internal subroutines and how these are implemented in ABAP Examine how parameters are passed between the main program and subroutines Distinguish calling parameters by value and by reference Introduce the concept of function modules and their characteristics Demonstrate how function modules can be searched for in the repository Demonstrate how the features of a function module can be displayed Demonstrate how a function module can be called Demonstrate how errors in function modules can be captured and handled.
Modularisation Techniques Modularisation enables us to reduce the complexity of large programs and to re-use code. Modularisation can be implemented using: Internal subroutines The subroutine and the call are in the same program. External subroutines The subroutine is in an external program. Function modules The subroutine is stored in the Function Library.
Internal Subroutines (“Forms”) Internal subroutines are declared using the  FORM  and  ENDFORM  keywords. All subroutines should be placed at the end of the program. Subroutines are called using the  PERFORM  statement. Nested and recursive subroutine calls are permitted. Example : perform  calculate_total. …… form  calculate_total. …. statements endform .
Global and Local Data Unless declared separately in an internal subroutine, data is global to the whole program. Data can be defined as being local to the subroutine by using the  data  keyword :   data flag value 1 type i. write flag. perform write_flag. write flag.   …….. form write_flag. data flag type i. flag = 2. write flag. endform.
Passing Parameters Data can be passed from the calling program to a subroutine using parameters. Parameters may be described as: Read only - the parameter is read in the subroutine but is not changed. Changing - the parameter is read and changed by the subroutine. In general, the parameter passed must be of the same type as the parameter received by the subroutine. Data structures (eg internal tables) can also be passed as parameters.
Passing Parameters - Example report ptest. data: val1 type p, val2 type p,   val3 type p, val4 type p,   answer  type p, operator type c. val1 = 4.  val2 = 2.  operator = ‘+’. val3 = 8.  val4 = 5. perform calc_answer  using val1 val2 operator   changing answer. operator = ‘-’. perform calc_answer  using val3 val4 operator changing answer. ……… ... form calc_answer using fv1 fv2 op    changing ans.   case op.   when ‘+’.   ans = fv1 + fv2.   when ‘-’.   ans = fv1 - fv2.   endcase. endform.
Calling by Value and by Reference Parameters can be called either by reference or by value. Calling by reference : The address of the actual parameter is called. Changes have an immediate effect. If only the formal parameter name is specified in the subroutine interface, then the parameter is called by reference . Calling by value : When the subroutine is called, the actual parameter value is copied to the formal parameter. There are two types of call by value.
External Subroutines A program can make an external call to a subroutine in another program. Example : report main. data:  name(20). …. perform print_name(process) using name. …. report process. …. form print_name using name1. …. endform.
Function  M odules A  function module   is a  separate program  that can be called from your ABAP code to perform a specific task.   SAP comes with a  library  of pre-written function modules. Function modules are pooled together into  function groups . E.g. calendar functions  Function modules have  a  clearly defined  fixed interface for data exchange.  The interface supports exception handling.  Function modules  use their own memory area.
Searching for a Function Module Go to the Function Builder: SAP Standard Menu >> Development >> Function Builder. Transaction Code SE37. Searching can be done using the  Repository Information System  or SAP’s  Application hierarchy . Click Matchcode Button
Displaying a Function Module You can display information about existing function modules: Attributes:  specifies  administrative  information like the person responsible for the module  and  a short description of the module. Import :  contains a list of the formal parameters that are used to pass data to a function module.  Export :  contains a list of the formal parameters that are used to receive data from a function module.
Displaying a Function Module You can display information about existing function modules: Changing :  contains a list of the formal parameters that are used both to pass data to and receive data from a function module.  Tables :  specifies the tables that are to be passed to a function module. Table parameters are always passed by reference.  Exceptions : s hows how the function module reacts to exceptions.  Source code :  program code of the function module . Documentation:  provides information about the interface and exceptions .
Calling a Function Module Function modules are called by the ABAP code using a  CALL  statement . To insert function into ABAP code click  [Pattern]  button on toolbar.
Calling a Function Module
Calling a Function Module

More Related Content

PDF
Passing table to subroutine
PPTX
SAP Modularization techniques
PPTX
Modularisation techniques new
DOCX
Sap abap modularization interview questions
PPTX
LeverX SAP ABAP Tutorial Creating Function Modules
PPTX
Subprogramms
PPTX
Function
PPTX
Fda unit 1 lec 1
Passing table to subroutine
SAP Modularization techniques
Modularisation techniques new
Sap abap modularization interview questions
LeverX SAP ABAP Tutorial Creating Function Modules
Subprogramms
Function
Fda unit 1 lec 1

What's hot (20)

PPT
User Defined Functions in C
PPTX
Sap scripts
PPT
DAC training-batch -2020(PLSQL)
PPT
user defined function
PPTX
PDF
Smartforms interview questions with answers
PPTX
structure of a c program
PPTX
Abap package concept
PPTX
Functions in C - Programming
PDF
PDF
Sap Abap Reports
PPTX
INTRODUCTION TO C PROGRAMMING - PART 2
PPT
Abap course chapter 7 abap objects and bsp
PPTX
Built in function
DOC
Programming style guideline very good
PPT
Ms vb
PPTX
Procedure n functions
PDF
PPTX
Chap 2 structure of c programming dti2143
User Defined Functions in C
Sap scripts
DAC training-batch -2020(PLSQL)
user defined function
Smartforms interview questions with answers
structure of a c program
Abap package concept
Functions in C - Programming
Sap Abap Reports
INTRODUCTION TO C PROGRAMMING - PART 2
Abap course chapter 7 abap objects and bsp
Built in function
Programming style guideline very good
Ms vb
Procedure n functions
Chap 2 structure of c programming dti2143
Ad

Similar to Lecture11 abap on line (20)

PDF
Functions-Computer programming
PPT
Chapter 5 modularization & catch statement (paradiso-a45b1d's conflicted co...
PPT
Abap function module help
PPTX
FUNCTIONengineeringtechnologyslidesh.pptx
PPTX
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
PDF
Functions part1
PDF
Fnctions part2
PPTX
FUNCTION CPU
PDF
PSPC-UNIT-4.pdf
PDF
Ch07.pdf
PPTX
Lecture_5_-_Functions_in_C_Detailed.pptx
PPTX
Chapter One Function.pptx
PDF
User defined functions in matlab
DOCX
Functions assignment
PPT
AVB201.1 MS Access VBA Module 1
PPTX
Functions and modular programming.pptx
PDF
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
PDF
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
PDF
cp Module4(1)
Functions-Computer programming
Chapter 5 modularization & catch statement (paradiso-a45b1d's conflicted co...
Abap function module help
FUNCTIONengineeringtechnologyslidesh.pptx
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
Functions part1
Fnctions part2
FUNCTION CPU
PSPC-UNIT-4.pdf
Ch07.pdf
Lecture_5_-_Functions_in_C_Detailed.pptx
Chapter One Function.pptx
User defined functions in matlab
Functions assignment
AVB201.1 MS Access VBA Module 1
Functions and modular programming.pptx
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
cp Module4(1)
Ad

More from Milind Patil (20)

PDF
Abap slide class4 unicode-plusfiles
PDF
Step by step abap_input help or lov
PDF
Step bystep abap_fieldhelpordocumentation
PDF
Step bystep abap_field help or documentation
PDF
Abap slides user defined data types and data
PDF
Abap slides set1
PPT
Abap slide class3
PDF
Abap slide lock Enqueue data clusters auth checks
PDF
Step bystep abap_changinga_singlerecord
PDF
Abap slide lockenqueuedataclustersauthchecks
PDF
Abap slide exceptionshandling
PDF
Step bystep abap_changinga_singlerecord
PDF
Abap reports
PPT
Lecture16 abap on line
PPT
Lecture14 abap on line
PPT
Lecture13 abap on line
PPT
Lecture12 abap on line
PPT
Lecture10 abap on line
PPT
Lecture09 abap on line
PPT
Lecture08 abap on line
Abap slide class4 unicode-plusfiles
Step by step abap_input help or lov
Step bystep abap_fieldhelpordocumentation
Step bystep abap_field help or documentation
Abap slides user defined data types and data
Abap slides set1
Abap slide class3
Abap slide lock Enqueue data clusters auth checks
Step bystep abap_changinga_singlerecord
Abap slide lockenqueuedataclustersauthchecks
Abap slide exceptionshandling
Step bystep abap_changinga_singlerecord
Abap reports
Lecture16 abap on line
Lecture14 abap on line
Lecture13 abap on line
Lecture12 abap on line
Lecture10 abap on line
Lecture09 abap on line
Lecture08 abap on line

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Encapsulation theory and applications.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPT
Teaching material agriculture food technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Understanding_Digital_Forensics_Presentation.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25 Week I
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Machine learning based COVID-19 study performance prediction
The Rise and Fall of 3GPP – Time for a Sabbatical?
Encapsulation theory and applications.pdf
Programs and apps: productivity, graphics, security and other tools
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Teaching material agriculture food technology
Reach Out and Touch Someone: Haptics and Empathic Computing
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Per capita expenditure prediction using model stacking based on satellite ima...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
“AI and Expert System Decision Support & Business Intelligence Systems”
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars
Chapter 3 Spatial Domain Image Processing.pdf
sap open course for s4hana steps from ECC to s4
Understanding_Digital_Forensics_Presentation.pptx

Lecture11 abap on line

  • 1. Lecture 11 Subroutines & Function Modules BCO5647 Applications Programming Techniques (ABAP)
  • 2. Readings & Objectives Readings Keller & Keller Chapter 5 Section 5.1 & 5.2 Objectives This lecture will Discuss modularisation techniques and how these could be implemented in ABAP Introduce the concept of internal subroutines and how these are implemented in ABAP Examine how parameters are passed between the main program and subroutines Distinguish calling parameters by value and by reference Introduce the concept of function modules and their characteristics Demonstrate how function modules can be searched for in the repository Demonstrate how the features of a function module can be displayed Demonstrate how a function module can be called Demonstrate how errors in function modules can be captured and handled.
  • 3. Modularisation Techniques Modularisation enables us to reduce the complexity of large programs and to re-use code. Modularisation can be implemented using: Internal subroutines The subroutine and the call are in the same program. External subroutines The subroutine is in an external program. Function modules The subroutine is stored in the Function Library.
  • 4. Internal Subroutines (“Forms”) Internal subroutines are declared using the FORM and ENDFORM keywords. All subroutines should be placed at the end of the program. Subroutines are called using the PERFORM statement. Nested and recursive subroutine calls are permitted. Example : perform calculate_total. …… form calculate_total. …. statements endform .
  • 5. Global and Local Data Unless declared separately in an internal subroutine, data is global to the whole program. Data can be defined as being local to the subroutine by using the data keyword : data flag value 1 type i. write flag. perform write_flag. write flag. …….. form write_flag. data flag type i. flag = 2. write flag. endform.
  • 6. Passing Parameters Data can be passed from the calling program to a subroutine using parameters. Parameters may be described as: Read only - the parameter is read in the subroutine but is not changed. Changing - the parameter is read and changed by the subroutine. In general, the parameter passed must be of the same type as the parameter received by the subroutine. Data structures (eg internal tables) can also be passed as parameters.
  • 7. Passing Parameters - Example report ptest. data: val1 type p, val2 type p, val3 type p, val4 type p, answer type p, operator type c. val1 = 4. val2 = 2. operator = ‘+’. val3 = 8. val4 = 5. perform calc_answer using val1 val2 operator changing answer. operator = ‘-’. perform calc_answer using val3 val4 operator changing answer. ……… ... form calc_answer using fv1 fv2 op changing ans. case op. when ‘+’. ans = fv1 + fv2. when ‘-’. ans = fv1 - fv2. endcase. endform.
  • 8. Calling by Value and by Reference Parameters can be called either by reference or by value. Calling by reference : The address of the actual parameter is called. Changes have an immediate effect. If only the formal parameter name is specified in the subroutine interface, then the parameter is called by reference . Calling by value : When the subroutine is called, the actual parameter value is copied to the formal parameter. There are two types of call by value.
  • 9. External Subroutines A program can make an external call to a subroutine in another program. Example : report main. data: name(20). …. perform print_name(process) using name. …. report process. …. form print_name using name1. …. endform.
  • 10. Function M odules A function module is a separate program that can be called from your ABAP code to perform a specific task. SAP comes with a library of pre-written function modules. Function modules are pooled together into function groups . E.g. calendar functions Function modules have a clearly defined fixed interface for data exchange. The interface supports exception handling. Function modules use their own memory area.
  • 11. Searching for a Function Module Go to the Function Builder: SAP Standard Menu >> Development >> Function Builder. Transaction Code SE37. Searching can be done using the Repository Information System or SAP’s Application hierarchy . Click Matchcode Button
  • 12. Displaying a Function Module You can display information about existing function modules: Attributes: specifies administrative information like the person responsible for the module and a short description of the module. Import : contains a list of the formal parameters that are used to pass data to a function module. Export : contains a list of the formal parameters that are used to receive data from a function module.
  • 13. Displaying a Function Module You can display information about existing function modules: Changing : contains a list of the formal parameters that are used both to pass data to and receive data from a function module. Tables : specifies the tables that are to be passed to a function module. Table parameters are always passed by reference. Exceptions : s hows how the function module reacts to exceptions. Source code : program code of the function module . Documentation: provides information about the interface and exceptions .
  • 14. Calling a Function Module Function modules are called by the ABAP code using a CALL statement . To insert function into ABAP code click [Pattern] button on toolbar.

Editor's Notes

  • #4: Advantages : Because the external subroutines and function modules are self-contained, they can be debugged independently of each other. Changes only have to be made in the one program - all other programs that call that subroutine or function module will be affected by the change. The overall complexity of the program is reduced.
  • #5: When the perform command is executed, all statements between the form and endform are processed. Data defined in the ‘main’ program is global and can be used and changed in the subroutine. Double-Click on the subroutine name in the perform statement to create the subroutine at the bottom of the program code with documentation .
  • #6: The output from the above program would be 1 2 1. Where possible, local data should be used in a subroutine. The contents of a local data object can be retained from one call of the subroutine to the next by using static variables. Example : perform count. perform count. ….. form count. statics calls type i. calls = calls + 1. write calls. endform. … will produce the output 1 2.
  • #7: This is the preferred method for passing data to a subroutine. Internal tables are always global.
  • #8: The result from the first call : answer = 6. The result from the second call : answer = 3. The parameter names in the PERFORM statement are called ACTUAL PARAMETERS. The parameter names in the FORM statement are called FORMAL PARAMETERS. The above example passes parameters by reference. The next slide explains this.
  • #9: Parameters can be called either by reference or by value. Calling by reference: The address of the actual parameter is called. Within the subroutine, the variable is addressed using the formal parameter name and changes have an immediate. If only the formal parameter name is specified in the subroutine interface, then the parameter is called by reference. Calling by value: When the subroutine is called the actual parameter value is copied to the formal parameter. There are two types of call by value: Calling by value: the formal parameter is listed in the interface after the USING clause with the addition VALUE( <parameter name> ) . When the subroutine is called, the actual parameter is copied to the formal parameter. Changes made to the formal parameter only affect the local copy, not the actual parameter. Calling by value and result: the formal parameter is listed in the interface after the CHANGING clause with the addition VALUE( <parameter name> ) . When the subroutine is called, the actual parameter is copied to the formal parameter. Changes made to the formal parameter initially only affect the local copy. When the ENDFORM statement is reached, the formal parameter value is copied back to the actual parameter.
  • #10: All rules re passing of data as for internal subroutines, except that using parameters is the only way of passing data. (Global data in the calling program may not be referenced in the called subroutine.) SAP do not recommend using External Subroutines.
  • #11: Examples of Function Modules : Calculates whether a date falls on a Sunday. Rounding a number with decimals to 2 places. Function modules possess a clearly defined fixed interface for data exchange. This makes it easier for you to pass input and output parameters to and from the function module. The function module interface supports exception handling. This allows you to catch errors and pass them back to the calling program for handling. Function modules use their own memory area. The calling program and the function module cannot exchange data using a shared memory area - they must use the function module interface. This avoids unpleasant side effects such as accidentally overwriting data. How many function modules are supplied with SAP? In release 3.1, more than 43,000 function modules are available. In rel 4.5 more than 80,000 function modules are available. (4.6B > 98,000; Enterprise > 170,000) The are many thousands of function groups which contain function modules.
  • #12: Hint: Add Function Builder to Favorites Information system allows you to search for function modules using search criteria of function name, short description and/or function group. SAP Applications allows you to list function modules associated with application components such as FI, SD, MM.
  • #14: NOTES: Changing parameters appears to be new in ver 4.0 or greater Documentation can be found in the Goto pull-down menu.
  • #15: {Pattern} button will display Insert Statement dialog. Enter or search for required function and click Continue to insert function into ABAP Editor. Ctrl-F6 shortcut to produce Insert Statement dialog.
  • #16: Once the function module template is inserted, it is up to the programmer to fill it in. Note the use of ‘X’ for export SIGN variable. This comes from the function’s documentation where the code “X” indicates commercial rounding. The use of the MESSAGE Statement with system message codes allows the function to handle error processing and produce appropriate error messages. IF SY-SUBRC <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. You can choose to handle errors yourself and define your own error messages by replacing the above code with something like: CASE SY-SUBRC. WHEN 0. write: inputnum, ' commercially rounded = ', rounded. WHEN 1. write 'Input Invalid'. WHEN 2. write 'Overflow Error'. WHEN 3. write 'Output Field type Invalid'. WHEN 4. write 'Unknown Error'. ENDCASE.