Functions in ABAP
Learn how ABAP packages reusable logic - classic FORM subroutines with PERFORM, ABAP Objects methods with IMPORTING/RETURNING parameters, default parameters, scope, and recursion
Reusable units of logic are the backbone of any non-trivial program, and ABAP - a multi-paradigm language that grew from procedural roots in 1983 into full object orientation in 1999 - offers more than one way to package them. Because ABAP spans both worlds, “functions” in ABAP is really three related ideas: classic subroutines (FORM / PERFORM), reusable function modules that live in function groups, and methods on classes from ABAP Objects.
This tutorial focuses on the two forms you can write and run in a single standalone program: the classic procedural subroutine and the modern object-oriented method. Subroutines (FORM ... ENDFORM) are the legacy workhorse - still everywhere in older SAP code - and pass data through USING and CHANGING parameter lists. Methods are the modern approach: they declare typed IMPORTING, EXPORTING, CHANGING, and RETURNING parameters, support default values, and - when they use RETURNING - can be called inline like a function in any expression.
In this tutorial you’ll see both styles in one runnable program: instance and static methods, a RETURNING parameter that enables function-style calls, a default parameter, recursion (the classic factorial), and how local variables scope inside a routine. The example runs on Node.js through the open-abap transpiler, so no SAP system is required.
Subroutines: FORM and PERFORM
A subroutine is defined with FORM name ... ENDFORM and invoked with PERFORM name. Parameters are grouped by intent:
USING- inputs passed into the subroutine.CHANGING- values the subroutine reads and writes back to the caller (the idiomatic way aFORM“returns” a result).
By convention, FORM routines sit at the end of a report, after the event blocks. They are considered obsolete in modern (clean-core) ABAP, but you will encounter millions of lines of them in real systems, so they are essential to recognize.
Methods: ABAP Objects
A class declares methods in its DEFINITION and codes them in its IMPLEMENTATION. Method parameters are explicitly typed and categorized:
IMPORTING- inputs (can specifyDEFAULTvalues to make them optional).EXPORTING- outputs passed back through named parameters.CHANGING- read-write parameters.RETURNING VALUE(...)- a single return value that turns the method into a functional method you can call inline, e.g.DATA(x) = obj->add( ... ).
Methods come in two flavors: instance methods (METHODS), called on an object with ->, and static methods (CLASS-METHODS), called on the class itself with =>. Recursion is natural - a method can simply call itself.
Putting It All Together
The program below defines a small local class lcl_math with an instance method (add), a method with a default parameter (power), and a static recursive method (factorial). It then demonstrates two classic FORM subroutines, including local variable scope.
Create a file named functions.abap:
| |
Running with Docker
Run the program using the same open-abap transpiler pattern from the Hello World tutorial - this time pointing at functions.abap instead of hello.abap.
| |
The transpiler converts the ABAP source to JavaScript, then @abaplint/runtime executes it - reproducing ABAP’s list buffer output on stdout.
Expected Output
4 + 9 = 13
5 squared = 25
2 cubed = 8
5! = 120
Area of 6 x 7 = 42
Total: 42
Notes on Behavior
A few details worth calling out:
- The first
WRITEhas no leading/, so it lands on the first line of the list. Every subsequentWRITE /starts a new line - the same list-buffer behavior introduced in Hello World. RETURNINGis what makes a method functional: becauseadd,power, andfactorialeach return a single value, they can be called inline inside an expression (DATA(lv_sum) = lo_math->add( ... )). A method with onlyEXPORTINGparameters would instead require the longerCALL METHOD ... IMPORTING ev_x = ...form.- Instance methods are called with
->on an object reference; static (CLASS-METHODS) methods are called with=>on the class name itself. iv_exp TYPE i DEFAULT 2makes that parameter optional. Callingpower( iv_base = 5 )uses the default2, yielding25; passingiv_exp = 3overrides it.- Inside
rectangle_area,p_localis a local variable - it exists only for the duration of that subroutine call and is not visible to the caller or to other routines. Data declared at the top level of the report (likelv_area) is effectively global to the program. FORMsubroutines return results throughCHANGINGparameters -PERFORM rectangle_area ... CHANGING lv_areawrites the computed area back intolv_area.
Real-World ABAP: Function Modules and Methods
Beyond subroutines and class methods, classic SAP systems lean heavily on function modules - globally reusable routines stored in function groups and maintained in transaction SE37. They cannot be defined inline in a single report (they require a function group), but their signature uses the same parameter categories:
| |
Such a function module is invoked with CALL FUNCTION:
| |
In modern, clean-core ABAP the recommendation is to prefer class methods (as shown in the runnable example) over both FORM subroutines and new function modules, reserving function modules for remote-enabled calls (RFC) and legacy integration points.
Key Concepts
FORM ... ENDFORM/PERFORM- classic procedural subroutines; pass inputs withUSINGand return results throughCHANGINGparameters.METHODSvsCLASS-METHODS- instance methods called with->on an object, static methods called with=>on the class.- Parameter categories -
IMPORTING(in),EXPORTING(out),CHANGING(in/out), andRETURNING VALUE(...)(single return value). RETURNINGenables functional calls - a method with aRETURNINGparameter can be used inline in any expression, just like a function.- Default parameters -
IMPORTING iv_exp TYPE i DEFAULT 2makes an argument optional with a fallback value. - Recursion - methods (and subroutines) can call themselves; the factorial example follows
n! = n * (n-1)!. - Scope - variables declared inside a
FORMorMETHODare local to that routine; top-level report data acts as global state. - Function modules - globally reusable routines in function groups, called with
CALL FUNCTION; prefer class methods in modern ABAP.
Comments
Loading comments...
Leave a Comment