Friday 1 July 2016

Function Modules



  • Function modules are special external subroutines stored in a central library. 
  • Can be created users own function modules. 
  • The main difference between a function module and a normal ABAP/4 subroutine is a clearly defined interface for passing data.
  • The calling program and the called function module have separate work areas for ABAP/4 Dictionary tables.
  • Several function modules can be combined to a function group in the program library with the attribute type F with definition of global data that can be accessed from all function modules of one function group. 


Interfaces: 

  • Import parameter:
  1. Pass data from the calling program to the function module. Cannot be overwritten, even when passed by reference.
  • Export parameters:
  1. Pass data from the function module back to the calling program. Always optional. 
  • CHANGING parameter:


  1. Passed by reference or by value and result.
  2.  Act simultaneously as import and export parameters. 
  3. They change the value passed to the function module and return it to the calling program.
  • Table parameters:



  1. Internal tables.
  2.  Treated like changing parameters and are always passed by reference. 


  •  Exception:




  1.  Used to handle error scenarios which can occur in function modules.
  • Proposal - default value for optional parameters.
  • Reference field|structure, Reference type - used for typing of formal parameters.
  • Calling Function Modules:



           CALL FUNCTION <module_name>
                     [EXPORTING <f1> = <a1> ... <fn> = <an>]
                     [IMPORTING <f1> = <a1> ... <fn> = <an>]
                     [CHANGING <f1> = <a1> ... <fn> = <an>]
                    [TABLES <f1> = <a1> ... <fn> = <an>]
                     [EXCEPTIONS <e1> = <r1> ... <en> = <rn> [ERROR_MESSAGE = <rE>]              [OTHER = <rO>]]. 

  • EXPORTING - the formal parameters must be declared as import parameters in the function module.
  • IMPORTING - the formal parameters must be declared as export parameters in the function module.
  • EXCEPTIONS - <ri> is a numeric literal that is assigned to SY-SUBRC if the exception is raised. <rE> is used for additional exception ERROR_MESSAGE if a message of types A, E was issued in the function module without RAISING option.
  • OTHERS - used to handle all exceptions that are not specified explicitly in the list. The same number <ri> can be used for several exceptions.
  • Programming Function Modules:


            FUNCTION <module_name>
                  ....
             RAISE <exception>.
                 ....
              MESSAGE ... RAISING <exception>.
                  ....
           ENDFUNCTION. 


  •  If an internal subroutine is needed, the program code of internal subroutines can be placed directly behind the ENDFUNCTION statement of a module.
  •  This subroutines can be called from all modules of the function group, but for clarity, should be called only from the function module behind which it is written.
  • To create internal subroutines that can be called from all modules of one function group <fgrp>, use special INCLUDE programs L<fgrp>F<xx>
  • We can open these INCLUDE programs easily by double clicking their names in the main program SAPL<fgrp> after selecting Main program on the Function Library->Maintain Function Modules screen
                              more information..............




 

No comments:

Post a Comment