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

..............sap............................................................................ 5 Parameter Interface........................................ 8 Related Content...... 4 Local Data in Subroutine............................................................................................................................................ 3 Defining Subroutines.......boc................................................................................................... 4 Structure of a Subroutine............................ 12 SAP COMMUNITY NETWORK SDN .. 4 Global data from main program..................................................................................................................................................................................................................sdn............................................................................................com | BPX ..............................................................................................com | BOC ............... ABAP: Subroutines Table of Contents Introduction.............................................................sap.......................................................sap.................................................................................bpx..............................................................................com | UAC ..............................................................................................................................................................................................................................................uac............................... 4 Data Handling in Subroutines............................................................................................................................................................................................................. 6 Calling Subroutines................................................................................ 11 Copyright.............................................. 7 Naming Subroutines................com © 2010 SAP AG 2 .. 7 External Subroutine Call... 7 Passing Parameters to Subroutines................................................ 7 Internal Subroutine Call...........................................sap....................................................................................................................................

com © 2010 SAP AG 3 . either to make the program easier to understand. subroutines also make the program easier to maintain.sap.sdn. if one knows that there are no mistakes in the subroutine itself.sap.com | UAC .sap. they are generally called from the program in which they are defined. and only see the result. The program thus becomes more function-oriented. the source of the error can be identified faster. with its task split into different constituent functions. and a different subroutine responsible for each one. that is. one can execute it "invisibly" in the Debugger. For example.com | BOC .sap. or because a particular section of coding is used at several points in the program. 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. As a rule.uac.com | BPX . Consequently. Subroutines can be used to encapsulate parts of the program.bpx.boc. SAP COMMUNITY NETWORK SDN . Subroutines are mainly used locally.

