How to Create Function Module in ABAP

What is Function Module?

Function Modules are a set of codes that can be reused by other programs. 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.

Function Modules can be executed independently and they can be called from any program.

This is how Function Module looks like:

What does Function Module Consist Of?

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

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

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

Exception Parameters: are parameter which raise certain exceptions that are expected by our code.

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

How to Create Function Module Step-by-Step

We are going to create Function Module 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 calculation is stored in parameter EV_RESULT.

If the IV_OPERATION is not addition (+) nor subtraction (-), the function module 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.

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 the name starts with ZAS?

In SAP, 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.

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

Note: to learn more what is function group 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 “Save” button to proceed.

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

“Attributes” Tab

We choose “Regular Function Module” radio button.

“Import” Tab

In this tab we provide all the importing parameters for the Function Module.

Checkbox “Optional” means that 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 start with “IV” are importing parameters.

Define them as follows :

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

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

“Export” Tab

In this tab we provide all the exporting parameters for the Function Module. 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.

“Changing” Tab

In this tab we provide all the changing parameters for the Function Module. The calculation function module does not have any changing parameter.

Just to demonstrate 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.

“Exception” Tab

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

We create the following exception:

“Source Code” Tab

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

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

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. Reason for that is to visually show that this parameter is exporting and I don’t expect to have any value when the logic of starts.

Then I look for the operation in IV_OPERATION.

If IV_OPERATION value is addition (+), add IV_OPERAND_1 and IV_OPERAND_2.

If IV_OPERATION value is subtraction (-), subtract IV_OPERAND_1 by IV_OPERAND_2.

If 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.

Comments

Add 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