- SAP ABAP - Web Dynpro
- SAP ABAP - Business Add-Ins
- SAP ABAP - User Exits
- SAP ABAP - Customer Exits
- SAP ABAP - SAPscripts
- SAP ABAP - Smart Forms
- SAP ABAP - Dialog Programming
- SAP ABAP - Report Programming
- SAP ABAP - Object Events
- SAP ABAP - Interfaces
- SAP ABAP - Encapsulation
- SAP ABAP - Polymorphism
- SAP ABAP - Inheritance
- SAP ABAP - Classes
- SAP ABAP - Objects
- SAP ABAP - Object Orientation
- SAP ABAP - Deleting Internal Tables
- SAP ABAP - Reading Internal Tables
- SAP ABAP - Copying Internal Tables
- ABAP - Populating Internal Tables
- SAP ABAP - Creating Internal Tables
- SAP ABAP - Internal Tables
- SAP ABAP - Native SQL Overview
- SAP ABAP - Open SQL Overview
- SAP ABAP - Include Programs
- SAP ABAP - Function Modules
- SAP ABAP - Macros
- SAP ABAP - Subroutines
- SAP ABAP - Modularization
- SAP ABAP - Lock Objects
- SAP ABAP - Search Help
- SAP ABAP - Views
- SAP ABAP - Structures
- SAP ABAP - Tables
- SAP ABAP - Data Elements
- SAP ABAP - Domains
- SAP ABAP - Dictionary
- SAP ABAP - Exception Handling
- SAP ABAP - Formatting Data
- SAP ABAP - Date & Time
- SAP ABAP - Strings
- SAP ABAP - Decisions
- SAP ABAP - Loop Control
- SAP ABAP - Operators
- SAP ABAP - Constants & Literals
- SAP ABAP - Variables
- SAP ABAP - Data Types
- SAP ABAP - Basic Syntax
- SAP ABAP - Screen Navigation
- SAP ABAP - Environment
- SAP ABAP - Overview
- SAP ABAP - Home
SAP ABAP Useful Resources
- SAP ABAP - Discussion
- SAP ABAP - Useful Resources
- SAP ABAP - Quick Guide
- SAP ABAP - Questions Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SAP ABAP - Subroutines
A subroutine is a reusable section of code. It is a modularization unit within the program where a function is encapsulated in the form of source code. You page out a part of a program to a subroutine to get a better overview of the main program, and to use the corresponding sequence of statements many times as depicted in the following diagram.
We have program X with 3 different source code blocks. Each block has the same ABAP statements. Basically, they are the same code blocks. To make this code easier to maintain, we can encapsulate the code into a subroutine. We can call this subroutine in our programs as many times as we wish. A subroutine can be defined using Form and EndForm statements.
Following is the general syntax of a subroutine definition.
FORM <subroutine_name>. <statements> ENDFORM.
We can call a subroutine by using PERFORM statement. The control jumps to the first executable statement in the subroutine <subroutine_name>. When ENDFORM is encountered, control jumps back to the statement following the PERFORM statement.
Example
Step 1 − Go to transaction SE80. Open the existing program and then right-cpck on program. In this case, it is ZSUBTEST .
Step 2 − Select Create and then select Subroutine. Write the subroutine name in the field and then cpck the continue button. The subroutine name is Sub_Display as shown in the following screenshot.
Step 3 − Write the code in FORM and ENDFORM statement block. The subroutine has been created successfully.
We need to include PERFORM statement to call the subroutine. Let’s take a look at the code −
REPORT ZSUBTEST. PERFORM Sub_Display. * Form Sub_Display * --> p1 text * <-- p2 text FORM Sub_Display. Write: This is Subroutine . Write: / Subroutine created successfully . ENDFORM. " Sub_Display
Step 4 − Save, activate and execute the program. The above code produces the following output −
Subroutine Test: This is Subroutine Subroutine created successfully
Hence, using subroutines makes your program more function-oriented. It sppts the program s task into sub-functions, so that each subroutine is responsible for one subfunction. Your program becomes easier to maintain as changes to functions often only have to be implemented in the subroutine.
Advertisements