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
1,393

Hi!

what is accessing the global work area values with regard to subroutine.

Pls Thanks for your replies in advance

Rahul.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,308

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.

<u><b>Global Data from the Main Program</b></u>

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', SY-UNAME,

/ 'on host', SY-HOST,

'date:', SY-DATUM, 'time:', SY-UZEIT.

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.

<u><b>Protecting Global Data Objects Against Changes</b></u>

To prevent the value of a global data object from being changed inside a subroutine, use the

following statement:

LOCAL <f>.

This statement may only occur between the FORM and ENDFORM statements. With LOCAL,

you can preserve the values of global data objects which cannot be hidden by a data declaration

inside the subroutine.

For example, you cannot declare a table work area that is defined by the TABLES statement with

another TABLES statement inside a subroutine. If you want to use the table work area locally, but

preserve its contents outside the subroutine, you must use the LOCAL statement.

Ex.

PROGRAM FORM_TEST.

TABLES SFLIGHT.

PERFORM TABTEST1.

WRITE: / SFLIGHT-PLANETYPE, SFLIGHT-PRICE.

PERFORM TABTEST2.

WRITE: / SFLIGHT-PLANETYPE, SFLIGHT-PRICE.

FORM TABTEST1.

SFLIGHT-PLANETYPE = 'A310'.

SFLIGHT-PRICE = '150.00'.

WRITE: / SFLIGHT-PLANETYPE, SFLIGHT-PRICE.

ENDFORM.

FORM TABTEST2.

LOCAL SFLIGHT.

SFLIGHT-PLANETYPE = 'B747'.

SFLIGHT-PRICE = '500.00'.

WRITE: / SFLIGHT-PLANETYPE, SFLIGHT-PRICE.

ENDFORM.

When you run the program, the following is displayed:

A310 150.00

A310 150.00

B747 500.00

A310 150.00

The program creates a table work area SFLIGHT for the database table SFLIGHT.

Different values are assigned to the table work area SFLIGHT in TABTEST1 and

TABTEST2. While the values assigned in TABTEST1 are valid globally, the values

assigned in TABTEST2 are only valid locally.

Regards,

Bhaskar

6 REPLIES 6
Read only

Former Member
0 Likes
1,308

Hi,

There are some variables which are declared in the main program itself.These varaibles are accessible from any point of the program.These variables are called GLOBAL variables.

If in any program you have a subroutine and in that subroutine you need to use any global variable then you can go ahead and directly do so.You do not need to pass these variables using the "USING" or "CHANGING" keywords.

This is how you can access the global variables in a subroutine.

Thanks,

Sandeep.

Read only

0 Likes
1,308

Hi!

Can u give some coding part of that

and

Same thing can be done using Fun Moduled possible???

Thanks Sandeep

Rahul.

Read only

0 Likes
1,308

Hello,

For function module, you can use global variables too, but it is recommended to pass the variables in the parameter

You just have to define the variables in the "top" include of your function group

Read only

Former Member
0 Likes
1,309

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.

<u><b>Global Data from the Main Program</b></u>

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', SY-UNAME,

/ 'on host', SY-HOST,

'date:', SY-DATUM, 'time:', SY-UZEIT.

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.

<u><b>Protecting Global Data Objects Against Changes</b></u>

To prevent the value of a global data object from being changed inside a subroutine, use the

following statement:

LOCAL <f>.

This statement may only occur between the FORM and ENDFORM statements. With LOCAL,

you can preserve the values of global data objects which cannot be hidden by a data declaration

inside the subroutine.

For example, you cannot declare a table work area that is defined by the TABLES statement with

another TABLES statement inside a subroutine. If you want to use the table work area locally, but

preserve its contents outside the subroutine, you must use the LOCAL statement.

Ex.

PROGRAM FORM_TEST.

TABLES SFLIGHT.

PERFORM TABTEST1.

WRITE: / SFLIGHT-PLANETYPE, SFLIGHT-PRICE.

PERFORM TABTEST2.

WRITE: / SFLIGHT-PLANETYPE, SFLIGHT-PRICE.

FORM TABTEST1.

SFLIGHT-PLANETYPE = 'A310'.

SFLIGHT-PRICE = '150.00'.

WRITE: / SFLIGHT-PLANETYPE, SFLIGHT-PRICE.

ENDFORM.

FORM TABTEST2.

LOCAL SFLIGHT.

SFLIGHT-PLANETYPE = 'B747'.

SFLIGHT-PRICE = '500.00'.

WRITE: / SFLIGHT-PLANETYPE, SFLIGHT-PRICE.

ENDFORM.

When you run the program, the following is displayed:

A310 150.00

A310 150.00

B747 500.00

A310 150.00

The program creates a table work area SFLIGHT for the database table SFLIGHT.

Different values are assigned to the table work area SFLIGHT in TABTEST1 and

TABTEST2. While the values assigned in TABTEST1 are valid globally, the values

assigned in TABTEST2 are only valid locally.

Regards,

Bhaskar

Read only

Former Member
0 Likes
1,308

Hi,

data: gv_var1 type i,

gv_var2 type i,

gv_var3 type i.

perform gf1.

perform gf2.

form gf1.

gv_var1 = 10.

gv_var3 = 20.

gv_var2 = gv_var1 + gv_var3.

endform.

form gf2.

write: gv_var3.

endform.

Read only

Former Member
0 Likes
1,308

Hi,

A global variable defined outside of a subroutine by using the TABLES or DATA stmts.

It can be accessed from any point in the prgm,be it inside an event or inside subroutine.

Variables by using TABLES stmt are always global.

So,no need to use tables stmt inside a subroutine since the def is always global.

Global definitions should be placed at the top of the prgm.

To define a local table work area insid a subroutine,have to use LOCAL instead of tables stmt.