SAP ABAP Tutorial on Subroutines. PERFORM FORM ENDFORM

SAP ABAP Tutorial on Subroutines. PERFORM FORM ENDFORM

SAP ABAP Tutorial on Subroutines. PERFORM FORM ENDFORM
A subroutine is a block of code introduced by FORM and concluded by ENDFORM.

FORM <subroutine name> [USING ... [VALUE(]<pi>[)] [TYPE <t>|LIKE <f>]... ] [CHANGING... [VALUE(]<pi>[)] [TYPE <t>|LIKE <f>]... ].
<Subroutine code>
ENDFORM.

In ABAP a subroutine is defined as shown above. All the subroutines start with FORM followed by the subroutine name.

For example.

FORM write_hello.     Write:/ 'Hello'. ENDFORM.

A subroutine is called by a PERFORM statement as follows.

PERFORM write_hello.

In the above example no parameters pased to the subroutine. We will now see one more example of a simple subroutine that passes parameters. Now if you want to add two numbers. we need to write the following code.

Data: d_sum type i,
d_num1 type i,
d_num2 type i.


d_num1 = 5.
d_num2 = 10.

perform addnumbers using d_num1
d_num2 .


write:/ d_sum, d_num1, d_num2.

*---------------------------------------------------------------------*
* FORM addnumbers *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
* --> NUM1 *
* --> NUM2 *
*---------------------------------------------------------------------*
form addnumbers using num1 num2.
d_sum = num1 + num2.
endform.

For the above code the output would be 15, 5, 10 because the values of the parameters would be as follows.

d_sum = 15,
d_num1 = 5,
d_num2 = 10.

This is because we are passing the parameters by REFERENCE and not by VALUE.

Important: For calling by reference, USING and CHANGING are equivalent.

This means that the value of the actual parameters changes if the value of the formal parameters changes. We will write some code
to demonstrate this point.

Data: d_sum  type i,
      d_num1 type i,
      d_num2 type i.


d_num1 = 5.
d_num2 = 10.


perform pass_ref using d_num1
                       d_num2.

write:/ d_sum, d_num1, d_num2.

*&---------------------------------------------------------------------*
*&      Form  pass_ref
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_D_NUM1  text
*      -->P_D_NUM2  text
*----------------------------------------------------------------------*
FORM pass_ref USING    P_D_NUM1
                       P_D_NUM2.

   P_D_NUM1 = 100.
   P_D_NUM2 = 200.

ENDFORM.                    " pass_ref


The output of the above program would be 15, 100, 200. This is because the value of actual parameters is changed because the value of the formal parameters also changes.

We will now see how to pass parameters by value. To pass parameters by value we need to explicitly state it in the subroutine. When parameters are passed by VALUE the value of the actual parameters does not change, even if the value of the formal parameters changes.

Sample code.

*&---------------------------------------------------------------------*
*&      Form  pass_val
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*     
*      -->P_D_NUM1  text
*     
*      -->P_D_NUM2  text
*----------------------------------------------------------------------*
FORM pass_val USING  VALUE(P_D_NUM1)
                     VALUE(P_D_NUM2).

 P_D_NUM1 = 1000.
 P_D_NUM2 = 2000.

ENDFORM.                    " pass_val

Please find the code below that demonstrates this concept.

Data: d_sum  type i,
      d_num1 type i,
      d_num2 type i.


d_num1 = 5.
d_num2 = 10.


perform pass_val using d_num1
                                     d_num2.

write:/ d_sum, d_num1, d_num2.

*&---------------------------------------------------------------------*
*&      Form  pass_val
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*     
*      -->P_D_NUM1  text
*     
*      -->P_D_NUM2  text
*----------------------------------------------------------------------*
FORM pass_val USING  VALUE(P_D_NUM1)
                     VALUE(P_D_NUM2).

 P_D_NUM1 = 1000.
 P_D_NUM2 = 2000.

ENDFORM.                    " pass_val

The output of the above program is 15, 5, 10.

We will now see the effect of CHANGING when the PARAMETERS are passed by VALUE. If CHANGING is specified in PARAMETERS that are passed by value then the value of the ACTUAL Parameters also changes. Please see the sample code below.


Data: d_sum  type i,
      d_num1 type i,
      d_num2 type i.


d_num1 = 5.
d_num2 = 10.

perform pass_val_chcg  using d_num1
                                               d_num2.

write:/ d_sum, d_num1, d_num2.

*&---------------------------------------------------------------------*
*&      Form  pass_val_chcg
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_D_NUM1  text
*      -->P_D_NUM2  text
*----------------------------------------------------------------------*
FORM pass_val_chcg USING      VALUE(P_D_NUM1)
                            CHANGING   VALUE(P_D_NUM2).

 P_D_NUM1 = 1001.
 P_D_NUM2 = 2002.

ENDFORM.                    " pass_val_chcg

The output of the above program would be as follows.

15, 5, 2,002

This is because we have specified CHANGING only for the PARAMETER P_D_NUM2 and hence the actual Parameter d_num2 also changes.
If the subroutine concludes successfully, that is, when the ENDFORM statement occurs, or when the subroutine is terminated through a CHECK or EXIT statement, the current value of the formal parameter is copied into the actual parameter.
If the subroutine terminates prematurely due to an error message, no value is passed.

 
Copyright © happiness synchronized. Original Concept and Design by My Blogger Themes | Tested by Blogger Templates | Best Credit Cards