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: 

reg, sub routine

Former Member
0 Kudos

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

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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

4 REPLIES 4

Former Member
0 Kudos

Hi,

Use below code :

PERFORM <fsubr> IN PROGRAM <fprog>.

Thanks,

Sriram Ponna.

Former Member
0 Kudos

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

Sougata
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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