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: 

difference between macro and subroutine

Former Member
0 Kudos

what is the difference between macro and subroutine? i

need some example on macro

5 REPLIES 5

Former Member
0 Kudos

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.

Functions are just like FORMs, but are intended to be called external.

Some differences between FUNCTIONs and FORMs:

The name of a FORM must be unique within the program (two programs can have different FORMs with the same name). A FORM is intended to be called internal (from within the program, however by a 'trick' you can call them external).

The name of a FUNCTION has to be unique throughout the system. A FUNCTION is intended to be called external (and is thus shared by 'many' programs).

The latter is more important for programmers and maintenance. Since a FUNCTION is called external, it is important to keep the interface (parameters) the same. The interface of a FORM (which is intended to be called internal) is check when debugging a program (but it is only checked within the program that is debugged). So if the interface of a FORM is changed, but the call to the FORM (perform ) is not, the debugger will notice this and issue an error message.

In general:

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.

--Excerpt from http://sap.ittoolbox.com/documents/popular-q-and-a/macro-vs-subroutine-1594#

DATA p_c(10).

FIELD-SYMBOLS: <fs> TYPE ANY.

DATA p_old1 TYPE i VALUE 10.

DATA p_old2 TYPE i VALUE 11.

DATA p_old3 TYPE i VALUE 12.

DATA p_old4 TYPE i VALUE 13.

DATA p_old5 TYPE i VALUE 14.

DATA p_old6 TYPE i VALUE 15.

DATA p_old7 TYPE i VALUE 16.

DATA p_old8 TYPE i VALUE 17.

DATA p_old9 TYPE i VALUE 18.

DATA p_old10 TYPE i VALUE 19.

DATA p_old11 TYPE i VALUE 21.

DATA p_old12 TYPE i VALUE 22.

DATA p_old13 TYPE i VALUE 23.

DATA p_old14 TYPE i VALUE 24.

DEFINE ADD_MAPPING.

p_c = &1.

CONDENSE p_c.

CONCATENATE 'p_old' p_c INTO p_c.

ASSIGN (p_c) TO <fs>.

WRITE <fs>.

END-OF-DEFINITION.

DO 14 TIMES.

ADD_MAPPING sy-index.

ENDDO.

Former Member
0 Kudos

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.

http://sap.ittoolbox.com/documents/popular-q-and-a/macro-vs-subroutine-1594

http://help.sap.com/saphelp_nw2004s/helpdata/en/9f/db972835c111d1829f0000e829fbfe/frameset.htm

I hope it helps.

Best Regards,

Vibha

*Please mark all the helpful answers

Former Member
0 Kudos

Hi,

Example :

DATA: RESULT TYPE I,

N1 TYPE I VALUE 5,

N2 TYPE I VALUE 6.

DEFINE OPERATION.

RESULT = &1 &2 &3.

OUTPUT &1 &2 &3 RESULT.

END-OF-DEFINITION.

DEFINE OUTPUT.

WRITE: / 'The result of &1 &2 &3 is', &4.

END-OF-DEFINITION.

OPERATION 4 + 3.

OPERATION 2 ** 7.

OPERATION N2 - N1.

The produces the following output:

The result of 4 + 3 is 7

The result of 2 ** 7 is 128

The result of N2 - N1 is 1

Here, two macros, OPERATION and OUTPUT, are defined. OUTPUT is nested in OPERATION. OPERATION is called three times with different parameters. Note how the placeholders &1, &2, ... are replaced in the macros.

Well macros cant be debugged where subroutine can.

Refer to saphelp.com

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.

Former Member
0 Kudos

Hi,

<b>

Subroutines</b>

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.

<b>Defining Subroutines</b>

A subroutine is a block of code introduced by FORM and concluded by ENDFORM.

FORM <subr> [USING ... [VALUE(]<pi>[)] [TYPE <t>|LIKE <f>]... ]

[CHANGING... [VALUE(]<pi>[)] [TYPE <t>|LIKE <f>]... ].

...

ENDFORM.

<subr> is the name of the subroutine. The optional additions USING and

CHANGING define the parameter interface. Like any other processing block,

subroutines cannot be nested. You should therefore place your subroutine

definitions at the end of the program, especially for executable programs (type 1).

In this way, you eliminate the risk of accidentally ending an event block in the

wrong place by inserting a FORM...ENDFORM block.

<b>Macros</b>

If you want to reuse the same set of statements more than once in a program, you can include

them in a macro. For example, this can be useful for long calculations or complex WRITE

statements. 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.

The following statement block defines a macro <macro>:

DEFINE <macro>.

<statements>

END-OF-DEFINITION.

You must specify complete statements between DEFINE and END-OF-DEFINITION. These

statements can contain up to nine placeholders (&1, &2, ..., &9). You must define the macro

before the point in the program at which you want to use it.

Macros do not belong to the definition part of the program. This means that the DEFINE...ENDOF-

DEFINITION block is not interpreted before the processing blocks in the program. At the

same time, however, macros are not operational statements that are executed within a

processing block at runtime. When the program is generated, macro definitions are not taken

into account at the point at which they are defined. For this reason, they do not appear in the

overview of the structure of ABAP programs [Page 44].

A macro definition inserts a form of shortcut at any point in a program and can be used at any

subsequent point in the program. As the programmer, you must ensure that the macro

definition occurs in the program before the macro itself is used. Particular care is required if you

use both macros and include programs, since not all include programs are included in the syntax

check (exception: TOP include).

To use a macro, use the following form:

<macro> [<p1> <p2> ... <p9>].

When the program is generated, the system replaces <macro> by the defined statements and

each placeholder &i by the parameter <pi>. You can use macros within macros. However, a

macro cannot call itself.

DATA: RESULT TYPE I,

N1 TYPE I VALUE 5,

N2 TYPE I VALUE 6.

DEFINE OPERATION.

RESULT = &1 &2 &3.

OUTPUT &1 &2 &3 RESULT.

END-OF-DEFINITION.

DEFINE OUTPUT.

WRITE: / 'The result of &1 &2 &3 is', &4.

END-OF-DEFINITION.

OPERATION 4 + 3.

OPERATION 2 ** 7.

OPERATION N2 - N1.

The produces the following output:

The result of 4 + 3 is 7

The result of 2 ** 7 is 128

The result of N2 - N1 is 1

Here, two macros, OPERATION and OUTPUT, are defined. OUTPUT is nested in

OPERATION. OPERATION is called three times with different parameters. Note

how the placeholders &1, &2, ... are replaced in the macros.

Rgds,

Prakash