Skip to Content
author's profile photo Former Member
Former Member

reg, sub routine

how can u call a sub-routine from other programmes?

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jan 06, 2008 at 09:19 AM

    Hi,

    Start of Content Area

    Naming Subroutines Locate the document in its SAP Library structure

    With the PERFORM statement, you can call subroutines which are coded in the same ABAP program (internal calls), or subroutines which are coded in other ABAP programs (external calls).

    You can also specify the name of the subroutine dynamically at runtime, and call subroutines from a list.

    Internal Subroutine Calls

    To call a subroutine defined in the same program, you need only specify its name in the PERFORM statement:

    PERFORM subr [USING p1 p2... ]

    [CHANGING p1 p2... ].

    The internal subroutine can access all of the global data of the calling program.

    Example

    REPORT demo_mod_tech_perform_int.

    DATA: num1 TYPE i,

    num2 TYPE i,

    sum TYPE i.

    num1 = 2. num2 = 4.

    PERFORM addit.

    num1 = 7. num2 = 11.

    PERFORM addit.

    FORM addit.

    sum = num1 + num2.

    PERFORM out.

    ENDFORM.

    FORM out.

    WRITE: / 'Sum of', num1, 'and', num2, 'is', sum.

    ENDFORM.

    This produces the following output:

    Sum of 2 and 4 is 6

    Sum of 7 and 11 is 18

    In this example the subroutines addit and out are defined at the end of the program. addit is called by the program and out is called by addit. The subroutines have access to the global fields num1, num2 und sum.

    External Subroutine Calls

    The principal function of subroutines is for modularizing and structuring local programs. However, subroutines can also be called externally from other ABAP programs. In an extreme case, you might have an ABAP program that contained nothing but subroutines. These programs cannot run on their own, but are used by other ABAP programs as pools of external subroutines.

    However, if you want to make a function available throughout the system, you should use function modules instead of external subroutines. You create function modules in the ABAP Workbench using the Function Builder. They are stored in a central library, and have a defined release procedure.

    You can encapsulate functions and data in the attributes and methods of classes in ABAP Objects. For any requirements that exceed pure functions, you can use global classes instead of external subroutines.

    When you call a subroutine externally, you must know the name of the program in which it is defined:

    PERFORM subr(prog) [USING p1 p2... ]

    [CHANGING p1 p2... ] [IF FOUND].

    You specify the program name prog statically. You can use the IF FOUND option to prevent a runtime error from occurring if the program progdoes not contain a subroutine subr. In this case, the system simply ignores the PERFORM statement.

    When you call an external subroutine, the system loads the whole of the program containing the subroutine into the internal session of the calling program (if it has not already been loaded). In order to save memory space, you should keep the number of subroutines called in different programs to a minimum.

    Example

    Suppose a program contains the following subroutine:

    REPORT formpool.

    FORM header.

    WRITE: / 'Program started by', sy-uname,

    / 'on host', sy-host,

    'date:', sy-datum, 'time:', sy-uzeit.

    ULINE.

    ENDFORM.

    The subroutine can then be called from another program as follows:

    REPORT demo_mod_tech_perform_ext.

    PERFORM header(demo_mod_tech_formpool_1) IF FOUND.

    In this example, no data is passed between the calling program and the subroutine.

    Specifying Subroutines Dynamically

    You can specify the name of a subroutine and, in the case of external calls, the name of the program in which it occurs, dynamically as follows:

    PERFORM (fsubr)[IN PROGRAM (fprog)][USING p1 p2... ]

    [CHANGING p1 p2... ]

    [IF FOUND].

    The names of the subroutine and the external program are the contents of the fields fsubr und fprogrespectively. By using the option IF FOUND, you can prevent a runtime error from being triggered if no subroutine with the name fsubr is found. If you omit the parentheses, this variant of the PERFORMstatement behaves like the static variant.

    Example

    Assume a program contains the following subroutines:

    PROGRAM formpool.

    FORM sub1.

    WRITE: / 'Subroutine 1'.

    ENDFORM.

    FORM sub2.

    WRITE: / 'Subroutine 2'.

    ENDFORM.

    Dynamic Subroutine Specification:

    PROGRAM form_test.

    DATA: progname(8) TYPE c VALUE 'FORMPOOL',

    subrname(8) TYPE c.

    subrname = 'SUB1'.

    PERFORM (subrname) IN PROGRAM (progname) IF FOUND.

    SUBRNAME = 'SUB2'.

    PERFORM (subrname) IN PROGRAM (progname) IF FOUND.

    This produces the following output:

    Subroutine 1

    Subroutine 2

    The character field progname contains the name of the program, in which the subroutines are contained. The names of the subroutines are assigned to the character field subrname.

    Calling Subroutines from a List

    You can call a subroutine from a list as follows:

    PERFORM idx OF subr1 subr2 ... subrn.

    The system calls the subroutine specified in the subroutine list in position idx. You can only use this variant of the PERFORMstatement for internal subroutine calls, and only for subroutines without a parameter interface. The field idx can be a variable or a literal.

    Example

    REPORT demo_mod_tech_perform_list.

    DO 2 TIMES.

    PERFORM sy-index OF sub1 sub2.

    ENDDO.

    FORM sub1.

    WRITE / 'Subroutine 1'.

    ENDFORM.

    FORM sub2.

    WRITE / 'Subroutine 2'.

    ENDFORM.

    This produces the following output:

    Subroutine 1

    Subroutine 2

    Regards,

    Chandru

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 06, 2008 at 06:51 AM

    Hi,

    Use below code :

    PERFORM <fsubr> IN PROGRAM <fprog>.

    Thanks,

    Sriram Ponna.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 06, 2008 at 08:03 AM

    Hi,

    What u have talking is abt External Subroutine..Fine

    If u want to access a subroutine From another program means u can use the following code.

    PERFORM <subroutine> IN PROGRAM <program-name>.

    Reward if useful

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jan 06, 2008 at 08:29 AM

    You should call external subroutines like this:

    PERFORM <subroutine> IN PROGRAM <program-name> IF FOUND.
    

    Note: The addition IF FOUND is important and it is recommended to leave it on (especially when calling subroutines in other customer programs) otherwise if the calling program does not find the subroutine in the external program, it will result in a short-dump.

    Although rare, but it does happen sometimes that a subroutine name might be changed or subroutine deleted from the external program...so when the calling program calls it without the IF FOUND option of course the name will not match or it is not found as a result it will result in a short-dump - which we want to avoid at all times.

    Cheers,

    Sougata.

    Venkat,

    What did I do not to deserve any points?? And you gave full-points to someone who did a copy & paste!!! 😊

    Edited by: Sougata Chatterjee on Jan 6, 2008 8:59 PM

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.