Sunteți pe pagina 1din 13

ABAP : Subroutines

Applies to:
This document applies to SAP ECC 6.0, SAP Netweaver 2004s.

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

Author(s): 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
2010 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
Copyright......................................................................................................................................................... 12

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
2010 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
2010 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
2010 SAP AG 4
ABAP: Subroutines

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

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

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

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

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

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.

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

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

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.

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.

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

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.

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.

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

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

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
2010 SAP AG 10
ABAP: Subroutines

Related Content
Subroutines & their use in SAP Script

Passing Internal Table to Subroutine

Subroutine Pool

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

Copyright
Copyright 2010 SAP AG. All rights reserved.
No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG.
The information contained herein may be changed without prior notice.
Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors.
Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation.
IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9,
iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server,
PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER, OpenPower, PowerPC, BatchPipes,
BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX,
Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered trademarks of IBM Corporation.
Linux is the registered trademark of Linus Torvalds in the U.S. and other countries.
Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated
in the United States and/or other countries.
Oracle is a registered trademark of Oracle Corporation.
UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.
Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of
Citrix Systems, Inc.
HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C, World Wide Web Consortium, Massachusetts
Institute of Technology.
Java is a registered trademark of Sun Microsystems, Inc.
JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by
Netscape.
SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP Business ByDesign, and other SAP products and services mentioned
herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries.
Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and
other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered
trademarks of Business Objects S.A. in the United States and in other countries. Business Objects is an SAP company.
All other product and service names mentioned are the trademarks of their respective companies. Data contained in this document
serves informational purposes only. National product specifications may vary.
These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("SAP
Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or
omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in the
express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an
additional warranty.

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

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
2010 SAP AG 13

S-ar putea să vă placă și