Function modules make up a major part of a SAP system, because for years SAP has modularized code using function modules, allowing for code reuse, by themselves, their developers and also by their customers.
Function modules are sub-programs that contain a set of reusable statements with importing and exporting parameters. Unlike Include programs, function modules can be executed independently. SAP system contains several predefined function modules that can be called from any ABAP program. The function group acts as a kind of container for a number of function modules that would logically belong together. For instance, the function modules for an HR payroll system would be put together into a function group.
To look at how to create function modules, the function builder must be explored. You can find the function builder with transaction code SE37. Just type a part of a function module name with a wild card character to demonstrate the way function modules can be searched for. Type *amount* and then press the F4 key.
Steps to create Function Modules –
- Create Function Group. Use TCode – SE80. Function Groups is an entity which acts like a container to the function modules. We can have any number of function modules in a function group.
- Create Function Module. User TCode – SE37. Entities in Function modules.
- Import – here we pass the input data.
- Export – holds output data.
- Changing – this behaves both as import and export parameters.
- Tables – this holds the internal tables. Behaves both as import and export.
- Exception – this is the way to handle errors inside the function module.
Source code – this is the place where we write the logic.
Create Function Group:-
Go to TCode – SE80. Select Function Group from the drop down and provide a function Group name. then press ENTER. Check below image.
Click ‘YES’ in the popup. Now you will get another popup. There provide the Short Text and click on ‘SAVE’. See image below.
Now provide the Package or put it under Local object($TMP) and click on ‘SAVE’.
Now right click on the function group and activate it.
Create Function Module.
Go to TCode – SE37. Give the FM name and click on ‘Create’.
In the popup – give the Function Group name and the short description for the FM. Then click on ‘SAVE’.
Now under tab – IMPORT, give 2 values which will be input to this FM.
Under tab – EXPORT, provide the variable name which will hold the output of this FM.
Under tab – EXCEPTION, provide the exception name. this will be handled in the source code.
Under tab – ‘SOURCE CODE’, provide the logic to do the calculation.
Line 13 – 14: check if the value ‘in_value_2’ is initial then raise an exception – Division by zero not possible.
Line 16: Actual logic of the division which will return the quotient in the exporting parameter – ‘out_result’.
Save and activate the FM. And we are done creating a function module which will take 2 values as input and return the quotient. If the denominator value is 0 then it will raise an exception.
Check out another post for an example of a detailed FM.