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:
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
We are going to create FM which calculates two operands:
The result of the calculation depends on the operation, which stores in parameter IV_OPERATION. There are 2 operations possible:
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.
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 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.
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.
Informational dialog displays with the following message. Click on the green button to proceed.
We choose “Regular Function Module” radio button.
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:
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.
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.
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.
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:
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 :
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…:
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.
This is it! Perfect.