Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Lecture 11 Subroutines & Function Modules BCO5647 Applications Programming Techniques (ABAP)
Readings & Objectives <ul><li>Readings </li></ul><ul><li>Keller & Keller   Chapter 5 Section 5.1 & 5.2    </li></ul><ul><l...
Modularisation Techniques <ul><ul><li>Modularisation enables us to reduce the complexity of large programs and to re-use c...
Internal Subroutines (“Forms”) <ul><li>Internal subroutines are declared using the  FORM  and  ENDFORM  keywords. </li></u...
Global and Local Data <ul><li>Unless declared separately in an internal subroutine, data is global to the whole program. <...
Passing Parameters <ul><li>Data can be passed from the calling program to a subroutine using parameters. </li></ul><ul><li...
Passing Parameters - Example <ul><li>report ptest. data: val1 type p, val2 type p,   val3 type p, val4 type p,   answer  t...
Calling by Value and by Reference <ul><ul><li>Parameters can be called either by reference or by value. </li></ul></ul><ul...
External Subroutines <ul><li>A program can make an external call to a subroutine in another program. </li></ul><ul><li>Exa...
Function  M odules <ul><li>A  function module   is a  separate program  that can be called from your ABAP code to perform ...
Searching for a Function Module <ul><ul><li>Go to the Function Builder: </li></ul></ul><ul><ul><ul><li>SAP Standard Menu >...
Displaying a Function Module <ul><ul><li>You can display information about existing function modules: </li></ul></ul><ul><...
Displaying a Function Module <ul><ul><li>You can display information about existing function modules: </li></ul></ul><ul><...
Calling a Function Module <ul><ul><li>Function modules are called by the ABAP code using a  CALL  statement . </li></ul></...
Calling a Function Module
Calling a Function Module
Upcoming SlideShare
Loading in …5
×

Lecture11 abap on line

1,950 views

Published on

Published in: Technology
  • Be the first to comment

Lecture11 abap on line

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

×