What is & How to Create Function Module in ABAP?

What is Function Module?

Notice: In this article, I will be using the abbreviation “FM” for Function Module or “FMs” for Function Modules.

FMs are a set of codes that can be reused by other programs. In other words, it means that instead of copying exactly the same statements within programs, it is much more useful and maintainable to create a function module. Function Modules are created with importing, exporting, changing parameters, and exceptions.

FMs can be executed independently and they can be called from any program.

This is what Function Module looks like:

Main screen with Function Module definition and ready for implementation

What Does Function Module Consist Of?

Import Parameters: are inputs of the FM sent from the main program.

Export Parameters: are outputs of the FM. The parameters are sent from the main program empty, and they are sent back to the main program with the output value.

Changing Parameters: are input parameters that at the end of the process, the values are changed and they become output parameters of the FM.

Exception Parameters: these are parameters that raise certain exceptions that are expected by our code.

Source Code: here is the place for the implementation of actual logic

How to Create Function Module Step-by-Step

We are going to create FM which calculates two operands:

  • IV_OPERAND_1 and
  • IV_OPERAND_2.

The result of the calculation depends on the operation, which stores in parameter IV_OPERATION. There are 2 operations possible:

  • addition (+) or
  • subtraction (-).

The result of the calculation is stored in parameter EV_RESULT.

If the IV_OPERATION is not addition (+) nor subtraction (-), the FM raises an exception called OPERATION_NOT_SUPPORTED.

Steps to Create Function Module:

From SAP easy access, open transaction SE37 to open Function Builder.

Notice: /OSE37, /O means open new session/window for transaction SE37.

Transaction SE37 to open Function Module Builder

Next “Function Builder” window shows up.

Enter the new “Function Module” name. In my case I will name it: ZAS_LIB_HPL_CALCULATE_DEMO.

Why does the name start with ZAS?

In SAP, the Function Module name starts with Z or Y, and AS are my initials. This naming convention is very important for readability and maintainability.

…and click on create button to proceed.

How to create Function Module?

Enter the “Function group”. The function group must be defined before the creation of the function module.

Note: to learn more about what is function group is and how to create it, click here How To Create Function Group.

Enter the “Short text” to describe the function module, e.g. “ABAP Academy Demo Calculate FM”

… and click the “Save” button to proceed.

Popup for defining and creating Function Module

Informational dialog displays with the following message. Click on the green button to proceed.

Function module name is reserved for SAP

“Attributes” Tab

We choose “Regular Function Module” radio button.

“Attributes” Tab of Function Module

“Import” Tab

In this tab, we provide all the importing parameters for the FM.

Checkbox “Optional” means that the current parameter is optional and does not need to be filled.

There are 3 importing parameters for this calculate function module, they are:

  • IV_OPERAND_1
  • IV_OPERATION
  • IV_OPERAND_2

Importing parameter starts with “IV”, which stands for “Importing Variable”. This naming convention is important for readability and maintainability. By doing this, we know that all parameters starting with “IV” are importing parameters.

Define them as follows :

The type of IV_OPERAND_1 and IV_OPERAND_2 are Integer as their content number value.

The type of IV_OPERATION is a single character, as it will contain ‘+’ or ‘-’ signs.

Import Tab of Function Module

“Export” Tab

In this tab, we provide all the exporting parameters for the FM. We only have one exporting parameter for this calculation function module, which is EV_RESULT. The type is Integer as it holds the result of the calculation.

Exporting parameter name starts with EV, which stands for Exporting Variable.

Export tab of Function Module

“Changing” Tab

In this tab, we provide all the changing parameters for the FM. The calculation function module does not have any changing parameters.

Just to demonstrate to you how to create one changing parameter, I’ve created e.g. CV_DEMO. Changing parameter starts with CV, which stands for Changing Variable.

Notice: Just make sure that you delete this parameter because we are not going to use it in this example.

Changing Tab of Function Module

“Exception” Tab

In this tab, we create an exception(s) to catch certain types of errors. As our calculation function module only will use addition (+) and subtraction (-), we need to create an error exception when the operation that is sent by the main program is not supported.

We create the following exception:

Exception Tab of Function Module

“Source Code” Tab

This is the actual ABAP Editor where we implement the logic for the FM.

ABAP provides pre-generated comments containing all the parameters: Import, Export, Changing parameters, and Exceptions as follows :

Source Code Tab of Function Module

Code implementation

This code calculates two operands: IV_OPERAND_1 and IV_OPERAND_2.

The operation value in IV_OPERATION can be addition (+) or subtraction (-).

The result is stored in EV_RESULT.

First, I clear exporting parameter EV_RESULT. The reason for that is to visually show that this parameter is exporting and I don’t expect to have any value when the logic starts.

Then I look for the operation in IV_OPERATION. If…:

  • IV_OPERATION value is addition (+), add IV_OPERAND_1 and IV_OPERAND_2
  • IV_OPERATION value is subtraction (-), subtract IV_OPERAND_1 by IV_OPERAND_2
  • IV_OPERATION value is not addition (+), nor subtraction (-), raise an exception

Check the consistency (CTRL+F2) and activate the function (CTRL+F3).

Function module ZAS_LIB_HPL_CALCULATE_DEMO is created.

To test the function module, press (F8) to execute, and the screen below shows up:

Enter the values of IV_OPERAND_1, IV_OPERATION, and IV_OPERAND_2 to perform the calculation and click on Execute (F8).

EV_RESULT is filled in by the result of the calculation as shown below:

If the IV_OPERATION entered is multiplication (*), it raises an exception because the operation is not supported.

For more information about Function Modules, you can look at the Official SAP Documentation here.

Comments
  1. of course says:

    This is it! Perfect.

Add a comment

Your email address will not be published. Required fields are marked *

 

Your personal information will be exclusively used for processing current comment only. Personal data protection policy