Ads

Function Module in SAP ABAP

Function Module in SAP ABAP

In any software language,reuse ability is most important ,also in SAP ABAP ,we are using the function modules reuse ability purpose .We can modularize coding using function modules in SAP.Function modules defined with importing and exporting input and output parameters with set of statements.Function modules can be executed independently unlike include programs.SAP system contains predefined function modules which are logically belong to cretian function groups.Function groups contains set of function modules which can be called from any ABAP program.

Function Module Use



Function modules are code blocks that can be used in ABAP programs for reuse ability purpose.Every function module defined within the function groups and can be called from all ABAP programs. Function groups contains the collection of function modules which logically belong together. You can create function groups and function modules in the ABAP Workbench tool which is Function Builder.

Once you release the function modules from the function builder(SE37). You can't make any incompatible changes for any function modules that have already been released.while making changes to function modules which already released and used in the programs still works continuously. 

You do not define function modules in the source code of your program ,unlike the subroutines.we will define and write source code function module within the Function Builder. The actual ABAP source code of FM(Function module) remains hidden from the programmer. You can create the input parameters of a function module as optional. You can also assign default values to input paramaters. Function modules supports the exception handling which helps you to catch certain errors while the function module is running. You can test function modules in the Function Builder.


We are using Function modules to encapsulate and reuse global functions in the SAP System. They will be stored in a central function library. The SAP System contains several predefined functions modules that can be called from any ABAP program. 

We can create function modules in the function builder(SE37) in the SAP system.Which are mainly used for global level function reusable purposes.We are using function modules for updating data and connecting between different SAP system and between SAP systems and to communicate with remote systems .

We can not define function modules within the ABAP source code program ,unlike the subroutines ,instead we are defining them in Function Builder.

 Steps for Creating Function Group  

Go to SE37 Transaction Code from SAP Easy Access Screen.
Function Module in SAP ABAP
Now Select  Go to Menu ,select Function Group and Select Create Function Group Option.

Function Module in SAP ABAP
Now ,enter the Function Group Name and Short Text and Click on Save.

Function Module in SAP ABAP
  OR

Go to SE80 (Repositary Browser)transaction code and 
Choose Goto → Function groups → Create group.
Specify the function group name and a short text.
Select Save.
We can define names of function groups up to a maximum length of 26 alphanumeric characters. Here ,we are using standard naming conventions for the first character (A-X for SAP development).
When we create a new function group, the SAP system automatically creates a main program containing two includes. Like other programs and includes in the ABAP Work Bench, we can display them in the Repository Browser.

The system will assign the name of the main program. This is made up of the prefix SAPL followed by the function group name. For example, the main program for function group AYYY is called SAPLAYYY.
The names of the include files begin with L followed by the name of the function group, and conclude with UXX ( or TOP for the TOP include). The TOP include contains global data declarations which are used by all of the function modules within the function group. Second include in the function group contains the all function modules related to it.

Function groups contains collection of function modules. Function groups can't be executed. When you call a function module, the system restores the whole function group into the internal session of the calling program .



Function group name can be up to 26 characters long. When you create a function group or function module in the Function Builder , the main program and include programs are generated automatically.

Steps for Creating Function Module

We are going to create a function module ZREAD_SPFLI_INTO_TABLE to read data for a specified airline from table SPFLI into an internal table, which it then passes back to the calling program.
Before creating any custom function module,we must be create function group which is mandatory.

Go to SE37 Transaction Code from SAP Easy Access Screen.
Function Module in SAP ABAP
Click on create and Give the already created function Group name and you can see it in the function module attributes tab.

Function Module in SAP ABAP
Now define the Import Parameters.

Function Module in SAP ABAP
Now define the export Parameters.

Function Module in SAP ABAP
Within the source code button ,write the source code of function module.

Function Module in SAP ABAP
Now ,click on save,check and activate button.

Function Module in SAP ABAP
Now give the some input values and click on execute button.

Function Module in SAP ABAP
Now ,you can see the output.

Function Module in SAP ABAP
To call function module from ABAP Program ,go to SE38 Tcode .

Function Module in SAP ABAP
Create the ABAP Program.

Function Module in SAP ABAP
Within the source code of ABAP Program place cursor,click on pattern button ,now pop up window will be opening in which give the created function module name and click on continue.


Function Module in SAP ABAP
You can see the called function module in the below image.

Function Module in SAP ABAP

We will discuss more about ,how to write source code in the function Builder(SE37)

Parameter Interface

We  can declare the types of interface parameters in function modules like the parameter interfaces of subroutines. Since function modules can be used anywhere in the system, their interfaces can only contain references to data types that are declared system-wide. The data types can be the elementary ABAP data types, the system-wide generic types, like any table, and types defined in the ABAP Dictionary. We cannot use LIKE to refer to data types declared in the framework program.

FUNCTION read_spfli_into_table.
*"------------------------------------------------------------
*"*"Local Interface:
*"       IMPORTING
*"             VALUE(ID) LIKE  SPFLI-CARRID DEFAULT 'LH '
*"       EXPORTING
*"             VALUE(ITAB) TYPE  SPFLI_TAB
*"       EXCEPTIONS
*"              NOT_FOUND
*"------------------------------------------------------------
  
SELECT * FROM spfli INTO TABLE itab WHERE carrid = id.
  IF sy-subrc NE 0.
    MESSAGE e007(at) RAISING not_found.
  ENDIF.
ENDFUNCTION.

The function module ZREAD_SPFLI_INTO_TABLE need an import parameter to restrict the selection to a single airline. 

Under Reference field or structure, we can enter a column of a database table, a component of a ABAP Dictionary structure, or a whole ABAP Dictionary structure. The import parameter ID is optional, and has a default value.

Reference type can contain any generic or full data type that is recognized system-wide. 

To pass data back to the calling program, the function module needs an export parameter with the type of an internal table. 


The internal table is passed by value. Only internal tables that are passed using tables parameters can be passed exclusively by reference.

Exceptions in Function Module

Function module needs an exception which can trigger if there are no entries in table SPFLI that meet the selection criterion. The exception NOT_FOUND serves this function:

The source code of function module in the function builder after creating it in the function builder.




Now ,we can call the function modules which are created in the Function Builder(SE37) through the ABAP Programming editor(SE38).

Example ABAP program for function Module.

REPORT zdemo_functionmodule.

PARAMETERS carrier TYPE s_carr_id.

DATA: jtab TYPE spfli_tab,
      wa   LIKE LINE OF jtab.

CALL FUNCTION 'ZREAD_SPFLI_INTO_TABLE'
     EXPORTING
          id        = carrier
     IMPORTING
          itab      = jtab
     EXCEPTIONS
          not_found = 1
          OTHERS    = 2.
CASE sy-subrc.
  WHEN 1.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno.
  WHEN 2.
    MESSAGE e702(at).
ENDCASE.
LOOP AT jtab INTO wa.
  WRITE: /  wa-carrid, wa-connid, wa-cityfrom, wa-cityto.
ENDLOOP.



Function Module,SAP ABAP,Function Module in SAP ABAP,Creating Function Module,Function Module Tutorial,Function Group.

Comments

Popular posts from this blog

BADI Interview Questions in SAP ABAP

Sample SAP ABAP Programming Examples for Practice

Module Pool Programming Interview Questions and Answers in SAP ABAP

Step by Step tutorial on BDC Session Method Program in SAP ABAP

SAP ABAP Interview Questions and Answers for 10 Years Experienced