Open In App

SAP ABAP | Function Modules

Last Updated : 06 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

SAP ABAP (Advanced Business Application Programming) is a high-level programming language created by the German software company SAP SE. ABAP is primarily used for developing and customizing applications within the SAP ecosystem, which includes enterprise resource planning (ERP) systems and other business software solutions. C++ is used to implement the ABAP kernel. A procedural and object-oriented programming model are both supported by the hybrid programming language ABAP.

SAP ABAP Function Modules

SAP ABAP | Function Modules

SAP ABAP | Function Modules

Function Modules in SAP ABAP (Advanced Business Application Programming) are reusable chunks of code that execute certain operations inside a SAP system. They contain a group of linked capabilities and may be accessed from a variety of applications, making them an important component of SAP’s modular and efficient design. The Function Builder (SE37 transaction code) is used to generate them. Function Modules can be invoked from various applications to carry out specified tasks.

Creating a New Function Module in SAP ABAP

  • To use the Function Builder, go to Development > ABAP Workbench > Function Builder on the SAP Menu.
  • Form a Function set: A set of linked function modules is called a function group. “Create Function Group” may be selected by right-clicking on the “Function Groups” node in the Project Explorer. Please provide a descriptive name and unique name for the function group.
  • To generate a new function module, right-click on it and choose “Create ABAP Function Module” from the menu. Give the function module a distinctive name and provide a short explanation.
  • Describe the Function Module Interface: The interface lists the function module’s arguments and exceptions. Choose the “Parameters” tab in the Function Module Editor. Names, data types, and whether the parameters are input, output, or both should all be specified when defining the parameters.
  • To put the Function Module logic into practice, go to the “Source Code” tab in the Function Module Editor. Write the logic for the function module in ABAP code. To carry out computations, get data, or communicate with external systems, use the parameter values.
  • Function Module: Save and Activate: First, save the function module, and then activate it. This allows other ABAP applications to utilize the function module.

Meaning and Importance of Function Modules in SAP ABAP

  • In SAP programming, Function Modules allow modularity and reusability.
  • They aid with the effective maintenance and organization of code.
  • They provide greater code abstraction, which reduces redundancy and mistakes.

Include Programs Vs. Function Modules in SAP ABAP

  • Include programs are portions of code that may be included in other programs, whereas function modules are freestanding units with well-defined inputs and outputs.
  • Include programs are often used for code sharing inside a program, whereas function modules are better suited for reusable, modular programming.

Groups of Functions

  • For better organization, function modules are frequently organized into function groups.
  • Function groups aid in the collective management of linked function modules.

Builder of Functions

  • Function Modules are created, modified, and managed using the Function Builder (transaction SE37).
  • Within the Function Builder, you may create import/export arguments, exceptions, and documentation.

Developing a New Program

  • You can call the Function Module from a new program (report, transaction, or other ABAP program type).

Parameter Configuration

  • You must give input parameters and possibly create tables or structures to accept output data before running a Function Module.

Modules Invoked

  • The CALL FUNCTION command is used to invoke a Function Module within your application.
  • The call specifies the name of the function module, as well as the input and output arguments.

Return Values Handling

  • You may manage the return values of a Function Module, including any exceptions it may raise, after executing it.
  • Error handling is critical to ensuring that your software runs smoothly.

Code Improvement

  • Function Modules can be customized to add or change functionality.
  • Function Exit, BAdI (Business Add-Ins), and other approaches are used for enhancements.

Execution

  • Finally, you run your program, which contains the function module call.
  • The Function Module completes its duty, and the results can be used in your application as needed.

Example of SAP ABAP Functional Module

Select the “Source Code” tab after launching the just built application.

Write your program’s ABAP code. In this scenario, you may translate an amount into words by using the SPELL_AMOUNT function module.

REPORT ZDEMO_SPELL_AMOUNT.

DATA(lv_amount) TYPE p DECIMALS 2 VALUE '12345.67'.
DATA(lv_currency) TYPE c LENGTH 5 VALUE 'USD'.
DATA(lv_spell_out) TYPE string.
CALL FUNCTION 'SPELL_AMOUNT'
EXPORTING
AMOUNT = lv_amount
CURRENCY = lv_currency
IMPORTING
IN_WORDS = lv_spell_out
EXCEPTIONS
AMOUNT_TOO_LARGE = 1
OTHERS = 2.
IF sy-subrc = 0.
WRITE: / 'Amount:', lv_amount,
/ 'Currency:', lv_currency,
/ 'In Words:', lv_spell_out.
ELSE.
WRITE: / 'Error in SPELL_AMOUNT function module.'.
ENDIF.

Customize the Code (Enhancement)

Custom logic may be added to the code to improve it. For instance, you may want to handle more cases or change the result.

DATA(lv_custom_spell_out) TYPE string.
" Custom logic to enhance the output
lv_custom_spell_out = lv_spell_out && ', which is a customized output.'.
WRITE: / 'Customized In Words:', lv_custom_spell_out.

Output

Amount: 12345.67
Currency: USD
In Words: TWELVE THOUSAND THREE HUNDRED FORTY-FIVE AND 67/100 DOLLARS
Customized In Words: TWELVE THOUSAND THREE HUNDRED FORTY-FIVE AND 67/100 DOLLARS, which is a customized output.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads