Function
modules are the reusable components in ABAP program. If you are from Java, C,
CPP background, function modules are just like functions. Function module takes
input, process it and return the output.
How to open Function
Builder?
SAP
GUI provides UI based tool to define function modules. Use the transaction code
'SE37' to open function builder.
Below
step-by-step procedure explains how to create a function module using function
builder.
Step 1: Use the transaction
code ‘SE37’ to open function builder.
Step 2: First we need to
create function group. Function group is a container that maintains function
modules.
Go
to GOTO -> Function Groups ->
Create Group.
It
opens below window, give the
‘Function
group’ name as ‘Z_MATH_UTIL_FUNCTIONS
‘Short
text’ as ‘All the mathematical functions kept here’.
Press
Save button. It opens below window.
I
do not want to save this function group in any package, so click on the button
‘Local Object’.
Step 3: Now enter the
function module name as ‘Z_SUM_OF_NUMBERS’.
Click
on ‘Create’ button.
It
opens below window. Update function group name and short text.
Click
on ‘Save’ button.
It
opens below window.
Once
you press on ‘Activate’ button. It opens activation window.
Select
all the objects and activate them.
Step 4: Define input
arguments to the function module.
Click
on the ‘Import’ tab.
I
defined two variables of type Int.
Step 5: Define output
parameter of function module.
Step 6: Go to ‘Source code’
tab and update the source code. I just added below statement.
Z_SUM
= Z_VALUE1 + Z_VALUE2.
Activate
the function module.
Step 7: Now you can test the
function module by pressing F8 (or) by clicking on the button shown in below
image.
Enter
input values and press F8 (or) click on Execute button.
You
can able to see the output.
How to call function
module ‘Z_SUM_OF_NUMBERS’?
Open
‘ABAP Editor’ using the transaction ‘SE38’.
Give
the program name as ‘Z_TEST_SUM’ and click on ‘Create’ button.
It
opens below window.
Give
some Title, Select the Type as ‘Executable program’. Press Save button.
It
opens other window, click on the button ‘Local Object’.
It
opens below window.
Click
on the button ‘Pattern’.
It
opens below window. Select the radio button ‘CALL FUNCTION’ and give the
function name as ‘Z_SUM_OF_NUMBERS’. Press Ok button.
Now
it generates some default code.
Update
the code like below.
Z_TEST_SUM
*&---------------------------------------------------------------------* *& Report Z_TEST_SUM *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT Z_TEST_SUM. DATA sum_of_two_numbers TYPE I. CALL FUNCTION 'Z_SUM_OF_NUMBERS' EXPORTING Z_VALUE1 = 5 Z_VALUE2 = 6 IMPORTING Z_SUM = sum_of_two_numbers . Write: ' sum_of_two_numbers : ', sum_of_two_numbers.
No comments:
Post a Comment