Skip to main content

Modularization

In ABAP, you can modularize your code with 3 different ways, subroutine, include, and function module.

Overview of Modularization Methods.
MethodAvailabilityAccept ParametersAccess to Program variablesExpose its Variables to Program
Subroutineonly within the same program
IncludeCan be accessed globally in any program
Function ModuleCan be accessed globally in any program

Define a Subroutine


FORM writeData.
write: / 'Name', lv_name.
"... many statements
ENDFORM

This code block can be called anywhere in the same program using:

PERFORM writeData.

This will execute the code-block written in the subroutine.

Define an Include

excatly same process of create a Z_Program, with one minor difference.

When creating the TYPE should be INCLUDE PROGRAM instead of EXECUTE PROGRAM

alt text

Should be like this after creating it.

alt text Note we define a variable here and printed a statement from the Include. alt text Here we didnt define a Z_Age in our program. but we can write it. Since include expose its variables to the program.

alt text

Note the print statements

alt text

Define Function Module

Pre-requisite

before we create a Function Module, we first need to create a function group from SE80 Transaction.

We select Function Group and write our function group name. alt text

Then we click on the Binocular 🕶️. alt text Provide a description and click ✅ Save.

You will be prompted to select the package, and request similar to creating a program.

Creating the Function Module

You can create a function module using the SE37 Transaction.

Fill the name and click on Binocular 🕶️2 alt text

Choose the function group you created previously and add short text as description alt text

After Creation you will see many options (Import, Export, Source Code...etc) We will discuss them one by one.

alt text

Import

Here you define the parameters that should be passed to the function.

These will be provided later on when you call the function from the program.

alt text

Here we can see that the function Accepts two imports (aka parameters). We need to define the data elements that they accepts. Here we choose a integer and a string.

Export

Here you define the variables that should be exported or returned to from the function call.

Export and Import Variables

You can consider these variables as defined before hand. You can use them directly or assign a value to them.

alt text

Testing Function module

You can try the function module by clicking F8

It will navigate to this view. Here you can provide sample data and click

alt text You can click again F8 to run it (it will show export/import data). and to view the output you can click Shift + F5

The Performance and View of Import and Export Data alt text

The Output alt text

Calling Function Module

To call a function module you use CALL FUNCTION 'FUNCTION_NAME'.

For our function we called it like this.

" We define a variable to store our exported/returned value
DATA: writeSt TYPE STRING.

CALL FUNCTION 'Z_DISPLAY_SALES2'
EXPORTING
age = 10 " These will be sent to the function
name = 'Abdullah Bozaid' " These will be sent to the function
IMPORTING
WRITE_STATEMENT = writeSt. "This will store the WRITE_STATEMENT value inside our writeSt

OUTPUT

alt text

Using Pattern to Call Functions

There is an important tool in the ABAP Environment. it is called Pattern. You can access it from the toolbar or using CTRL+F6 alt text

Pattern will prompt you with a dialog to choose function module.

What it will do is write the function call with all its parameters. so you dont have to remember what variables the function export or import..etc. You can fill what you need.

alt text