When you modularize source code, you place a sequence of ABAP statements in a module. Then, instead of placing all of the statements in your main program, you just call the module.When the program is generated, the source code in the modularization unit is treated as though it were actually physically present in the main program.
In this tutorial you will learn:
- Improve the structure of the program.
- Easy to read the code
- Easy to maintain the code
- Avoid redundancy and promotes code reuse
- Use of Macros
- Use of include files
- Function Modules
Macros can be useful for long calculations or complex WRITE statements.
DEFINE <macro_name> 'Macro Statements END-OF-DEFINITIONMacros can use Parameters &N where N = 1,2,3...
DATA: number1 TYPE I VALUE 1. DEFINE increment. ADD 1 to &1. WRITE &1. END-OF-DEFINITION. Increment number1. WRITE number1.Output: 2
Include <include program Name>Points to Note
- Include programs cannot call themselves.
- Include programs must contain complete statements.
INCLUDE ZILX0004. WRITE: / 'User', SY-UNAME,/ 'Date', SY-DATUM. ================================ PROGRAM ZRPM0001. INCLUDE ZILX0004.
FORM <Subroutine> [<pass>]. <Statement block>. ENDFORM.<Subroutine> = Name of the subroutine
<pass> = Parameters being passed
Types of Subroutines
- Subroutine defined in same program being called.
- Can access all the data objects declared in the main ABAP/4 program.
- Subroutine defined outside the program being called.
- Need to use the <pass> option or declare data objects in common parts of memory.
PERFORM <subroutine> [<pass>]<subroutine> = Name of the subroutine
<pass> = Parameters being passed
Data declared in main program is automatically available.
PERFORM <subroutine>(<Program>) [<pass>]. PERFORM <subroutine> (<Program>) [<pass>] [IF FOUND]. PERFORM (<subroutine>) IN PROGRAM (<Program>) [<pass>] [IF FOUND]. PERFORM <index> OF <subroutine1> <subroutine2> <subroutine3> [<pass>].Points to Note
- Nested calls are allowed in subroutines (i.e. PERFORM within a FORM ... ENDFORM ).
- Recursive calls are also possible.
- To define local data, use the DATA statement after FORM . Each time you enter the subroutine, the data is recreated (with an initial value) and released at the end (from the stack).
- To define global data used within a subroutine, use the LOCAL statement after FORM . The values are saved when you enter the subroutine and then released at the end (from the stack)
FUNCTION <function module> <Statements> ENDFUNCTION.Important information Associated with Function Module
- Import/Changing/Export parameters.
- Table Parameters/Exceptions.
- Source code - L<fgrp>U01 . <fgrp> is the Function Group
- Global Data - L<fgrp>TOP .Global data for the function group- Accessible across function modules in the function group.
- Main Program - SAPL<fgrp> . Contains the list of all the include files for that function group
To call a function module, use the CALL FUNCTION statement:
CALL FUNCTION <module> [EXPORTING f1 = a 1.... f n = a n] [IMPORTING f1 = a 1.... f n = a n] [CHANGING f1 = a 1.... f n = a n] [TABLES f1 = a 1.... f n = a n] [EXCEPTIONS e1 = r 1.... e n = r n [ERROR_MESSAGE = r E] [OTHERS = ro]].
Like executable programs (type 1) and module pools (type M), function groups can contain screens, selection screens, and lists.
Points to Note
- Function Groups cannot be executed.
- The name of a function group can be up to 26 characters long.
- When you create a function group or function module, the main program and include programs are generated automatically.
- Function groups encapsulate data.
- Goto Transaction SE80.
- Select Program in the DropDown.
- Write the name of the Function Group That you want to create. Generally User made Function groups start with "Z". e.g. - <Z_FUNCTION_GROUP_NAME> . Hit Enter Key.
- Note that The TOP Include is create by default if the user checks the option of creating a TOP include.
- Create a function Group (say "ZCAL").
- Create a function module, set the attributes like (Function group, Application, Short Text and Process Type) and Save.
- Include file "LZCALU01" will have source code of first function module.
- Include file "LZCALTOP" will have global data.
- Main program "SAPLZCAL" contains
- Global data Include file "LZCALTOP"
- Function modules include file "LZCALUXX"
- User defined Include files "LZCALF..", "LZCALO.." and "LZCALI.."
- Define interface parameters and Exceptions
- Write the source code
- Activate Function Module
- Testing the Function Module - Single Test & Debugging
- Documenting and Releasing a Function Module