What is Function Module?

Function modules are sub programs that contains set of reusable source code statements with importing, exporting parameters and exceptions. Function modules are stored in central library.

Function modules available to the entire system. Function modules can execute independently. If the source code is only used within the same program, then use a Subroutine. Otherwise a function module is preferred.

SAP R/3 system contains wide range of predefined function modules that can be called from any ABAP program.

Every function module is a part of function group. The function group acts as a container for function modules that would logically belong together. Function modules plays significant role in updating the databases.

Function modules plays a key role in remote function calls (RFC) in between SAP R/3 Systems or between an SAP R/3 System and a non SAP system. Function modules support exception handling to handle any errors when the function module is running.

Components of Function Module -

  • Import - Input parameters of a Function Module.
  • Export - Output parameters of a Function Module.
  • Changing - Specifies the parameters act as importing and exporting parameters to a Function Module.
  • Tables - Specifies internal tables acts as importing and exporting parameters.
  • Exceptions - Specifies exceptions in Function Modules.

Function Module Creation Process -

The function module creation process involves the below steps -

  • Create Function Group. If exist, use function group.
  • Create Function Module
  • Execute Function Module

Let us discuss function module creation process in ABAP programming with a simple example. In the example, we are triggering the exception when the input2 is ZERO in the function module.

Create Function Group -

To create the function module, the function group should already exist first. If function group not exist, create new function group.

Step-1: Go to SE37 transaction. From the menu, select Goto Function Groups Create Group.

Function Group Creation Process

Step-2: Enter the function group name, Short text and click on "Save"

Function Group Creation Process

Step-3: Enter the package details and click on Local Object icon to continue. The status bar displays the "Function group ZTC_FG created" message if it successfully created.

Create Function Module -

Step-1: Go to SE37 to open Function builder: initial Screen. Enter the new function module name and click on Create icon to proceed.

Function Module Creation Process

Step-2: Enter the function group, short text and click on "Save" icon.

Function Module Creation Process

Below informational dialog displays with following message. Click on "Continue" icon to proceed.

Function Module Creation Process

Step-3: Enter the input Parameters Name and their Associated Type in import tab.

Function Module Creation Process

Step-4: Go to Export tab, enter output Parameters Name and their Associated Type.

Function Module Creation Process

Step-5: Go to Exceptions tab, enter the Exceptions name and Short Text like shown below –

Function Module Creation Process

Step-6: Go to Source code tab, add Source Code in the gap in between FUNCTION and ENDFUNCTION shown below.

Function Module Creation Process

Step-7: Save and activate the function module. Once the function module executed, it displays the below screen.

Function Module Creation Process

Execute Function Module -

Case-1: Enter the valid input1 and input2 to perform the division operation and click on Execute (F8) to get the output.

Function Module Execution Process

The output gets displayed like below -

Function Module Execution Process

Case-2: Enter the valid input1, input2 as ZERO and click on Execute (F8) to trigger the exception.

Function Module Execution Process

The exception gets displayed like below -

Function Module Execution Process