Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

What is the difference b/w Macros and Subroutine

Former Member
0 Kudos

Hi,

What is the difference b/w Macros and Subroutine?

I need clear example

4 REPLIES 4

Former Member
0 Kudos

HI

Macros are basically used for <b>complex calculation and Write statements</b>.U can reuse them more than once in a program whereas subroutines are mainly for <b>modularity</b>.

Thanks

Vasudha

former_member387317
Active Contributor
0 Kudos

1) Macros can only be used in the program the are defined in and only after the definition are expanded at compilation / generation. Subroutines (FORM) can be called from both the program the are defined in and other programs . A MACRO is more or less an abbreviation for some lines of code that are used more than once or twice. A FORM is a local subroutine (which can be called external). A FUNCTION is (more or less) a subroutine that is called external. Since debugging a MACRO is not really possible, prevent the use of them (I’ve never used them, but seen them in action). If the subroutine is used only local (called internal) use a FORM. If the subroutine is called external (used by more than one program) use a FUNCTION

2) The macros, which we may use,

(stored in table TRMAC)

, we cannot debug that code.

3) where as in subroutine,

its a abap code, directly visile in

the editor,

and we can debug this code.

4) MACROS =>

Getting a feel of macros

The basic syntax of macros is as follows:

DEFINE macro_name. "Macro Definition

……………. Statements

……………. Statements

…………………

END-OF-DEFINITION. "Macro Definition

macro_name par1 par2 …par9. "Macro call -parameters separated by spaces

Within the DEFINE... and END-OF-DEFINITION lies the body of the macro—the statements that you wish to be executed each time the macro is called. These statements may be any valid ABAP statements, such as WRITE, CLEAR, FORM calls, or database statements such as SELECT or UPDATE.

To familiarize yourself with the working of macros, it's necessary to take a close look at exactly what happens when an ABAP program containing a macro call is generated. Consider Listing A.

All ABAP programs must be generated before they can be executed. At the time of program generation, the system supplants each macro call, as shown in Listing A, with the statement(s) placed between the macro definition. Furthermore, the parameters passed at the time of macro calling are copied in place of the any placeholders (numbered &1, &2 …&9) found in the body of the macro definition. The system simply ignores the presence of the macros during the execution of the program—that is, all statements are executed as a single block of code:

write : int1.

write : int2.

write : int3.

Other than readability and meaningfulness, macros also offer performance advantages. For testing purposes, I wrote a macro that incremented the value of a variable by 1 and called the macro N times via a DO loop, as shown here:

DEFINE INCREMENT.

ADD 1 TO &1.

END-OF-DEFINITION.

DO N TIMES.

INCREMENT VAR1.

ENDDO.

SUBROUTINES=>

Subroutines

You call subroutines from ABAP programs using the PERFORM statement.

Subroutines are introduced with the FORM statement and concluded with the ENDFORM statement.

You can define subroutines in any ABAP program. You can either call a subroutine that is part of the same program or an external subroutine, that is, one that belongs to a different program. If you call an internal subroutine, you can use global data to pass values between the main program and the subroutine. When you call an external subroutine, you must pass actual parameters from the main program to formal parameters in the subroutine.

Refer below threads for more information..

Thanks & Regards

ilesh 24x7

Former Member
0 Kudos

hi udaya,

Macros can only be used in the program the are defined in and only after the definition.

Macros can take max 9 parameters.

Macros are expanded at compilation / generation.

Subroutines (FORM) can be called from both the program the are defined in and other programs ('perform

' of 'perform in program ').

Subroutines can take any amount of parameters.

Subroutines are 'expanded' at runtime.

reward if useful..

Former Member
0 Kudos

Hi Udaya,

Macros are used for calculation or somthing that u want to perform again & again.

e.g if you want to build a fieldcatalog in alv.then u can use macros for this.

while subroutines are used for making ur program modular and structured.so that every code that you have written would be understood easily.

Regards,

Hemant