How to create a function module?

Function Modules or simply known as FMs are procedures that are created and defined in Function Builder or via transaction code SE37. They are contained in a special ABAP programs known as function groups.

In this tutorial, I will discuss how to create your first function module and how to integrate it to your ABAP program.

STEPS:
1. Go to SE37, function builder screen. Type the name of your function module. As always, it must begin with a Z* or Y*. Then click the Create button.


2. On this next screen, enter name of the function group and a short text for your function module. Be descriptive enough. If you don’t have any function group, click this link to create your own.  Hit the save button afterwards.


3. So in this screenshot below, you can see different tabs such as Attributes, Import, Export Changing, Tables, Exceptions and Source code. Along the way, you will learn how to use each tabs. But in this tutorial I created, to make it simplify, I use only three tabs which are the most necessary tabs. Because what I want to share is the principle in creating your first function module.


4. The first tab to fill up with parameters is the Import tab. In my sample, I added one input parameter and I called it input_param with a type of integer. Don’t be confused that it is limited to only one parameter. You can add as many as you want. But since this is a simple tutorial as I said, I make it simple as possible.


By the way, don’t be misled also when after creating your FM then you will call it into your ABAP program, the IMPORT title might be confusing (it is also true to Export). Take a steady look with this screenshot below and to the screenshot above and notice the difference.



Did you spot the difference? In the first screenshot, the tab title is IMPORT while the parameter is input_param. But on the second screenshot, the title now is EXPORTING and the parameter is still input_param. As I said don’t be confused with these titles. Take note that your main ABAP program is different to your Function module. They are two different programs. So if we add FMs to our ABAP program, all input parameters are exported from your main ABAP program and imported into your function module for further processing.

5. After you fill up your import tab, go to Export tab as this is the returning value. So when this FM is called in an ABAP program, the parameter indicated in this tab will be the returning value. In my sample, I added one export parameter and I called it output_param with a type of string. Also, you can add as many as you want. But since this is a simple tutorial as I said, I make it simple as possible.


6. The last tab I used in this tutorial is the Source code tab. As the tab title implies, you will write here your ABAP codes. So in my sample below, I write a sample code which determines if an input value is a positive number or not. I will not elaborate further on how to create logic here. Its up to you how to do or implement your program logic in this source code tab.


7. After you are done with your source code, click save and activate buttons on the toolbar. If errors persists, check your source code. It must be activated before it can be used in your main ABAP program.

8. So in this next step, I will show how you will call your newly created FM into your ABAP program. In my sample below, I created a new program and I called it as zfirst_fxprog. Its up to you how you will call your program too. Inside this program, I added one parameter which will serves as my input parameter. Below it, I call the FM, ZMYFIRST_FUNCTION. Then I will display the returning value using the WRITE keyword. By the way, a simple way to add your FM is by clicking the button PATTERN in the toolbar. Just enter the name of your function module and it will be added automatically into your program.


9. Save and activate your program. Try to run and test if its working as expected. It must display this first screen. Try to input a positive number.


10. The result would now be like this one below, showing the message you entered in your function module.

I hope I make your day another day with smiles. :) Enjoy reading my posts.

No comments:

Post a Comment

How to download the data structure of tables in SE11?

Good morning people around the world. Its me again doing some blogs. Sharing to you some tips and tricks in the SAP world. Well, before I s...