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,126

WHATS THE USE OF SUBROUTINE IN THE PROGRAM

8 REPLIES 8
Read only

Former Member
0 Likes
1,087

Hi,

Subroutine can be used code re-usablity..

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..

Thanks,

Naren

Read only

Former Member
0 Likes
1,087

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.

Regards,

Vara

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,087

They are also used for modularizing the code, organnizing logic into smaller, more manageable coding blocks.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
1,087

Hi,

Subroutines are used to modularize your program struture for better readbility and function flow.

When the entire code is split into subroutines, its easy to debug for any newcomer.

Regards

Subramanian

Read only

SantoshKallem
Active Contributor
0 Likes
1,087

to reuse the set(lines) of code.

easy to understand. no complexity.

Read only

Clemenss
Active Contributor
0 Likes
1,087

Hi Darsh,

Subroutines

You call a subroutine using the

PERFORM subrc[(prog)] ...

Without the (prog) addition, the call is internal. The subroutine is in the calling program and does not have to be loaded.

If you use the (prog) addition, the call is external. The subroutine is in the program prog. When you call the subroutine, the entire program prog is loaded into the internal session of the calling program (if it has not been already). The loaded program belongs to the program group of the calling program. However, if the subroutine belongs to a function group, a new additional program group is created. The program group to which the external subroutine belongs determines the interface work areas and screens that it will use. However, this assignment can vary dynamically, so it is best to avoid using external subroutines.

You call subroutines using the statement

PERFORM ... [USING p1 p2 ... ]

[CHANGING p1 p2 ... ].

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.

Regards,

Clemens

Read only

Former Member
0 Likes
1,087

hi darsh,

Subroutines are modularization techniques. They make program easy to understand. We can keep a set of stmts which must be performed repeatedly as a set and call them using a single stmt wherever we require. This is the simple concept of the subroutines. This reduces the program size and also makes it easy to understand. For ex. in my program i have to select some data from a table, do some modifications to the data and then display it in the output. Then it can be done like this

..............

...........

...........

start-of-selection.

perform fetch_data.

perform modify_data.

perform display_data.

end-of-selection.

form fetch_data.

.............

...........

endform.

form modify_data.

...........

............

endform.

form display_data.

............

............

endform.

In the above program it can be easily understood that first the subroutine for fetching data is called. Then subroutine for modifying data is called. Then subroutine for displaying data is called. Here perform stmts are for calling the subroutines named after perform and the actual action to be done i.e., the abap stmts for that particular task is given between form -- endform stmt. This block refers a subroutine body. similarly each form -- endform will refer to one subroutine body. Here the advantage is that it makes program easy to understand and also if we have to do same actions repeatedly like displaying data repeatedly after doing some modifications in the same manner, then we can simply call the subroutine with single stmt instead of writing the entire code repeatedly.

Hope u understood.

there are more options like using some parameters in the perform stmt like ' using ', etc.

Function Modules are the advanced modularization techniques of subroutines.

regards.

rajasekhar.

Read only

Former Member
0 Likes
1,087

Hi,

Procedure that can be defined in every ABAP program (except class pools and interface pools) but only outside classes. Can be called from any program. Begins with FORM and ends with ENDFORM. Can be called thru PERFORM statment.

It is one of modularization techniques. Quite often used piece of code can be written as a Subroutine and can be called anytime, anywhere depending upon where it is defined.

Regards

U. Uma