Sei sulla pagina 1di 12

Subroutines

Applies to:
This document applies to SAP ECC 6.0, SAP Netweaver 2004s. For more information, visit the ABAP
homepage.

Summary
This article contains information on Subroutines and its implementation in SAP ECC 6.0 version.

Author: Renjith R Thampi


Company: Applexus Software Solutions (P) Ltd
Created on: 5 January 2011

Author Bio
Renjith R Thampi is working as SAP Technology Consultant with Applexus Software Solutions (P) Ltd.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2011 SAP AG 1
ABAP: Subroutines

Table of Contents
Introduction ......................................................................................................................................................... 3
Defining Subroutines .......................................................................................................................................... 4
Structure of a Subroutine ................................................................................................................................ 4
Data Handling in Subroutines. ........................................................................................................................ 4
Global Data from Main Program .................................................................................................................................. 4
Local Data in Subroutine. ............................................................................................................................................. 5
Parameter Interface ..................................................................................................................................................... 6
Calling Subroutines ............................................................................................................................................. 7
Naming Subroutines........................................................................................................................................ 7
Internal Subroutine Call ............................................................................................................................................... 7
External Subroutine Call .............................................................................................................................................. 7
Passing Parameters to Subroutines. .............................................................................................................. 8
Related Content ................................................................................................................................................ 11
Disclaimer and Liability Notice .......................................................................................................................... 12

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2011 SAP AG 2
ABAP: Subroutines

Introduction
Subroutines are procedures that can be defined in any ABAP program and can be called form within the
program or from any ABAP program. Subroutines are mainly used locally, that is, they are generally called
from the program in which they are defined. Subroutines can be used to encapsulate parts of the program,
either to make the program easier to understand, or because a particular section of coding is used at several
points in the program. The program thus becomes more function-oriented, with its task split into different
constituent functions, and a different subroutine responsible for each one. As a rule, subroutines also make
the program easier to maintain. For example, one can execute it "invisibly" in the Debugger, and only see the
result. Consequently, if one knows that there are no mistakes in the subroutine itself, the source of the error
can be identified faster.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2011 SAP AG 3
ABAP: Subroutines

Defining Subroutines
Structure of a Subroutine
A subroutine begins with the FORM statement and ends with ENDFORM.
After the subroutine name, the interface is defined. In the FORM statement, the formal parameters are
defined and assign them types if required. The parameters must occur in a fixed sequence - first the
importing parameters, then the importing/exporting parameters. Within the subroutine, the data that passed
to it is processed using the formal parameters. Local data can be declared in a subroutine. After any local
data declarations, the statements that are executed as part of the subroutine are added.
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.

Data Handling in Subroutines.


Global Data from Main Program.
Subroutines can access all global data declared in the program in which it is defined. Therefore the need of
defining an explicit parameter interface is eliminated if there is no need to change any data in the subroutine,
or if very little data is involved. Any changes to the value of a global object within a subroutine can be done
by using the LOCAL <f> statement. 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 over written by
a data declaration inside the subroutine.
Example:
DATA: v_int1 TYPE i,
V_int2 TYPE i,
v_sum TYPE i.
PERFORM sum1.
WRITE: /'The Sum is:’ v_sum.
PERFORM sum2. “Sum1
WRITE: /'The Sum is:’ v_sum.
FORM sum1.
v_int1 = 10.
v_int2 = 20.
v_sum = v_int1 + v_int2.
WRITE: /'The Sum is:’ v_sum.
ENDFORM. " SUM1
FORM sum2. “SUM2
LOCAL: v_int1,
v_int2,
v_sum .
v_int1 = 5.
v_int2 = 15.
v_sum = v_int1 + v_int2.
WRITE: / 'The Sum is:’ v_sum.
ENDFORM. " SUM2

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2011 SAP AG 4
ABAP: Subroutines

When the code is executed the result generated is shown below.

Note: It is NOT a good practice to access or change global data directly within the subroutines and is not recommended.
Always use the Parameter/Form Interface methods which are explained below to perform these actions.

Local Data in Subroutine.


It is possible for the user to define local data types and objects that are only visible within that program.
There are two kinds of data types and objects – dynamic and static. Dynamic data objects only exist while
the subroutine is running, while static objects still exist after the subroutine has finished running, and retain
their values until the next time the subroutine is called. There are provisions for declaring local field symbols
and copies of global variables as well. If a local data type or object is declared in the same name of global
one then the global type or object cannot be addressed from within the subroutine. Within the subroutine the
locally declared data type gets precedence over the global one. If the local data has to be retained then the
declaration is carried out using STATICS statement.
Example:
PERFORM sroutine1.
PERFORM sroutine1.
SKIP.
PERFORM sroutine2.
PERFORM sroutine2.

FORM sroutine1 .
DATA lv_text TYPE string VALUE 'INIT'.
WRITE lv_text.
lv_text = 'XXXX'.
WRITE lv_text.
ENDFORM. " SROUTINE1

FORM sroutine2 .
STATICS lv_text TYPE string VALUE 'INIT'.
WRITE lv_text.
lv_text = '0000'.
WRITE lv_text.
ENDFORM. " SROUTINE2

The above code when executed gives the following output.

Field Symbols declared within the subroutines are local. They can’t be accessed outside the subroutines.
Each time the subroutine is called the field symbols remain unassigned even if it was assigned in the
previous run. Local field symbols can have the same names as global field symbols. Local field symbols hide

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2011 SAP AG 5
ABAP: Subroutines

global field symbols. It is also possible to declare structured field symbols locally. They can have local
structures and you can assign local fields to them.
In a subroutine it is possible to create local copies of global data on the local stack by using a local field
symbol and the following variants of the ASSIGN statement:
ASSIGN LOCAL COPY OF <f> TO <FS>.
The system places a copy of the specified global field <f> on the stack. In the subroutine, you can access
and change this copy without changing the global data by addressing the field symbol <FS>.
ASSIGN LOCAL COPY OF INITIAL <f> TO <FS>.
This statement creates an initialized copy of the global field <f> on the stack without copying the field
contents.
ASSIGN LOCAL COPY OF INITIAL LINE OF <itab> TO <FS>.
This statement creates an initial copy of the line of a global internal table <itab> on the stack.
ASSIGN LOCAL COPY OF INITIAL LINE OF (<f>) TO <FS>.
This statement creates an initial copy of the line of a global internal table <itab> on the stack. The internal
table is specified dynamically as the contents of the field <f>.

Parameter Interface
Formal parameter definitions are carried out using the USING and CHANGING keywords along with the
FORM statement. There can be any number of formal parameters but their sequence is fixed. These
parameters have to be filled while calling the subroutine. At the end of the subroutine, the formal parameters
are passed back to the corresponding actual parameters. Within a subroutine, formal parameters behave like
dynamic local data. Their initial value is the value passed from the corresponding actual parameter.

Subroutines can have the following formal parameters:

1. Parameters Passed by Reference


FORM <subr> USING ... <pi> [TYPE <t>|LIKE <f>] ...
CHANGING ... <pi> [TYPE <t>|LIKE <f>] ...

Here the address of the actual parameter is passed onto the formal parameter, as a result of which if the
formal parameter is changed the value of the actual parameter also changes.

2. Input Parameters That Pass Values


FORM <subr> USING ... VALUE(<pi>) [TYPE <t>|LIKE <f>] ...

Here the formal parameter has memory of its own. value of the actual parameter is passed to the formal
parameter. If the value of the formal parameter changes, this has no effect on the actual parameter.

3. Output Parameters That Pass Values


FORM <subr> CHANGING ... VALUE(<pi>) [TYPE <t>|LIKE <f>] ...

The value of the actual parameter is passed to the formal parameter. If the subroutine concludes
successfully, that is, when the ENDFORM statement occurs, or when the subroutine is terminated through a
CHECK or EXIT statement, the current value of the formal parameter is copied into the actual parameter.
The formal parameters can be of any ABAP standard data type and can be assigned using LIKE/TYPE key
words. The type can be specified generically (E.g. TYPE ANY, TYPE TABLE etc.) or fully (TYPE <type>,
TYPE LINE OF <> etc.). Once the type of the formal parameters is specified, the system checks that the
corresponding actual parameters are compatible when the subroutine is called. For internal subroutines, the
system checks this in the syntax check. For external subroutines, the check cannot occur until runtime.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2011 SAP AG 6
ABAP: Subroutines

A subroutine normally ends at the ENDFORM statement. However, it can be halted by using the EXIT or
CHECK statement. On termination the subroutine with EXIT or CHECK, the current values of the output
parameters (CHANGING parameters passed by value) are passed back to the corresponding actual
parameter. When EXIT is used the calling program regains control at the statement following the PERFORM
statement. When CHECK is used and if the logical expression in the CHECK statement is untrue, the
subroutine is terminated, and the calling program resumes processing after the PERFORM statement. If the
EXIT or CHECK statement occurs within a loop in a subroutine, it applies to the loop, and not to the
subroutine.

Calling Subroutines
Subroutines can be called by triggering the statement.
PERFORM... [USING ... <pi>... ]
[CHANGING... <pi>... ].

