Preface – This post is part of the ABAP Beginner series.
Table of Contents
Introduction
A good coder always tries to reduce repetitive codes and implements codes in such way that it can be reused. Subroutine in SAP ABAP provides us the same functionality. In this article we will learn what it is and how to implement it in SAP ABAP.
What are Sub Routine
A Subroutine in SAP ABAP is a well-defined reusable procedures used for code modularisation. Subroutines can be used locally in a program.
It is written between FORM and ENDFORM.
PERFORM subroutine is used to invoke subroutine.
Passing values with Subroutines
There are 2 ways to pass values with subroutines:
Pass By Value
- In this the actual and formal parameter refers to different memory.
- IF the value of formal parameter is changed then the value of actual parameter won’t change.
- Keyword VALUE identifies that the parameter is passed by value.
Pass by Reference
- In this the actual and formal parameters refer to same memory.
- If the formal parameter is changed, actual parameter also changes.
Types of Subroutines
Internal Subroutines
Subroutines that are defined in the same program and called within the same program.
External Subroutines
Subroutines that are defined in a program and are called in a different program are called as external subroutines.
How to create a are Sub Routine Module in SAP
- Go to SE80
- Select your program for which you want to create a subroutine -> select -> create a subroutine (name it).
- Write a code in FORM and ENFORM and if u want to call it as an internal subroutine then PERFORM has to be included.
Calling a Subroutine
Internal Subroutines
REPORT PROGRAM NAME.
PERFORM SUBROUTINE NAME.
FORM SUBROUTINE NAME.
P1 TEXT
FORM SUBROUTINE NAME.
Write: ‘This is internal subroutine’.
ENDFORM.
External Subroutines
FORM FORMNAME.
Select *from MARA into table it_mara where matnr is s_matnr.
ENDFORM.
To call this form in a program.
PERFORM subroutine_name IN PROGRAM program_name.
Advantages of Sub Routine
- Pass by value allocates new memory location for use in subroutines
- Pass by reference passes a pointer to a memory location.
0 Comments