Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

difference between using & changing in perform subroutine

Former Member
0 Likes
6,600

can any one explain me the exact difference between using & changing in perform subroutine with simple example..

thanks in advance...

6 REPLIES 6
Read only

Former Member
0 Likes
2,588

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.

Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
2,588

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

Read only

jaideeps
Product and Topic Expert
Product and Topic Expert
0 Likes
2,588

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

Read only

Former Member
0 Likes
2,588

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.

Read only

0 Likes
2,588

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