0
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,211

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,211
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
145
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • 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.
  • 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 .
  • 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.
  • This is the preferred method for passing data to a subroutine. Internal tables are always global.
  • 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.
  • 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( &lt;parameter name&gt; ) . 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( &lt;parameter name&gt; ) . 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.
  • 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.
  • 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 &gt; 98,000; Enterprise &gt; 170,000) The are many thousands of function groups which contain function modules.
  • 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.
  • NOTES: Changing parameters appears to be new in ver 4.0 or greater Documentation can be found in the Goto pull-down menu.
  • {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.
  • 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 &lt;&gt; 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, &apos; commercially rounded = &apos;, rounded. WHEN 1. write &apos;Input Invalid&apos;. WHEN 2. write &apos;Overflow Error&apos;. WHEN 3. write &apos;Output Field type Invalid&apos;. WHEN 4. write &apos;Unknown Error&apos;. ENDCASE.
  • Transcript of "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
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×