ABAP Subroutine basics in SAP

Sub routines

Basic ABAP code for calling a subroutine is as follows

DATA: w_var type s_carrid.
Perform sub_routine.
FORM sub_routine.
  DATA: w_var TYPE s_carrid.

Pass by reference

PERFORM read_data USING d_carrid1.
FORM read_data USING local_carrid.
* local_carrid points to d_carrid. When you change local_carrid you also change d_carrid1.

Note: When passing by reference the statements USING/CHANGING do not alter the functionality, you can change values with either statement. Just makes it easier to read if you use the appropriate statement.

Pass by VALUE

PERFORM read_data Changing d_carrid1.
FORM read_data CHANGING VALUE(local_carrid)

*Using the VALUE command creates a local variable called 'local_carrid' which contains the value passed to it. This can then be changed within the local subroutine with no effect on the passing variable(d_carrid). Once the program reaches the ENDFORM statement the passing variable (d_carrid) is updated). If the exit command is execurted before the ENDFORM the value is not updated(must reach ENDFORM).

FORM read_data USING VALUE(local_carrid).

If you use the USING command instead of CHANGING the global variable will not be updated even at the end of the sub routine(ENDFORM).

Passing tables using the TABLES statement

PERFORM sub_routine TABLES itab. 	"Itab must be a standard  table

*note: You can pass tables without using the TABLES statement. Simply use USING/CHANGING.

Related Articles

Beginners Guide to learning SAP development starting with logging into SAP
ABAP Programming EVENTS in SAP
ABAP Function Module basics in SAP
DATA and @DATA Inline ABAP declarations available from release 7.40 to help make your code cleaner and more readable
ABAP Workbench Programming Techniques - BC402
ABAP rules to consider before creating a bespoke abap report or program
Get access to an SAP system for individual needs
SAP Base 64 encoding and decoding using ABAP code
Call web URl from ABAP report or SAP help documentation
Direct download, downloading sap objects
SAP Icons
SAP icons and some ABAP code to display them
SAP icons list and their associated codes
Internal Program Environment diplays all internal/external objects and operations used by an SAP program
minisap installation on your local pc to allow ABAP development, your own local SE80
SAP minisap installation on your local pc to allow ABAP development for free
SAP module based information such FI, HR, MM, PM, BW etc
Need an SAP ABAP program created?
SAP repository objects - List of useful standard and bespoke SAP repository objects
Retrieve SAP objects using transport entry in SE10 to restore objects that have been deleted
SAP Help for all areas for SAP ABAP Development inc ABAP, ALV, Web dynpro, bsp, HR, BW
ABAP tutorial - Logging into an SAP system for the first time
Manage and delete SAP sessions using ABAP code
SAP module areas
Increase & Decrease size of SAP editor text
ABAP development information and code examples for producing be-spoke SAP functionality
ABAP Development Info - Example code and information on various areas of ABAP development
SAP command field entries - box in top left corner
Force new page when printing abap source code
ABAP FIELD SYMBOL - Techniques for manupulating data using the FIELD-SYMBOL statement
Hiding ABAP Source Code so that it can not be viewed by anyone
ABAP Internal table declaration - Various methods of creating internal data structures and tables
Parameter ID - ABAP code to demonstrate how to set and get a parameter ID
RANGE statement - Example ABAP code to demonstrate the RANGE command
Change SAP logo in top right hand corner of SAP client
ABAP SELECT statement within SAP - Example ABAP code to demonstrate the SELECT command
Create desktop Shortcut to SAP function
SAP Note assistant - Using transaction SNOTE to read and apply OSS note fix
VARYING command - Example ABAP code to demonstrate the VARYING command
Creating your first helloworld ABAP report in SAP