Difference between Macros and subroutines

Hi experts can any one tell difference between macros and subroutines.

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.
*U Shud start rewarding helpfull answers*
amit

Similar Messages

  • Difference between macro and subroutine

    what is the difference between macro and subroutine? i
    need some example on macro

    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

  • Difference between Macros and subroutine

    Hi all,
    What is the difference between the macros subroutine and function module functinalitywise.;

    Hi,
    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.
    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
    Regards,
    Priyanka.

  • Difference between implicit and explicit enhancement points

    Hi Folks,
    I had learned many documents to findout the difference between explicit and implicit enhancement point.I got the theoritical knowledge on that but losing on the practical side..
    Can anybody help me out with one real time scenario where the explicit enhancement points have been implemented and the step by step procedure to implement it..
    Hoping for the positive response from you..
    Regards,
    Rohan.

    Hi Rohan,
    In implicit they are available at the beginning of subroutines defined in the standard reports.
    Edit-> Enhancement operations->show implicit enhancement options.
    This will highlight all the implicit enhancements in the report.
    In Explicit enhancement we have two different types of enhancements -
    Enhancement point and Enhancement section.
    Enhancement point - SAP has some standard functionality defined for a report , but it may be required that you'll need to add your own code. In this case the code that you'll add will be executed along with the standard code.
    Enhancement section - SAP has its standard functionality defines but it may not suit your requirement, in that case you may need to add your code and you'll need that code to be executed only. By this method the existing code is automatically commented and a copy of the same code is available to edit.
    After executing the report only your code will be executed and the standard code will be bypassed.
    For Practicle implementation, follow this link.
    Link : [http://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/bb54c452-0801-0010-0e84-a653307fc6fc]
    i hope it will give you some clear picture....
    Thanks.
    Anurag

  • Difference between RP_PROVIDE_FROM_LAST and RP-PROVIDE-FROM-LAST

    Can anyone tell me the difference between RP_PROVIDE_FROM_LAST and RP-PROVIDE-FROM-LAST?  Both the macros are same difference is with underscore(_) and hiphen(-).  Both are working fine for the functionality without any difference.  Then why there are two macros for a single functionality?

    Just providing the links are considered as link farming ( which are against the rules of the forums ), the links would be removed for the following reasons:
    1) If a link is provided( not many ) , then you must point out the explanation in it
    2) If the links were easily searchable by the OP
    3) If the links just direct you to sap documentation
    4) If the reply consists only bunch of link references.
    I think the 3rd & 4th point made your post to be deleted.
    There are no links which states the differences between these two Macro's. Mod's are doing their right job, please join them and make this forum clean
    Kesav

  • Difference between zero and blank values

    Is there a way for the system to determine the difference between zero and blank values. One can have a zero allowed for the 0 value to actualy be there in the KF but is there a way that a macro can be written by which we can use the differentiation between the zero ( a value) and blank (no value) in the KF
    I have tried to look for an explanation on the service.sap.com and sdn but was not able to find anything.
    Thanks in advance

    Thanks for your reply. You mentioned something about the "fixing" setting that could make the "0" displayed in the planning book. Could you elaborate?
    Also, in the post from user "APO APO" on 5/8, the author seemed to indicate they could see the "0" in the planning book, as quoted below:
    "I have already made the zero allowed arrangement for that in the planning area and can see the 0 values in the planning book but I am not being able to make the macro get the results."
    I just want to find out if there is any way the user can visually distinguish b/w the "0" and blank values. If not, it'll be very confusing to the users. I understand the macros can distinguish b/w them.
    Thanks in advance.

  • Difference between Using and Changing

    hi,
    while defining subroutines, what is the exact difference between Using and Changing?
    when we pass by reference we can use both and while passing using value we can use both + Value (VAR).
    Then what is the exact difference between two?
    Also, what is formal parameter?q
    Moderator message - Please do not ask or answer basic questions - thread locked
    Edited by: Rob Burbank on Dec 14, 2009 3:31 PM

    Hi C`hinmay,
    When a subroutine is called with "USING" you can make use of the value inside your subroutine but you cannot change the value. With CHANGING you can CHANGE the value too. Formal parameter is the PARAMETER NAME used while defining the interface of a Subroutine.
    Regards,
    Ravi.

  • Difference between Appropriation and Proportion?- HFM

    Dear Professionals,
    I am new to HFM, I have a query regarding Value dimension.
    What is the difference between Appropriation and Proportion?
    If possible give me simple examples..
    Help me out
    Thanks
    Sarath

    Allocation is a subroutine in rules, and also a general concept of taking amounts from one place and spreading them using some method to other places. Proportion is a specific value dimension member which stores non-eliminated data after consolidation rules have been executed. Combine that with Elimination data and any adjustments from [Contribution Adjs] and that's the amount the child contributes to the parent entity.
    --Chris                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Difference between Null and null?

    What is the difference between null and NULL?
    When is each used?
    Thanks,

    veryConfused wrote:
    There is a null in java, but no NULL. null means no value. However, when assigning value, the following is different:Although the empty String has no special role. Null means, the referential type is not assigned (doesn't refer) to a specific object. The empty String is just another object though, so seeing it or pointing it out as something special when it actually isn't at all (no more special than new Integer(0) or new Object[0]) just adds to the confusion.

  • Difference between GUI_UPLOAD and WS_UPLOAD

    Hi,
    Please make me clear about the difference between GUI_UPLOAD and WS_UPLOAD. In which cases we need to use these modules...??
    Thanks,
    Satish

    I would suggest to always use the GUI_UPLOAD.  I say this because this is the function module which is used in the GUI_UPLOAD method of the class CL_GUI_FRONTEND_SERVICES.   Really, you should probably use the class/method instead of the function module.
      data: filename type string.
      filename = p_file.
      call method cl_gui_frontend_services=>gui_upload
             exporting
                  filename                = filename
                  filetype                = 'ASC'
             changing
                  data_tab                = iflatf
             exceptions
                  file_open_error         = 1
                  file_read_error         = 2
                  no_batch                = 3
                  gui_refuse_filetransfer = 4
                  no_authority            = 6
                  unknown_error           = 7
                  bad_data_format         = 8
                  unknown_dp_error        = 12
                  access_denied           = 13
                  others                  = 17.
    Regards,
    Rich Heilman

  • Difference between char and varchar, also the difference between varchar2

    Hi,
    Can anyone explain me the difference between char and varchar, and also the difference between varchar and varchar2...

    Varchar2 is variable width character data type, so if you define column with width 20 and insert only one character to tis column only, one character will be stored in database. Char is not variable width so when you define column with width 20 and insert one character to this column it will be right padded with 19 spaces to desired length, so you will store 20 characters in the dattabase (follow the example 1). Varchar data type from Oracle 9i is automaticlly promoted to varchar2 (follow example 2)
    Example 1:
    SQL> create table tchar(text1 char(10), text2 varchar2(10))
    2 /
    Table created.
    SQL> insert into tchar values('krystian','krystian')
    2 /
    1 row created.
    SQL> select text1, length(text1), text2, length(text2)
    2 from tchar
    3 /
    TEXT1 LENGTH(TEXT1) TEXT2 LENGTH(TEXT2)
    krystian 10 krystian 8
    Example 2:
    create table tvarchar(text varchar(10))
    SQL> select table_name,column_name,data_type
    2 from user_tab_columns
    3 where table_name = 'TVARCHAR'
    4 /
    TABLE_NAME COLUMN_NAME DATA_TYPE
    TVARCHAR TEXT VARCHAR2
    Best Regards
    Krystian Zieja / mob

  • The difference between Lion and Mountain Lion

    Can some one explain to me the difference between Lion and Mtn Lion? I'm currently 10.6.8 Is it beneficiall for me to upgrade?

    Mountain Lion is an enhanced version of previous OS X and so that is Mavericks.
    About upgrading it all depends on what your needs are and if your hardware supports it.
    System requirements for OS X Lion
    System requirements for OS X Mountain Lion
    OS X Mavericks: System Requirements
    Please check also applications compatibility. From Lion onward, you cannot run PPC application.

  • The difference between N80 and N80IE ?

    What is the difference between M80 and N80ie?

    02-Jan-2007
    07:45 PM
    korngear wrote:
    The Nokia N80 Internet Edition is a new version of this handset with the same hardware as the normal N80. It is due for release in Q4 of 2006 and will be available in Patina Bronze or Pearl Black, and has the following additional software included.
    Yahoo Go! for Mobile
    Flickr
    Some Amazon Branded Software
    'Download!' App management
    Internet Telephone - SIP VOIP Frontend
    WLAN Wizard
    Gizmo VOIP - Gizmo Project VOIP Frontend.
    \\en.wikipedia.org//
    @Korngear
    Thanks.
    Could N80 be upgraded to N80IE?

  • The difference between SFP+ and X2

    Dear Expert,
    I need to know what is the difference between SFP+ and X2! and can I use SFP+ card and module instead of X2?
    Thanks,
    Mohammad Saeed

    Hi,
    They are both used for 10Gig interfaces.  The difference is the connector type.
    SFP+ is LC and X2 is SC.
    You can not put an SFP+ into an X2 slot or X2 optic into a SFP+.
    HTH

  • Difference between ok_code and sy-ucomm

    Hi,
    Can any one tell me the difference between ok_code and sy-ucomm

    Hi,
    Actually OK_CODE and SY-Ucomm are the same. But experts suggest use of OK code for following reason:
    In each PAI event that a user triggers by choosing either a pushbutton on the screen or an element in a GUI status, the corresponding function code is placed into the system field SYST-UCOMM or SY-UCOMM and placed in the OK_CODE field (as long as the function code is not empty). Empty function codes are placed in neither the SY-UCOMM field nor the OK_CODE field.
    In your ABAP programs, you should work with the OK_CODE field instead of SY-UCOMM. There are two reasons for this: Firstly, the ABAP program has full control over fields declared within it, and secondly, you should never change the value of an ABAP system field. However, you should also always initialize the OK_CODE field in an ABAP program for the following reason:
    In the same way that the OK_CODE field in the ABAP program and the system field SY-UCOMM receive the contents of the corresponding screen fields in the PAI event, their contents are also assigned to the OK_CODE screen field and system field SYST-UCOMM in the PBO event. Therefore, you must clear the OK_CODE field in the ABAP program to ensure that the function code of a screen is not already filled in the PBO event with an unwanted value. This is particularly important when the next PAI event can be triggered with an empty function code (for example, using ENTER). Empty function codes do not affect SY-UCOMM or the OK_CODE field, and consequently, the old field contents are transported.

Maybe you are looking for

  • 2nd display not as bright as first display

    I have 2 facory video cards installed. They're the same (ATI 5770) each have mini ports and aldo DVI. When I boot up each day at work, the second display won't go to full brightness. USBs are both plugged in and I do get the brightness control levers

  • 4 pin to 6 pin Graphic card

    Hi everybody. I'm looking to install a graphic card vith 4 pin connection with 4 pin to 6 pin PCI Express Power Adapter. My question is: where i plug this 6 pin socket? My machine is: a G5 2.3 dual early 2005 M9748xx/A No: CK514J4XRU3 Thank's

  • Using Page Level Summary Column in Report 6i

    Hi Folks, I'm trying to create a report in which I want a Page level Summary column i.e., the Summary column should reset at each page and should display the sum of a particular field on each page depending upon how many records are displayed on each

  • Wireless Keyboard Brightness Keys Not Working

    Hello. Since an update a while back the brightness adjustment keys for screen brightness on my apple keyboard have stopped working. These are the F1 and F2 keys normally. On my Macbook Pro keyboard they work fine but the wireless keyboard keys do not

  • Wht is primary key in INFOTYPES

    wht is primary key in INFOTYPES