Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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 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.
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
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.
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
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.
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.
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.
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.
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
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
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
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
Subroutine Pool
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
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2011 SAP AG 12