SAP ABAP - Function Modules

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.
The results of the search will be displayed in a new window. The function modules are displayed in the lines with blue background and their function groups in pink lines. You may look further at the function group ISOC by using the Object Navigator screen (Transaction SE80). You can see a list of function modules and also other objects held in the function group. Let's consider the function module SPELL_AMOUNT. This function module converts numeric figures into words.
Step 1 − Go to transaction SE38 and create a new program called Z_SPELLAMOUNT.
Step 2 − Enter some code so that a parameter can be set up where a value could be entered and passed on to the function module. The text element text-001 here reads ‘Enter a Value’.
Step 3 − To write the code for this, use CTRL+F6. After this, a window appears where ‘CALL FUNCTION’ is the first option in a list. Enter 'spell_amount' in the text box and click the continue button.
Step 4 − Some code is generated automatically. But we need to enhance the IF statement to include a code to WRITE a message to the screen to say "The function module returned a value of: sy-subrc” and add the ELSE statement so as to write the correct result out when the function module is successful. Here, a new variable must be set up to hold the value returned from the function module. Let's call this as 'result'.
Following is the code −
REPORT Z_SPELLAMOUNT. 
data result like SPELL. 

selection-screen begin of line. 
selection-screen comment 1(15) text-001. 

parameter num_1 Type I. 
selection-screen end of line. 
CALL FUNCTION 'SPELL_AMOUNT' 
EXPORTING 
AMOUNT = num_1 
IMPORTING 
IN_WORDS = result. 

IF SY-SUBRC <> 0. 
   Write: 'Value returned is:', SY-SUBRC. 
else. 
   Write: 'Amount in words is:', result-word. 
ENDIF.
Step 5 − The variable which the function module returns is called IN_WORDS. Set up the corresponding variable in the program called ‘result’. Define IN_WORDS by using the LIKE statement to refer to a structure called SPELL.
Step 6 − Save, activate and execute the program. Enter a value as shown in the following screenshot and press F8.
The above code produces the following output −
Spelling the Amount 
Amount in words is: 
FIVE THOUSAND SIX HUNDRED EIGHTY


    25 Lectures 
     6 hours 

 Sanjo Thomas

    26 Lectures 
     2 hours 

 Neha Gupta

    30 Lectures 
     2.5 hours 

 Sumit Agarwal

    30 Lectures 
     4 hours 

 Sumit Agarwal

    14 Lectures 
     1.5 hours 

 Neha Malik

    13 Lectures 
     1.5 hours 

 Neha Malik
 Print
 Add Notes
 Bookmark this page