Application Development and Automation 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: 
Read only

Subroutine

Former Member
0 Likes
3,138

what is meant by Subroutine

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,542

A <b>subroutine</b> is a block of code introduced by <b>FORM</b> and concluded by <b>ENDFORM</b>.

FORM subr [USING     p1 TYPE type

                     p2 LIKE field

                     ...

                     VALUE(p3) TYPE type

                     VALUE(p4) LIKE field

                     ...                 ]

          [CHANGING { {VALUE(p1)}|{p1 [{TYPE type}|{LIKE field}]}

                      {VALUE(p2)}|{p2 [{TYPE type}|{LIKE field}]}

                      ...                                        } ]

 ...

ENDFORM.

subr is the name of the subroutine. The optional additions <b>USING and CHANGING</b> 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. In this way, you eliminate the risk of non-executable statements occurring by mistake through a subroutine closing an event block at the wrong place.

<b>EXAMPLE :</b>

REPORT demo_mod_tech_data_types .

TYPES word(10) TYPE c.
DATA  text TYPE word.

text = '1234567890'.  WRITE / text.

PERFORM datatest.

WRITE / text.

FORM datatest.
  TYPES word(5) TYPE c.
  DATA  text TYPE word.
  text = 'ABCDEFGHJK'. WRITE / text.
ENDFORM.

reward points if it is usefull ....

Girish

8 REPLIES 8
Read only

Former Member
0 Likes
2,542

Hi,

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.

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.

You call subroutines using the statement

PERFORM... [USING ... <pi>... ]

[CHANGING... <pi>... ].

Subroutines can call other subroutines (nested calls) and may also call themselves (recursive calls). Once a subroutine has finished running, the calling program carries on processing after the PERFORM statement. You can use the USING and CHANGING additions to supply values to the parameter interface of the subroutine.

Example

-


PERFORM ADDITION USING 1 2.

PERFORM ADDITION USING 3 4.

FORM ADDITION USING V1 V2.

DATA: V3 TYPE I.

V3 = V1 + V2.

WRITE:/ V1.

ENDFORM.

Please reward points for helpful answers..

Regards,

Omkar.

Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
2,542

Hi,

Its a small procedure(Method). Its is defined using FORM and ENDFORM.

And called using PERFORM statement.

It can have the Input parameters with the help of USING and CHANGING paramters.

It cannot have Exceptions.

It cannot effect SY-SUBRC.

It cannot be remote enabled.

It is a smaller version of a Function module with less useage.

It cannot have OPTIONAL parameters.

Regards,

Sesh

Read only

Former Member
2,542

hi..

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.

As mentioned, both forms and FMs are reusable modularization units. To distinguish we generally say that forms are used for internal modularization and FMs are used for external modularization.

To decide on which to implement, consider whether you need the content to be used just for a limited program or wheteher it can be called from many independent programs. For the first purpose it is better to implement a form whereas for the second we implement an FM.

However, ABAP does not isolate the usage context. That is; you can call a form from another program within whose code the form is not actually implemented. However, this requires attention since the form may utilize global variables.

The same issue holds for FMs. FMs are encapsulated in function groups and function groups may have global variables that can be globally used by all FMs inside it.

Subroutines are loacl to a progarm.. It cant be extended to other program.

But Function Modules are similar to subroutines but are global in view and can be used in many program.

Subroutine have three types.

pass by value, pass by reference and pass by value and reference similar to pointers but not exactly the same.

Function modules are used in BDC , BAPI etc..

Also there are standard FMs which can be used.

http://help.sap.com/saphelp_nw2004s/helpdata/en/c5/aa575926ad11d2954d0000e8353423/content.htm

[Removed by the moderator.]

reward points if useful

Regards

Ashu

Read only

Former Member
0 Likes
2,543

A <b>subroutine</b> is a block of code introduced by <b>FORM</b> and concluded by <b>ENDFORM</b>.

FORM subr [USING     p1 TYPE type

                     p2 LIKE field

                     ...

                     VALUE(p3) TYPE type

                     VALUE(p4) LIKE field

                     ...                 ]

          [CHANGING { {VALUE(p1)}|{p1 [{TYPE type}|{LIKE field}]}

                      {VALUE(p2)}|{p2 [{TYPE type}|{LIKE field}]}

                      ...                                        } ]

 ...

ENDFORM.

subr is the name of the subroutine. The optional additions <b>USING and CHANGING</b> 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. In this way, you eliminate the risk of non-executable statements occurring by mistake through a subroutine closing an event block at the wrong place.

<b>EXAMPLE :</b>

REPORT demo_mod_tech_data_types .

TYPES word(10) TYPE c.
DATA  text TYPE word.

text = '1234567890'.  WRITE / text.

PERFORM datatest.

WRITE / text.

FORM datatest.
  TYPES word(5) TYPE c.
  DATA  text TYPE word.
  text = 'ABCDEFGHJK'. WRITE / text.
ENDFORM.

reward points if it is usefull ....

Girish

Read only

Former Member
0 Likes
2,542

Subroutines:

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.A subroutine is a block of code introduced by FORM and concluded by ENDFORM.Subroutines are principally for local modularization, that is, they are generally called from the program in which they are defined. You can use subroutines to write functions that are used repeatedly within a program. You can define subroutines in any ABAP program.

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.

Subroutines can access all of the global data in the program in which they are defined (main program). You therefore do not need to define a parameter interface if you do not want to change any data in the subroutine, or if very little data is involved.

FORM HEADER.

WRITE: / 'Program started by', SYUNAME,

/ 'on host', SYHOST,

'date:', SYDATUM,

'time:', SYUZEIT.

ULINE.

ENDFORM.

This example creates a subroutine called HEADER, which, like the example of an include program, displays a list header. However, if you want subroutines to perform complex operations on data without affecting the global data in the program, you should define a parameter interface through which you can pass exactly the data you need. In the interests of good programming style and encapsulation, you should always use a parameter interface, at least when the subroutine changes data.

You call subroutines using the statement

PERFORM... [USING ... <pi>... ]

[CHANGING... <pi>... ].

Subroutines can call other subroutines (nested calls) and may also call themselves (recursive calls). Once a subroutine has finished running, the calling program carries on processing after the PERFORM statement. You can use the USING and CHANGING additions to supply values to the parameter interface

of the subroutine.

Read only

Former Member
0 Likes
2,542

Hi,

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.

<a href="http://help.sap.com/saphelp_nw04/helpdata/en/9f/db975c35c111d1829f0000e829fbfe/content.htm">link fo defining and calling subroutine</a>

Pls reward points.

Regards,

Ameet

Read only

Former Member
0 Likes
2,542

Dear all , Thank you for your helpful Reply

Read only

Former Member
0 Likes
2,542

Dear all , Thank you for your helpful Reply