If you want to reuse the same set of statements more than once in a program, you can include them in a macro.
You can only use a macro within the program in which it is defined, and it can only be called in lines of the program following its definition.
Macros can be useful for long calculations or complex WRITE statements.
Syntax
DEFINE <macro_name> 'Macro Statements END-OF-DEFINITION
Macros can use Parameters &N where N = 1,2,3...
Example:-
DATA: number1 TYPE I VALUE 1. DEFINE increment. ADD 1 to &1. WRITE &1. END-OF-DEFINITION. Increment number1. WRITE number1.
Output: 2
Include Programs are solely for modularizing source code, and have no parameter interface. Include programs allow you to use the same source code in different programs. They can be useful if you have lengthy data declarations that you want to use in different programs.
Syntax
Include <include program Name>
Points to Note
Example:
INCLUDE ZILX0004. WRITE: / 'User', SY-UNAME,/ 'Date', SY-DATUM. ================================ PROGRAM ZRPM0001. INCLUDE ZILX0004.
Subroutines are procedures that you can define in any ABAP program and also call from any program. Subroutines are normally called internally, that is, they contain sections of code or algorithms that are used frequently locally. If you want a function to be reusable throughout the system, use a function module.
Syntax-
FORM <Subroutine> [<pass>]. <Statement block>. ENDFORM.
<Subroutine> = Name of the subroutine
<pass> = Parameters being passed
Types of Subroutines
Calling a Subroutine Internal Subroutines
PERFORM <subroutine> [<pass>]
<subroutine> = Name of the subroutine
<pass> = Parameters being passed
Data declared in main program is automatically available.
External Subroutines
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
Function Modules are general purpose ABAP/4 routines that anyone can use. Infact , there are a large number of standard function Modules available.
Function Modules are organized into Function Groups: Collections of logically related functions. A Function module always belongs to a Function Group.
Syntax-
FUNCTION <function module> <Statements> ENDFUNCTION.
Important information Associated with Function Module
Call a Function Module
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]].