Subroutines can call other subroutines (nested calls) and may also call themselves (recursive calls). On
termination, the calling program carries on processing after the PERFORM statement. USING and
CHANGING statements can be used to transfer values to the parameter interface of the subroutine and
back.

Naming Subroutines.

Internal Subroutine Call


Subroutine defined in the same program can be called as shown below.
PERFORM <subr> [USING ... <pi>... ]
[CHANGING... <pi>... ].

External Subroutine Call


Subroutines can be externally called from other ABAP programs. The syntax followed to perform such a call
is shown below.
PERFORM <subr>(<prog>) [USING ... <pi>... ]
[CHANGING... <pi>... ] [IF FOUND].

The program name <prog> can be defined statically or dynamically. IF FOUND can be used to prevent a
runtime error if the program referenced does not have the mentioned subroutine. Under such a case the
perform call is skipped. 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
Example:
Assume the program has the following subroutine.
REPORT zth_subroutine_test1.
PERFORM sroutine1.
FORM sroutine1 .
DATA lv_text TYPE string VALUE 'Dynamic Call'.
WRITE lv_text.
ENDFORM. " SROUTINE1
This routine is called from another program as shown below.
REPORT ZTH_SUBROUTINE_CALL.
PERFORM sroutine1(zth_subroutine_test1)
IF FOUND.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2011 SAP AG 7
ABAP: Subroutines

The result is shown below.

Passing Parameters to Subroutines.


When calling a subroutine which has an interface defined values to all the formal parameters have to be
assigned in its interface while triggering the call. The sequence of the actual parameters must in sync with
the formal parameters. Actual parameters can be any data objects or field symbols of the calling program
whose technical attributes are compatible with the type specified for the corresponding formal parameter.
Example:
1. Call by Reference.

Consider the following program.


DATA: v_sum TYPE i,
v_num1 TYPE i,
v_num2 TYPE i.
v_num1 = 5.
v_num2 = 10.
WRITE: / 'Variable 1 :',v_num1,
'Variable 2 :',v_num2.
PERFORM pass USING v_num1
v_num2.
SKIP.
WRITE: / 'Variable 1 :',v_num1,
'Variable 2 :',v_num2.
FORM pass USING p_num1 TYPE i
p_num2 TYPE i.
p_num1 = 100.
p_num2 = 111.
ENDFORM. "sum

On execution it will give the following result.

Here it has to be noted that the value of actual parameters is changed because the value of the
formal parameters also changes.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2011 SAP AG 8
ABAP: Subroutines

2. Call by Value

Consider the following program.


DATA: v_sum TYPE i,
v_num1 TYPE i,
v_num2 TYPE i.
v_num1 = 5.
v_num2 = 10.
WRITE: / 'Variable 1 :',v_num1,
'Variable 2 :',v_num2.
PERFORM pass USING v_num1
v_num2 .
SKIP.
WRITE: / 'Variable 1 :',v_num1,
'Variable 2 :',v_num2.
FORM pass USING VALUE(p_num1) TYPE i
VALUE(p_num2) TYPE i.
p_num1 = 100.
p_num2 = 111.
ENDFORM. "sum

On execution it will give the following result.

It has to be observed here that even though the formal parameters were changed it has had no effect
on the actual parameters.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2011 SAP AG 9
ABAP: Subroutines

3. Call by Value and Result

Consider the following program.


DATA: v_sum TYPE i,
v_num1 TYPE i,
v_num2 TYPE i.
v_num1 = 5.
v_num2 = 10.
WRITE: / 'Variable 1 :',v_num1,
'Variable 2 :',v_num2.
PERFORM pass USING v_num1
CHANGING v_num2.
SKIP.
WRITE: / 'Variable 1 :',v_num1,
'Variable 2 :',v_num2.
FORM pass USING VALUE(p_num1) TYPE i
CHANGING VALUE(p_num2) TYPE i.
p_num1 = 100.
p_num2 = 111.
ENDFORM. "sum

On execution the following result is obtained.

Since we had specified CHANGING only for the PARAMETER p_num2


Therefore the value of the actual parameter v_num2 also changes.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2011 SAP AG 10
ABAP: Subroutines

Related Content
Subroutines & their use in SAP Script

Passing Internal Table to Subroutine

Subroutine Pool

For more information, visit the ABAP homepage.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2011 SAP AG 11
ABAP: Subroutines

Disclaimer and Liability Notice


This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not
supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade.
SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document,
and anyone using these methods does so at his/her own risk.
SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or
code sample, including any liability resulting from incompatibility between the content within this document and the materials and
services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this
document.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2011 SAP AG 12

Potrebbero piacerti anche