Function Modules in ABAP – How to Create and Call Reusable Code
Table of Contents
- • What is a Function Module?
- • Function Module vs Subroutine — When to Use What?
- • Understanding the Interface
- ↳ 1. IMPORTING Parameters (Input)
- ↳ 2. EXPORTING Parameters (Output)
- ↳ 3. CHANGING Parameters (Input + Output)
- ↳ 4. TABLES Parameters (Internal Tables)
- ↳ Plus: EXCEPTIONS
- • How to Create a Function Module in SE37
- ↳ Step 1: Open Transaction SE37
- ↳ Step 2: Create a Function Group First
- ↳ Step 3: Create the Function Module
- ↳ Step 4: Define the Interface
- ↳ Step 5: Write the Source Code
- ↳ Step 6: Activate
- • How to Call a Function Module
- ↳ Output:
- ↳ Understanding the CALL FUNCTION Syntax
- • Testing a Function Module in SE37
- • Calling SAP Standard Function Modules
- ↳ Example 1: Get User Details
- ↳ Example 2: Convert Date to External Format
- ↳ Output:
- ↳ Example 3: Send Email Using Function Module
- • How to Find Standard Function Modules
- ↳ Method 1: Search in SE37
- ↳ Method 2: Search in SE84
- ↳ Method 3: Google It
- • Exception Handling in Detail
- ↳ Output:
- • RFC-Enabled Function Modules
- • Common Mistakes to Avoid
- ↳ 1. Forgetting to Handle Exceptions
- ↳ 2. Confusing IMPORTING and EXPORTING Perspectives
- ↳ 3. Not Activating the Function Group
- ↳ 4. Using TABLES Instead of CHANGING for Internal Tables
- • Interactive Checkpoints
- • Summary
![]()
In the previous tutorial, we learned about subroutines using FORM and PERFORM. Subroutines are great for organizing code within a single program, but they have a big limitation — you cannot reuse them across different programs. If you write a tax calculation subroutine in Program A, Program B cannot call it.
Function Modules solve this problem. A Function Module is a reusable block of code that is stored centrally in the SAP system. Once you create a Function Module, any ABAP program in the entire system can call it. You write the logic once, and it is available everywhere.
In real SAP projects, Function Modules are used constantly. SAP itself provides thousands of standard Function Modules for everything — reading material data, posting financial documents, sending emails, converting currencies, and much more. As a developer, you will call SAP’s standard Function Modules and also create your own custom ones.
This tutorial covers everything you need to know about Function Modules — from understanding the concept to creating one in SE37 and calling it from your programs.
What is a Function Module?
A Function Module is a self-contained piece of ABAP code that:
- Has a clearly defined interface (input parameters, output parameters, exceptions)
- Is stored in a central repository called a Function Group
- Can be called from any ABAP program in the system
- Can be tested independently using transaction SE37
Think of it like a service in a restaurant. You do not need to know how the kitchen prepares your food. You just place an order (input), and the kitchen delivers the dish (output). If something goes wrong, they tell you about it (exception). The kitchen can serve any table in the restaurant — just like a Function Module can serve any program.
Function Module vs Subroutine — When to Use What?
| Feature | Subroutine (FORM) | Function Module |
|---|---|---|
| Scope | Within the same program only | Across all programs in the system |
| Storage | Inside the program code | Centrally in a Function Group |
| Interface | USING, CHANGING | IMPORTING, EXPORTING, CHANGING, TABLES |
| Testing | Run the whole program | Test independently in SE37 |
| Exceptions | No built-in support | Full exception handling |
| RFC Support | No | Yes (can be called remotely) |
Use subroutines for small, program-specific tasks. Use Function Modules for anything that needs to be shared across programs or tested independently.
Understanding the Interface
Every Function Module has a well-defined interface with four types of parameters:
1. IMPORTING Parameters (Input)
Data that flows INTO the Function Module from the calling program. The caller provides these values.
2. EXPORTING Parameters (Output)
Data that flows OUT of the Function Module back to the calling program. The Function Module sets these values.
3. CHANGING Parameters (Input + Output)
Data that flows IN, gets modified, and flows back OUT. The caller sends a value, the Function Module changes it, and the updated value goes back.
4. TABLES Parameters (Internal Tables)
Used to pass internal tables in and out. In modern ABAP, it is better to use CHANGING or EXPORTING with table types instead.
Plus: EXCEPTIONS
Named error conditions that the Function Module can raise if something goes wrong.
How to Create a Function Module in SE37
Let me walk you through the steps to create a Function Module. We will build a simple one that calculates the area of a rectangle.
Step 1: Open Transaction SE37
Type SE37 in the SAP command field and press Enter. This opens the Function Builder.
Step 2: Create a Function Group First
Before creating a Function Module, you need a Function Group to store it in. Think of a Function Group as a folder that holds related Function Modules.
Go to transaction SE80 → Choose “Function Group” → Enter a name like ZLEARNSAP_UTILS → Create it.
Step 3: Create the Function Module
Back in SE37, enter a name for your Function Module: Z_CALCULATE_AREA
Click “Create”. Choose your Function Group (ZLEARNSAP_UTILS). Enter a short description: “Calculate area of a rectangle”.
Step 4: Define the Interface
IMPORTING tab:
| Parameter Name | Type | Associated Type | Description |
|---|---|---|---|
| IV_LENGTH | TYPE | I | Length of rectangle |
| IV_WIDTH | TYPE | I | Width of rectangle |
EXPORTING tab:
| Parameter Name | Type | Associated Type | Description |
|---|---|---|---|
| EV_AREA | TYPE | I | Calculated area |
EXCEPTIONS tab:
| Exception Name | Description |
|---|---|
| INVALID_INPUT | Raised if length or width is zero or negative |
Step 5: Write the Source Code
Click on the “Source Code” tab and write:
FUNCTION z_calculate_area.
*"----------------------------------------------------------------------
*" IMPORTING
*" VALUE(IV_LENGTH) TYPE I
*" VALUE(IV_WIDTH) TYPE I
*" EXPORTING
*" VALUE(EV_AREA) TYPE I
*" EXCEPTIONS
*" INVALID_INPUT
*"----------------------------------------------------------------------
" Validate input
IF iv_length <= 0 OR iv_width <= 0.
RAISE invalid_input.
ENDIF.
" Calculate area
ev_area = iv_length * iv_width.
ENDFUNCTION.
Step 6: Activate
Click the Activate button (or press Ctrl+F3). Your Function Module is now ready to use.
How to Call a Function Module
Now that we have created our Function Module, let us call it from a program:
REPORT z_call_fm_demo.
DATA: lv_area TYPE i.
CALL FUNCTION 'Z_CALCULATE_AREA'
EXPORTING
iv_length = 10
iv_width = 5
IMPORTING
ev_area = lv_area
EXCEPTIONS
invalid_input = 1
OTHERS = 2.
IF sy-subrc = 0.
WRITE: 'Area:', lv_area.
ELSEIF sy-subrc = 1.
WRITE: 'Error: Invalid input! Length and width must be positive.'.
ELSE.
WRITE: 'Error: Unknown error occurred.'.
ENDIF.
Output:
Area: 50
Understanding the CALL FUNCTION Syntax
CALL FUNCTION 'FUNCTION_MODULE_NAME'
EXPORTING
input_param1 = value1 " You send data TO the FM
input_param2 = value2
IMPORTING
output_param = variable " FM sends data BACK to you
EXCEPTIONS
exception_name = number " Error handling
OTHERS = number.
Notice something important here:
- In the CALL FUNCTION statement, EXPORTING means you are exporting data FROM your program TO the Function Module.
- IMPORTING means you are importing data FROM the Function Module INTO your program.
- This is the opposite perspective from the Function Module’s definition, and it confuses many beginners.
Testing a Function Module in SE37
One of the biggest advantages of Function Modules is that you can test them without writing a program. In SE37:
- Open your Function Module
Z_CALCULATE_AREA - Click the “Test” button (or press F8)
- Enter test values: IV_LENGTH = 10, IV_WIDTH = 5
- Click Execute
- The result screen shows EV_AREA = 50
This is incredibly useful for debugging. You can test your Function Module with different inputs, check edge cases, and verify the output — all without touching your calling program.
Calling SAP Standard Function Modules
SAP provides thousands of built-in Function Modules. Here are some commonly used ones:
Example 1: Get User Details
REPORT z_get_user.
DATA: lv_fullname TYPE string.
CALL FUNCTION 'USER_NAME_GET'
EXPORTING
user_name = sy-uname
IMPORTING
display_name = lv_fullname
EXCEPTIONS
OTHERS = 1.
IF sy-subrc = 0.
WRITE: 'Hello,', lv_fullname.
ENDIF.
Example 2: Convert Date to External Format
REPORT z_date_convert.
DATA: lv_date_ext TYPE c LENGTH 10.
CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
EXPORTING
date_internal = sy-datum
IMPORTING
date_external = lv_date_ext
EXCEPTIONS
OTHERS = 1.
IF sy-subrc = 0.
WRITE: 'Today is:', lv_date_ext.
ENDIF.
Output:
Today is: 24.07.2026
Example 3: Send Email Using Function Module
REPORT z_popup_demo.
DATA: lv_answer TYPE c.
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
titlebar = 'Confirm Action'
text_question = 'Do you want to continue processing?'
text_button_1 = 'Yes'
text_button_2 = 'No'
IMPORTING
answer = lv_answer
EXCEPTIONS
OTHERS = 1.
IF lv_answer = '1'.
WRITE: 'User clicked Yes - processing continues.'.
ELSE.
WRITE: 'User clicked No - processing cancelled.'.
ENDIF.
These standard Function Modules save you hundreds of hours of development time. Instead of writing complex logic from scratch, you just call the FM that SAP already built and tested.
How to Find Standard Function Modules
When you need a specific functionality, here is how to find the right Function Module:
Method 1: Search in SE37
Open SE37, type a pattern in the Function Module field (like *DATE*CONVERT*), and press F4. SAP will show all matching Function Modules.
Method 2: Search in SE84
Transaction SE84 is the Repository Information System. Navigate to Function Groups → Function Modules and search by keywords.
Method 3: Google It
Honestly, the fastest way is often to search Google for something like “SAP Function Module convert currency” and you will find the exact FM name from SAP documentation or community forums.
Exception Handling in Detail
Exceptions are how Function Modules communicate errors back to the caller. Let us look at how to handle them properly:
REPORT z_exception_demo.
DATA: lv_area TYPE i.
CALL FUNCTION 'Z_CALCULATE_AREA'
EXPORTING
iv_length = -5 " Negative value - should trigger exception
iv_width = 10
IMPORTING
ev_area = lv_area
EXCEPTIONS
invalid_input = 1
OTHERS = 2.
CASE sy-subrc.
WHEN 0.
WRITE: 'Area calculated successfully:', lv_area.
WHEN 1.
WRITE: 'Validation failed: Length and width must be positive numbers.'.
WHEN 2.
WRITE: 'An unexpected error occurred.'.
ENDCASE.
Output:
Validation failed: Length and width must be positive numbers.
Each exception is mapped to a number (1, 2, 3, etc.) in the CALL FUNCTION statement. After the call, you check sy-subrc to see which exception was raised. Zero means success, any other number means a specific exception occurred.
Always include OTHERS as the last exception to catch any unexpected errors that you did not explicitly map.
RFC-Enabled Function Modules
RFC stands for Remote Function Call. An RFC-enabled Function Module can be called from:
- Another SAP system (system-to-system communication)
- External applications (Java, .NET, Python)
- Third-party integration platforms
To make a Function Module RFC-enabled, simply check the “Remote-Enabled Module” option in SE37 under the Attributes tab. This is how SAP systems communicate with each other in a landscape.
Common Mistakes to Avoid
1. Forgetting to Handle Exceptions
If you do not specify exception handling in your CALL FUNCTION, and the FM raises an exception, your program will dump (short dump). Always include at minimum OTHERS = 1.
2. Confusing IMPORTING and EXPORTING Perspectives
Remember: from the caller’s perspective, EXPORTING sends data TO the FM, and IMPORTING receives data FROM the FM. This is backwards from what you might expect.
3. Not Activating the Function Group
After creating or changing a Function Module, you must activate both the FM and its Function Group. Forgetting this causes “FM not found” errors.
4. Using TABLES Instead of CHANGING for Internal Tables
The TABLES parameter type is considered obsolete. Use CHANGING with a proper table type instead. This gives better performance and cleaner code.
Interactive Checkpoints
🙋♂️ Checkpoint 1: What is the main advantage of Function Modules over Subroutines?
🙋♂️ Checkpoint 2: Why are IMPORTING and EXPORTING reversed between the FM definition and the CALL FUNCTION statement?
🙋♂️ Checkpoint 3: What happens if a Function Module raises an exception and you did not handle it in your CALL FUNCTION?
Summary
Function Modules are one of the most important concepts in ABAP development. They let you write code once and reuse it across the entire SAP system. Every real SAP project relies heavily on both standard SAP Function Modules and custom-built ones.
The key things to remember are: create your FM in SE37, define clear IMPORTING, EXPORTING, and EXCEPTION interfaces, always handle exceptions in your CALL FUNCTION statements, and test your FM independently before using it in production code.
Once you are comfortable with Function Modules, you are ready to explore BAPIs (Business Application Programming Interfaces), which are essentially standardized Function Modules that SAP provides for specific business processes. But that is a topic for another tutorial.
Written by Daksh Dedha
SAP Technical ConsultantDaksh is an SAP Technical Consultant specializing in ABAP programming, SAP S/4HANA migrations, Fiori development, and BTP cloud architecture. He authors free, hands-on tutorials to make enterprise SAP education accessible to all developers.
Test Your Knowledge
Loading question...
Quiz Completed
Related Tutorials
SAP ABAP Enhancement Framework — User Exits, BADIs, and Enhancement Points
Master SAP ABAP enhancements. Learn User Exits, Customer Exits, BADIs, Enhancement Points, and Implicit/Explicit Enhancement Spots to modify standard SAP without modifying source code.
ABAP ProgrammingABAP CDS Views — Core Data Services Complete Guide for Beginners
Learn ABAP CDS Views from scratch. Understand how to create CDS views in Eclipse ADT, use annotations, associations, parameters, and build OData services with CDS.
ABAP ProgrammingMessage Handling in SAP ABAP — Complete SE91 Message Classes Guide
Master SAP ABAP message handling. Learn how to create message classes in transaction SE91, use the MESSAGE statement, handle error types A E W I S X, and pass placeholders.
Found this tutorial useful? Share it with your SAP development team.