‎2007 Jul 12 1:17 PM
can any one explain me the exact difference between using & changing in perform subroutine with simple example..
thanks in advance...
‎2007 Jul 12 1:20 PM
Hi,
Go through the below links..
http://help.sap.com/saphelp_46c/helpdata/en/9f/db975c35c111d1829f0000e829fbfe/content.htm
http://help.sap.com/saphelp_46c/helpdata/en/9f/db976935c111d1829f0000e829fbfe/content.htm
Regards,
nagaraj
‎2007 Jul 12 1:23 PM
hi,
Subroutines are procedures that you can define in any ABAP program and also call from any program. Subroutines are normally called internally, that is, they contain sections of code or algorithms that are used frequently locally. If you want a function to be reusable throughout the system, use a function module.
A subroutine is a block of code introduced by FORM and concluded by ENDFORM.
FORM <subr> [USING ... [VALUE(]<pi>[)] [TYPE <t>|LIKE <f>]... ]
[CHANGING... [VALUE(]<pi>[)] [TYPE <t>|LIKE <f>]... ].
...
ENDFORM.
<subr> is the name of the subroutine. The optional additions USING and CHANGING define the parameter interface. Like any other processing block, subroutines cannot be nested. You should therefore place your subroutine definitions at the end of the program, especially for executable programs (type 1). In this way, you eliminate the risk of accidentally ending an event block in the wrong place by inserting a FORM...ENDFORM block.
You call subroutines using the statement
PERFORM... [USING ... <pi>... ]
[CHANGING... <pi>... ].
Subroutines can call other subroutines (nested calls) and may also call themselves (recursive calls). Once a subroutine has finished running, the calling program carries on processing after the PERFORM statement. You can use the USING and CHANGING additions to supply values to the parameter interface of the subroutine.
Example
-
PERFORM ADDITION USING 1 2.
PERFORM ADDITION USING 3 4.
FORM ADDITION USING V1 V2.
DATA: V3 TYPE I.
V3 = V1 + V2.
WRITE:/ V1.
ENDFORM.
‎2007 Jul 12 1:23 PM
Hi,
There is no difference when you PASS BY REFERENCE.
The difference is only when you PASS BY VALUE as follows.
Pass by value for USING parameters
For each formal parameter p1 p2 ..., a local object with the same data type as the corresponding actual parameter is created in the subroutine and filled with its values. A change to the formal parameter in the subroutine does not change the value of the actual parameter. The actual parameter also retains its original value even after the subroutine has ended.
Pass by value for CHANGING parameters
For each formal parameter p1 p2 ..., a local data object with the same data type as the corresponding actual parameter is created in the subroutine and filled with its values. A change to the formal parameter in the subroutine does not directly change the value of the actual parameter. If the subroutine is ended using ENDFORM, RETURN, CHECK or EXIT however, the content of the formal parameter is assigned to the actual parameter. If the subroutine is ended by a message or an exception, the actual parameter remains unchanged.
Regards,
Sesh
‎2007 Jul 12 1:37 PM
hi maya,
chk out this example...
DATA: num1 TYPE i,
num2 TYPE i,
sum TYPE i.
num1 = 2. num2 = 4.
PERFORM addit USING num1 num2 CHANGING sum.
num1 = 7. num2 = 11.
PERFORM addit USING num1 num2 CHANGING sum.
FORM addit
USING add_num1 TYPE any
add_num2 TYPE any
CHANGING add_sum TYPE any.
add_sum = add_num1 + add_num2.
PERFORM out USING add_num1 add_num2 add_sum.
ENDFORM.
FORM out
USING out_num1 TYPE any
out_num2 TYPE any
out_sum TYPE any.
WRITE: / 'Sum of', out_num1, 'and', out_num2, 'is', out_sum.
ENDFORM.
thanks
jaideep
*if useful reward points..
‎2007 Jul 12 1:37 PM
HI maya,
1. Usually, using is used
to pass parameters
<b>which are not expected to change inside the routine</b>.
2. Parameters, <b>which are expected to change inside the routine</b>,
are passed with the syntax of <b>CHANGING</b>.
regards,
amit m.
‎2020 Mar 11 10:49 PM
REPORT ZRK_PGM2_SUB_ROUTINES. DATA: AVAR1(100) TYPE C VALUE 'SHELL'. TYPES: BEGIN OF STRUCT_VBAP, VBELN LIKE VBAP-VBELN, POSNR LIKE VBAP-POSNR, END OF STRUCT_VBAP. DATA: IT_VBAP1 TYPE STANDARD TABLE OF STRUCT_VBAP. DATA: WA_VBAP TYPE STRUCT_VBAP. PERFORM SUBROUTINE1 USING AVAR1 . "Pass By Reference USING(It is same as Pass By Reference CHANGING). WRITE:/ 'Actual Parameter Value =', AVAR1. ULINE. PERFORM SUBROUTINE2 USING AVAR1 . "Pass By Reference CHANGING(It is same as Pass By Reference USING). WRITE:/ 'Actual Parameter Value =', AVAR1. ULINE. PERFORM SUBROUTINE3 CHANGING AVAR1. "Pass By Value USING. WRITE:/ 'Actual Parameter Value =', AVAR1. ULINE. PERFORM SUBROUTINE4 CHANGING AVAR1. "Pass By Value CHANGING(We can also use USING). WRITE:/ 'Actual Parameter Value =', AVAR1. PERFORM SUBROUTINE5 TABLES IT_VBAP1[]. "Passing Table Parameter LOOP AT IT_VBAP1 INTO WA_VBAP. WRITE:/ WA_VBAP-VBELN, WA_VBAP-POSNR. ENDLOOP. *----------Pass By Reference USING----------* FORM SUBROUTINE1 USING FVAR1. WRITE:/ 'Start of Pass By Reference USING'. WRITE:/ 'Formal Parameter Value =', FVAR1. FVAR1 = 'SHELL123'. WRITE:/ 'Changed Value =', FVAR1. WRITE:/ 'End of Pass By Reference USING'. ENDFORM. *-------------------------------------------------* *-----------Pass By Reference CHANGING------------* FORM SUBROUTINE2 CHANGING FVAR1. WRITE:/ 'Start of Pass By Reference CHANGING'. WRITE:/ 'Formal Parameter Value =', FVAR1. FVAR1 = 'SHELL123'. WRITE:/ 'Changed Value =', FVAR1. WRITE:/ 'End of Pass By Reference CHANGING'. ENDFORM. *-------------------------------------------------* *-------------Pass By Value USING-----------------* FORM SUBROUTINE3 USING VALUE(FVAR1). WRITE:/ 'Start of Pass By Value USING'. WRITE:/ 'Formal Parameter Value =', FVAR1. FVAR1 = 'SHELL123'. WRITE:/ 'Changed Value =', FVAR1. WRITE:/ 'End of Pass By Value USING'. ENDFORM. *--------------------------------------------------* *-------------Pass By Value CHANGING---------------* FORM SUBROUTINE4 CHANGING VALUE(FVAR1). WRITE:/ 'Start of Pass By Value CHANGING'. WRITE:/ 'Formal Parameter Value =', FVAR1. FVAR1 = 'SHELL123'. WRITE:/ 'Changed Value =', FVAR1. WRITE:/ 'End of Pass By Value CHANGING'. ENDFORM. *--------------------------------------------------* *----------------Table Parameter-------------------* FORM SUBROUTINE5 TABLES IT_VBAP2. SELECT VBELN POSNR FROM VBAP INTO TABLE IT_VBAP2. ENDFORM. *-------------------------------------------------*