Local data can be declared in a subroutine.. v_int2 = 20. v_int1 = 10.sap. <subr> is the name of the subroutine.com | BOC .com | BPX . In the FORM statement. PERFORM sum1.uac. Data Handling in Subroutines. This statement may only occur between the FORM and ENDFORM statements. you can preserve the values of global data objects which cannot be over written by a data declaration inside the subroutine. the interface is defined.first the importing parameters. WRITE: /'The Sum is:’ v_sum. then the importing/exporting parameters.. or if very little data is involved. ENDFORM. Subroutines can access all global data declared in the program in which it is defined. v_int2 = 15. WRITE: /'The Sum is:’ v_sum. " SUM1 FORM sum2. PERFORM sum2. V_int2 TYPE i. With LOCAL.. Example: DATA: v_int1 TYPE i. the statements that are executed as part of the subroutine are added.com | UAC . v_sum = v_int1 + v_int2. v_sum .sdn. " SUM2 SAP COMMUNITY NETWORK SDN . the data that passed to it is processed using the formal parameters.sap.. The optional additions USING and CHANGING define the parameter interface. ] [CHANGING..sap.boc. “Sum1 WRITE: /'The Sum is:’ v_sum. Within the subroutine. Therefore the need of defining an explicit parameter interface is eliminated if there is no need to change any data in the subroutine. [VALUE(]<pi>[)] [TYPE <t>|LIKE <f>].sap. ENDFORM. After any local data declarations. Global data from main program. v_sum = v_int1 + v_int2. v_sum TYPE i. v_int2. Any changes to the value of a global object within a subroutine can be done by using the LOCAL <f> statement. The parameters must occur in a fixed sequence . ENDFORM. “SUM2 LOCAL: v_int1. [VALUE(]<pi>[)] [TYPE <t>|LIKE <f>]. the formal parameters are defined and assign them types if required. ABAP: Subroutines Defining Subroutines Structure of a Subroutine A subroutine begins with the FORM statement and ends with ENDFORM. FORM sum1.com © 2010 SAP AG 4 . ]. FORM <subr> [USING .. v_int1 = 5.. WRITE: / 'The Sum is:’ v_sum. After the subroutine name..bpx.

com | BPX . They can have local structures and you can assign local fields to them. WRITE lv_text.com | BOC .uac. lv_text = '0000'. Within the subroutine the locally declared data type gets precedence over the global one.bpx.sap. WRITE lv_text.com | UAC . There are two kinds of data types and objects – dynamic and static. Dynamic data objects only exist while the subroutine is running. WRITE lv_text. If the local data has to be retained then the declaration is carried out using STATICS statement.sdn. PERFORM sroutine1.sap. Local field symbols hide global field symbols. They can’t be accessed outside the subroutines. WRITE lv_text.sap. DATA lv_text TYPE string VALUE 'INIT'.sap. Local Data in Subroutine. " SROUTINE2 The above code when executed gives the following output. ABAP: Subroutines When the code is executed the result generated is shown below. Local field symbols can have the same names as global field symbols. Example: PERFORM sroutine1. SKIP. ENDFORM. ENDFORM. " SROUTINE1 FORM sroutine2 . It is also possible to declare structured field symbols locally. SAP COMMUNITY NETWORK SDN . Each time the subroutine is called the field symbols remain unassigned even if it was assigned in the previous run. STATICS lv_text TYPE string VALUE 'INIT'.com © 2010 SAP AG 5 . It is possible for the user to define local data types and objects that are only visible within that program. 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. Field Symbols declared within the subroutines are local. PERFORM sroutine2. FORM sroutine1 .boc. and retain their values until the next time the subroutine is called. while static objects still exist after the subroutine has finished running. lv_text = 'XXXX'. There are provisions for declaring local field symbols and copies of global variables as well. PERFORM sroutine2.

. ASSIGN LOCAL COPY OF INITIAL LINE OF <itab> TO <FS>. The system places a copy of the specified global field <f> on the stack. Subroutines can have the following formal parameters: 1.com | BPX .... VALUE(<pi>) [TYPE <t>|LIKE <f>] .sap.sdn. Input Parameters That Pass Values FORM <subr> USING . you can access and change this copy without changing the global data by addressing the field symbol <FS>..sap. the formal parameters are passed back to the corresponding actual parameters.. 2.com | BOC . this has no effect on the actual parameter. <pi> [TYPE <t>|LIKE <f>] . Here the formal parameter has memory of its own.uac.. CHANGING .. 3.. At the end of the subroutine.sap. Parameters Passed by Reference FORM <subr> USING . This statement creates an initialized copy of the global field <f> on the stack without copying the field contents. 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>.com © 2010 SAP AG 6 . This statement creates an initial copy of the line of a global internal table <itab> on the stack. Output Parameters That Pass Values FORM <subr> CHANGING .. <pi> [TYPE <t>|LIKE <f>] . Within a subroutine.... SAP COMMUNITY NETWORK SDN .com | UAC . 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>. Their initial value is the value passed from the corresponding actual parameter.sap. as a result of which if the formal parameter is changed the value of the actual parameter also changes. value of the actual parameter is passed to the formal parameter.. ASSIGN LOCAL COPY OF INITIAL <f> TO <FS>. These parameters have to be filled while calling the subroutine.boc. Here the address of the actual parameter is passed onto the formal parameter.bpx. Parameter Interface Formal parameter definitions are carried out using the USING and CHANGING keywords along with the FORM statement. In the subroutine. There can be any number of formal parameters but their sequence is fixed. VALUE(<pi>) [TYPE <t>|LIKE <f>] . If the value of the formal parameter changes. The internal table is specified dynamically as the contents of the field <f>. formal parameters behave like dynamic local data...

. The syntax followed to perform such a call is shown below. and not to the subroutine.. The program name <prog> can be defined statically or dynamically. A subroutine normally ends at the ENDFORM statement.. it can be halted by using the EXIT or CHECK statement. When EXIT is used the calling program regains control at the statement following the PERFORM statement. <pi>. Subroutines can call other subroutines (nested calls) and may also call themselves (recursive calls).com | BOC .boc. <pi>. ] [CHANGING.. On termination the subroutine with EXIT or CHECK. ].. PERFORM <subr> [USING . <pi>. The formal parameters can be of any ABAP standard data type and can be assigned using LIKE/TYPE key words. Once the type of the formal parameters is specified. When CHECK is used and if the logical expression in the CHECK statement is untrue.) or fully (TYPE <type>. TYPE LINE OF <> etc.. PERFORM.. ABAP: Subroutines The value of the actual parameter is passed to the formal parameter. <pi>. the system checks that the corresponding actual parameters are compatible when the subroutine is called.. USING and CHANGING statements can be used to transfer values to the parameter interface of the subroutine and back. and the calling program resumes processing after the PERFORM statement. ] [CHANGING. However.....sap.... that is. the subroutine is terminated. Naming Subroutines. or when the subroutine is terminated through a CHECK or EXIT statement.bpx. the check cannot occur until runtime.g.sap. TYPE TABLE etc. For external subroutines.com | UAC .. ].. PERFORM <subr>(<prog>) [USING . If the subroutine concludes successfully. when the ENDFORM statement occurs. Internal Subroutine Call Subroutine defined in the same program can be called as shown below. <pi>.sap..com | BPX . ] [IF FOUND]..). Under such a case the SAP COMMUNITY NETWORK SDN . [USING . For internal subroutines. On termination.sap. <pi>.. The type can be specified generically (E. Calling Subroutines Subroutines can be called by triggering the statement. the system checks this in the syntax check.com © 2010 SAP AG 7 . ] [CHANGING.sdn.. the calling program carries on processing after the PERFORM statement.uac. IF FOUND can be used to prevent a runtime error if the program referenced does not have the mentioned subroutine.. External Subroutine Call Subroutines can be externally called from other ABAP programs.. the current value of the formal parameter is copied into the actual parameter. the current values of the output parameters (CHANGING parameters passed by value) are passed back to the corresponding actual parameter. it applies to the loop.... If the EXIT or CHECK statement occurs within a loop in a subroutine. TYPE ANY.

WRITE: / 'Variable 1 :'. 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.com | BPX .sap. PERFORM sroutine1.com | BOC . The result is shown below.sap.boc. DATA lv_text TYPE string VALUE 'Dynamic Call'.uac. REPORT zth_subroutine_test1. 'Variable 2 :'. FORM sroutine1 . SAP COMMUNITY NETWORK SDN .com © 2010 SAP AG 8 . v_num1 TYPE i. ABAP: Subroutines perform call is skipped. v_num2 = 10. v_num1 = 5. 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. " SROUTINE1 This routine is called from another program as shown below. ENDFORM.com | UAC . PERFORM sroutine1(zth_subroutine_test1) IF FOUND.sap. v_num2 TYPE i. When you call an external subroutine. Call by Reference. 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. DATA: v_sum TYPE i. Consider the following program. The sequence of the actual parameters must in sync with the formal parameters. REPORT ZTH_SUBROUTINE_CALL. WRITE lv_text.sdn.v_num1.v_num2. Example: 1.bpx. Passing Parameters to Subroutines.sap.

DATA: v_sum TYPE i. ENDFORM.v_num2.v_num1.boc. Here it has to be noted that the value of actual parameters is changed because the value of the formal parameters also changes.v_num2.v_num1.uac.v_num2. p_num2 = 111. SKIP. v_num1 TYPE i. PERFORM pass USING v_num1 v_num2 . WRITE: / 'Variable 1 :'. ABAP: Subroutines PERFORM pass USING v_num1 v_num2.com © 2010 SAP AG 9 . SKIP.com | UAC . 2. p_num1 = 100. p_num2 = 111. v_num2 TYPE i. 'Variable 2 :'. FORM pass USING p_num1 TYPE i p_num2 TYPE i. FORM pass USING VALUE(p_num1) TYPE i VALUE(p_num2) TYPE i. "sum On execution it will give the following result. 'Variable 2 :'. WRITE: / 'Variable 1 :'.com | BOC . "sum On execution it will give the following result.v_num1. ENDFORM. 'Variable 2 :'. SAP COMMUNITY NETWORK SDN . Call by Value Consider the following program.com | BPX .sap. v_num2 = 10.sap.bpx.sap. v_num1 = 5.sdn. p_num1 = 100. WRITE: / 'Variable 1 :'.sap.

"sum On execution the following result is obtained. 3. ENDFORM. p_num1 = 100.sap.sap. Since we had specified CHANGING only for the PARAMETER p_num2 Therefore the value of the actual parameter v_num2 also changes.sap.com © 2010 SAP AG 10 . p_num2 = 111.sap.boc. v_num2 = 10.uac. WRITE: / 'Variable 1 :'. v_num1 = 5. WRITE: / 'Variable 1 :'. PERFORM pass USING v_num1 CHANGING v_num2. DATA: v_sum TYPE i.v_num1. v_num2 TYPE i.sdn. 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. SKIP.v_num2.bpx. FORM pass USING VALUE(p_num1) TYPE i CHANGING VALUE(p_num2) TYPE i. Call by Value and Result Consider the following program. SAP COMMUNITY NETWORK SDN .com | BOC . v_num1 TYPE i.com | UAC .com | BPX . 'Variable 2 :'. 'Variable 2 :'.v_num2.v_num1.

com | BPX .com | UAC .boc.sap.sdn. ABAP: Subroutines Related Content Subroutines & their use in SAP Script Passing Internal Table to Subroutine Subroutine Pool SAP COMMUNITY NETWORK SDN .bpx.com © 2010 SAP AG 11 .sap.com | BOC .uac.sap.sap.

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

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