What are Function Modules ? How to create a Function Module in SAP ?

What is a Function Module in SAP ABAP ? What is a Function Group in SAP ? Creating Function modules in SAP ABAP, calling function modules in SAP ABAP programs

+ -

Function Modules are sub-programs which contains set of re-usable statements with importing, exporting parameters, exceptions. Unlike include programs Function Modules can be executed independently.

Function Group in SAP ABAP

Function group is a container of Function modules, every Function Module must be saved in a Function group. T-codes for Function Modules and Function groups are SE37 OR SE80. We can save N number of Function Modules in a Function Group.

Components of Function Module:

Import: These are input parameters of a Function Module.

Export: These are output parameters of a Function Module.

Changing: These are parameters which act as importing and exporting parameters to a Function Module.

Tables: These are internal tables which also acts as importing and exporting parameters.

Exceptions: Exceptions in Function Modules are used to catch certain type of errors.

In this lesson we are going to learn how to create Function Group and Function Module, in order to create a Function Module we need a Function Group (We can save in existing one also), follow the below steps to create a Function Group.

Go to SE80.

Select 'Function Group' from drop-down list.

Provide a Function Group name ex: ZTFUNCTIONGROUP and press enter.

Creating a Function Group in SAP

A Popup will open Click on 'Yes' and provide short text, click on save.

SAP Create a Function Group

Select Local Object.

Function Group in SAP

Right-Click on Function Group name (ZTFUNCTIONGROUP) and activate.

Creating Function Module:

Here we take a small requirement and create a Function Module.

Requirement: Develop a Function Module, which displays Material details for a material no.

Go to SE37, provide a Function Module name ZSAPN_GET_MATERIAL, click on create.

Creating Function Module

A pop up will open, provide a Function Group name ZTEST_SAPNUTS and short text, Save .

Function Module Create

An information message will come saying that Function Module name is reserved for SAP , just click on continue icon

SAP Function Modules

Add below parameters.

Import: Select Import tab and enter IM_MATNR under 'Parameter name', TYPE under typing and MARA-MATNR under 'Associated Type', enter. See below image

Parameters in Function Modules

Export: Select Export tab and enter EX_MARA under 'Parameter name', TYPE under typing and MARA under Associated Type, enter. See below image

Function Module parameters

Source Code.

Select Source Code tab and write below code to get material details, save as local object and activate.

Function Module Source Code

SELECT SINGLE * FROM MARA
       INTO EX_MARA
       WHERE MATNR = IM_MATNR.

Test Function Module.

Click on Execute icon (F8), and give a material no, execute (F8).

Function Module testing

It will get material details

Test SAP Function Module

Using Function Modules in ABAP Programs

CALL FUNCTION is a keyword which is used to call a Function Module in ABAP Programs, now we use the above created Function Module in our ABAP program.

Go to SE38 and Provide name as ZSAPN_GET_MATERIAL and click on Create.

Call Function Module

Provide a title, select Executable Program from drop-down and Save as local object.

Create program in SE38

Source Code

Declare work area and an input field (parameter).

DATA : WA_MARA TYPE MARA . "work area to store material details

PARAMETERS : P_MATNR TYPE MARA-MATNR . "Material no input

START-OF-SELECTION. "Default event

To Call a Function Module, click on 'Pattern'

Function Module calling

A pop up will open, select CALL FUNCTION radio, provide Function Module name and click on continue icon (tick mark at the bottom).

Call a Function in SAP

The below code will be generated.

  CALL FUNCTION 'ZSAPN_GET_MATERIAL'
    EXPORTING
      IM_MATNR      = 
    IMPORTING
      EX_MARA       = 
  IF SY-SUBRC <> 0.
* Implement suitable error handling here
  ENDIF.

Now pass material no parameter and receive material data into a work area, full program code is below.

REPORT  ZSAPN_GET_MATERIAL.
DATA : WA_MARA TYPE MARA .

PARAMETERS : P_MATNR TYPE MARA-MATNR .

START-OF-SELECTION.

  CALL FUNCTION 'ZSAPN_GET_MATERIAL'
    EXPORTING
      IM_MATNR      = P_MATNR
    IMPORTING
      EX_MARA       = WA_MARA .
  IF SY-SUBRC <> 0.
* Implement suitable error handling here
 
  ENDIF.

  WRITE : WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MBRSH . "Display result

Execute (F8) and test the program.

Input Screen

Test program in SE38

Output

Function Module program result

Please Sign in to ask a question

Was this lesson helpful to you? Yes No 185 People out of 190 think this lesson helpful


Lesson Navigation

←Previous Chapter
Next Chapter →