Summary -
In this topic, we described about the below sections -
What is Subroutines -
Subroutines are procedures that can define in any program and call from any ABAP program. Subroutines normally contains sections code or algorithms.
Subroutines can be defined anywhere in the program and no restriction on where to define. Subroutines can be defined using FORM and ENDFORM statements.
PERFORM statement used to call the subroutine. PERFORM and FORM must contain the same number of parameters.
Let us take an example program Y and below diagram explains how the program Y look like before and after subroutine implementation -
Program Y has the same statements block in three places which is redundant code. It also increases the complexity of the program.
The below diagram shows the same program after subroutines implementation -
In the above diagram, subroutine-1 is the subroutine definition and the code block is replaced with calling subroutine-1 in all the places.
Subroutine Types -
Subroutines are classified into two types based on how they are used. Those are -
- Internal Subroutine
- External Subroutine
Internal Subroutines -
Internal subroutines used in the same program where it is defined. These subroutines can't available to the other programs. Internal subroutine can't be nested.
Syntax -
Definition subroutine -
FORM <subroutine-name> [USING ... [VALUE(]<parm1>[)]
[TYPE <data-type>|LIKE <field>]... ]
[CHANGING... [VALUE(]<parm1>[)]
[TYPE <data-type>|LIKE <field>]... ].
.
Statements
.
ENDFORM.
Calling subroutine -
PERFORM <subroutine-name> [USING ... <parm1>... ]
[CHANGING... <parm1>... ].
- Subroutine-name - Specifies the subroutine name.
- <parm1>… - Specifies the parameters being passed. Once the PERFORM statement executes, control transfers to the subroutine. Control returns to the next statement immediate to PERFORM once subroutine execution completed.
External Subroutines -
External subroutines defined globally. This type of subroutines can be used by other programs. External subroutines can be nested.
Syntax -
Definition subroutine -
FORM <subroutine-name> [USING ... [VALUE(]<parm1>[)]
[TYPE <data-type>|LIKE <field>]... ]
[CHANGING... [VALUE(]<parm1>[)]
[TYPE <data-type>|LIKE <field>]... ].
.
Statements
.
ENDFORM.
Calling subroutine -
PERFORM (<fsubr>)[IN PROGRAM (<program-name>)][USING ... <pi>... ]
[CHANGING... <pi>... ]
[IF FOUND].
- Subroutine-name - Specifies the subroutine name. IF FOUND option can prevent a runtime error from being triggered if <program-name> does not contain a subroutine with the name <subroutine-name>. Once the PERFORM statement executes, control transfers to the subroutine. Control returns to the next statement immediate to PERFORM once subroutine execution completed.
Variables in Subroutine -
There are two types of variables used in subroutines. Those are -
Local variables | Global variables |
---|---|
A variable that define within the subroutine. | A variable that define outside of the subroutine. |
It is said to be local to the subroutine. | It is said to be global to the subroutine. |
These variables can't be used outside the subroutine. | These variables can be used outside the subroutine and within the program. |
Local variable life time is until the end of subroutine execution. | Global variable life time is until the end of program execution. |
Parameters in Subroutine -
There are two types of parameters in subroutines. Those are -
Actual parameters | Formal parameters |
---|---|
Parameters that appear on the PERFORM statement are called actual parameters. | Parameters that appear on the FORM statement are called formal parameters. |
For Example - PERFORM s1 p1, p2, p3 | For Example - FORM s1 p1, p2, p3 |
p1, p2 and p3 are the actual parameters. | p1, p2 and p3 are the formal parameters. |
Passing parameters to subroutine -
There are three ways of passing parameters to a subroutine. Those are -
- Pass by reference
- Pass by value
- Pass by value and result
Method | Description |
---|---|
By reference | Passes the pointer of the original memory location where the value stored. Most efficient method. Value changes in the subroutine are reflected in the calling program. |
By value | Only value passed to the subroutine. Allocates new temporary memory location within the subroutine for use. The memory is freed when the subroutine ends. Value changes are not reflected in the calling program. |
By value and result | Like pass by value, the contents of the new temporary memory copied back into the original memory before returning. Allows changes and allows rollback. |
Subroutines statements addition -
Below are the list of additions and the corresponding methods refer to -
Addition | Method |
---|---|
using v1 | Pass by reference |
changing v1 | Pass by reference |
using value(v1) | Pass by value |
changing value(v1) | Pass by value and result |
Example -
Simple example to create one program with subroutine.
Step-1: Create the main program Z_SUBRMAIN with the below code to perform subroutine named sub_routine.
REPORT Z_SUBRMAIN.
Write 'Before calling subroutine..'.
PERFORM sub_routine.
Write /'After subroutine called....'.
Step-2: In this case, Z_SUBRMAIN is the main program. Go to SE80 transaction, select the program and right click on the program to create subroutine (Create Subroutine).
Step-3: Enter the subroutine name in the below screen, select the New Include Z_SUBRMAIN and click on Continue icon.
Step-4: Now, new subroutine sub_routine code included in the Z_SUBMAIN program like below.
REPORT Z_SUBRMAIN.
Write 'Before calling subroutine..'.
PERFORM sub_routine.
Write /'After subroutine called....'.
*&---------------------------------------------------------------------*
*& Form sub_routine
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM sub_routine .
ENDFORM.
Step-5: Add the subroutine code in between FORM and ENDFORM. Complete the coding and activate the program to execute.
REPORT Z_SUBRMAIN.
Write 'Before calling subroutine..'.
PERFORM sub_routine.
Write /'After subroutine called....'.
*&---------------------------------------------------------------------*
*& Form sub_routine
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM sub_routine .
Write /'Subroutine executed...'.
ENDFORM.
Output -
Explaining Example -
In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.
In the example, we have added a subroutine sub_routine into the program Z_SUBRMAIN.