Software Development Exam  >  Software Development Notes  >  Learn and Master SAP ABAP  >  SAP ABAP - Subroutines

SAP ABAP - Subroutines | Learn and Master SAP ABAP - Software Development PDF Download

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.

SAP ABAP - Subroutines | Learn and Master SAP ABAP - Software Development

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-click 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 click the continue button. The subroutine name is 'Sub_Display' as shown in the following screenshot.

SAP ABAP - Subroutines | Learn and Master SAP ABAP - Software Development

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

The document SAP ABAP - Subroutines | Learn and Master SAP ABAP - Software Development is a part of the Software Development Course Learn and Master SAP ABAP.
All you need of Software Development at this link: Software Development
73 videos|68 docs

Top Courses for Software Development

73 videos|68 docs
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

past year papers

,

Viva Questions

,

MCQs

,

practice quizzes

,

Summary

,

Semester Notes

,

study material

,

SAP ABAP - Subroutines | Learn and Master SAP ABAP - Software Development

,

Important questions

,

shortcuts and tricks

,

pdf

,

Objective type Questions

,

Sample Paper

,

video lectures

,

Previous Year Questions with Solutions

,

Exam

,

SAP ABAP - Subroutines | Learn and Master SAP ABAP - Software Development

,

Extra Questions

,

SAP ABAP - Subroutines | Learn and Master SAP ABAP - Software Development

,

Free

,

mock tests for examination

,

ppt

;