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: 

Subroutines

Former Member
0 Kudos

Hi all,

apart from reducing the redundancy of the code,why do we use subroutines.

i.e.. why do we use 'using','changing','tables', in syntax

can any one explain me with a scenario.

4 REPLIES 4

Former Member
0 Kudos

Hi,

Check this link.

Regards

guru

naimesh_patel
Active Contributor
0 Kudos

We can call the Subroutines from the other programs if you have the parameters with Using, Changing or tables.

Like:

Subroutine GET_COMPLETE_OBJECTS from the program RSWBO040 can be used in the other program to get the details abou the objects in th Trasnport. RSWBO040 gives the requestes for perticular object.

Regards,

Naimesh Patel

Former Member
0 Kudos

Well, i always believed that it was just about performance stuff (for the internal calls), but i'll see if i find any other reason (of course you can make external calls to other programs' performs, like used in most of sapscripts)

Here's some nice examples:

http://abaplovers.blogspot.com/2008/03/sap-abap-tutorial-on-subroutines.html

and of course: http://help.sap.com/saphelp_nw70/helpdata/EN/9f/db977635c111d1829f0000e829fbfe/frameset.htm

Former Member
0 Kudos

Hi,

SUBROUTINES

The process of breaking down a large program into smaller modules is supported by ABAP/4 through subroutine, also called forms.

Subroutines are programs modules, which can be called from ABAP/4 programs. Frequently used parts of program can be put into subroutines and these subroutines can be called explicitly from the program. You use subroutines mainly to modularize and structure your program.

Defining Subroutines

A subroutine is block of code introduced by FORM and concluded by ENDFORM. Following syntax is used to define a form or subroutine:

FORM <name> <parameters>

. . u2026..

u2026

ENDFORM.

Here parameters is optional. You can have plain subroutine without the parameters for example.

FORM SUB1.

u2026 . .

u2026 .

ENDFORM.

Calling Subroutines

You can call subroutines from program by following statement:

PERFORM <subr> [<para>].

Parameters are optional. i.e., you can call subroutine without passing any parameter

Perform SUB1.

Passing Data to Subroutines

When you work with global data in subroutines, you can put a copy of the global data on a local data stack and use it to avoid accidental loss of data (In this way you protect global data.)

You can pass data between calling program and subroutines by using parameters.

u2022 Parameters, which are defined during definition of a subroutine with FORM statement are called u2018formal parameteru2019.

u2022 Parameters which are specified during the call of a subroutine with the PERFORM statement are called u2018actual parameteru2019.

Parameters are passed to the FORM either:

u2022 By value

u2022 By Reference

u2022 By value and return.

By Value

Data : a type I value 20.

Perform sub1 using a.

Write a.

FORM sub1 using value (p_a)

P u2013 a = 15

ENDORM.

In this case during subroutine call, the formal parameter are created as copies of actual parameter.

The formal parameters have the memory of their own. Changes made to formal parameter have no effect on the actual parameter.

Like in this case, though value of p_a is changed to 15, it has no effect on u2018au2019 which remains as 20.

By Reference

Data: a type I value 20.

Perform sub1 using a.

Write a.

FORM sub1 using value (p_a)

P u2013 a = 15.

ENDORM.

By default system calls all the forms by reference.

In this case, only the address of the actual parameter is transferred to the formal parameters. The formal parameter has no memory of its own. If you change the formal parameter, change is visible in actual parameter also.

By Value and Return

Data : a type I value 20.

Perform sub1 changing a.

FORM sub1 changing value (p_a)

P u2013 a = 15.

ENDORM.

In this case if you change formal parameter, then the value of actual parameter is changed when the control is transferred back to the main program.

Passing Table to a Subroutine

You can pass internal tables as parameters USING or CHANGING in the FORM and PERFORM statements. If you want to access the components of the internal table, you must specify the type of the corresponding formal parameter.

You also must distinguish between internal tables with or without header lines. For internal tables with header lies, you must specify the table body by using square brackets [ ], after the table name to distinguish it from the header line.

With internal subroutines, you can use TYPE or LIKE to refer to the internal table you want to pass directly.

You can pass all internal tables as parameters in the list after TABLES in the FORM and PERFORM statements. Internal tables passed with TABLES are always called by reference.

If you pass all internal table with a header line, the table body and the table work area are passed to the subroutine. If you pass an internal table without a header line, a header line is created automatically as a local data object in the subroutine.

PROGRAM ZDEMO

DATA: Begin of itab occurs 0,

Number type I,

end of itab

PERFORM SUB1 TABLES ITAB.

LOOP AT ITAB.

WRITE: / itab-number.

ENDLOOP.

FORM SUB1 TABLES F_ITAB LIKE ITAB [ ].

DO 3 TIMES.

F_itab-number = SY-INDEX.

APPEND F_ITAB.

ENDDO.

ENDFORM.

In this example, an internal table ITAB is declared with a header line. ITAB is passed to the subroutine SUB1, where it is filled using the table work area F_ITAB. And itab is written in main program.

Regards,

Bhaskar

Edited by: Bhaskar Chikine on Sep 18, 2008 8:56 PM