2008 Jun 17 10:41 AM
Can any one explain me the diff between call by value and call by reference in Subroutines.
ex :
PERFORM ADD_Z1CLASS1 USING LW_MARA-MATNR
CHANGING IDOC_DATA[].
FORM ADD_Z1CLASS1 USING PV_MATNR TYPE MATNR
CHANGING PT_EDIDD TYPE EDIDD_TT.
Please let me know, if the above code is call by value or call by reference...?
Any suggestions will be appreciated!
Regards,
Kittu
2008 Jun 17 10:52 AM
Hiii
I think its call by referenc ebecause u r trying to change the value at the same memory location ..by specifying CHANGING....
Call By Value:
Creates a new memory loaction for use within the
subroutine.The memory is freed once it leaves the
subroutine.Changes made to the variable are not affected
outside the subroutine.
Call By Reference:
Passes a pointer to the memory location.Changes made to the
variable within the subroutine affects the variable outside
the subroutine.
regards
chandu reddy
Can any one explain me the diff between call by value and call by reference in Subroutines.
ex :
PERFORM ADD_Z1CLASS1 USING LW_MARA-MATNR
CHANGING IDOC_DATA[].
FORM ADD_Z1CLASS1 USING PV_MATNR TYPE MATNR
CHANGING PT_EDIDD TYPE EDIDD_TT.
Please let me know, if the above code is call by value or call by reference...?
Any suggestions will be appreciated!
Regards,
Kittu
2008 Jun 17 10:52 AM
Hiii
I think its call by referenc ebecause u r trying to change the value at the same memory location ..by specifying CHANGING....
Call By Value:
Creates a new memory loaction for use within the
subroutine.The memory is freed once it leaves the
subroutine.Changes made to the variable are not affected
outside the subroutine.
Call By Reference:
Passes a pointer to the memory location.Changes made to the
variable within the subroutine affects the variable outside
the subroutine.
regards
chandu reddy
2014 Apr 24 11:15 AM
Hi all,
I am new to abap programming
when we say changes made to formal/actual parameter does not affect to formal/actual parameter, what exactly meant by "changes" ?
Regards,
Shiva Sahu
2008 Jun 17 10:57 AM
hi,
using -- Pass by reference
changing -- Pass by reference
using value() -- Pass by value
changing value( )-- Pass by value and result
The main difference between the pass by value and pass by reference is that " In pass by value the actual parameters are not changed while in pass by reference those are changed".
By reference Passes a pointer to the original memory location. Very efficient
By value Allocates a new memory location for use within the subroutine. The memory is freed when the subroutine ends. Prevents changes to passed variable
By value and result Similar to pass by value, but the contents of the new memory is copied back into the original memory before returning. Allows changes and allows a rollback .
Go through the following eg:
pass by ref:
data f1 value 'A'.
perform s1 using f1.
write / f1.
form s1 using p1.
p1 = 'X'.
endform.
o/p is X
using and changing Are Identical in Function
report ztx1805.
data: f1 value 'A',
f2 value 'B'.
write: / f1, f2.
perform s1 using f1
changing f2.
write: / f1, f2.
form s1 using p1
changing p2.
p1 = p2 = 'X'.
endform.
o/p : A B
X X
Pass by value:
data: f1 value 'A'.
perform s1 using f1.
write / f1.
form s1 using value(p1).
p1 = 'X'.
write / p1.
endform.
o/p is X
A
Hope this is clear.
so the code which you have pasted comes under pass by reference.
Reward points if helpful.
Thanks and regards.
2008 Jun 17 11:06 AM
pass by reference
examples
pass by reference
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.
the changes made in form will be reflected immediatly to the memory location,because actual parameters(parameters with perform statement) and formal parameters(parameters with FORM) are pointing to same memory location
Pass by value:
DATA: num TYPE i VALUE 5,
fac TYPE i VALUE 0.
PERFORM fact USING num CHANGING fac.
WRITE: / 'Factorial of', num, 'is', fac. "#EC NOTEXT
FORM fact
USING value(f_num) TYPE i
CHANGING f_fact TYPE i.
f_fact = 1.
WHILE f_num GE 1.
f_fact = f_fact * f_num.
f_num = f_num - 1.
ENDWHILE.
Here actual parameters and formal parameters are pointing to different memory location
Regds
Murali
2008 Jun 17 11:10 AM
Hi kittu,
the first field PV_MATNR TYPE MATNR and second field PT_EDIDD TYPE EDIDD_TT in the form statement are call by reference.
if u want to use call by value use the addition 'VALUE'
USING VALUE(PV_MATNR)
CHANGING VALUE(PT_EDIDD ).
There is no difference for USING and CHANGING.
the only difference lies with the value addition.
if ur using USING VALUE ADDITION the original parameter values are not changed when u have used CHANGING VALUE ADDITON the original parameter values are effected only the FORM is successfully executed.
Reward if helpful,
Regards,
S.Gangi reddy.
2008 Jun 17 11:12 AM
Fields are passed by Value and Internal Tables are always passed by reference.
See the SAP documentation.
go to abaphelp tcode
2008 Jun 18 9:45 AM
Hello,
Thank you very much for your response!
The info that you had provided was really helpful.
I appreciate it.
Have a good day!
Regards,
Kittu
2014 Apr 24 11:21 AM
Dear Krishna,
see given link:
Passing Parameters to Subroutines - ABAP Programming (BC-ABA) - SAP Library
Regards,
Abbas.
2014 Apr 24 11:28 AM
Hi Syed,
I have already gone through to this link but still i am not able to find the meaning of "changes made to formal parameter can affect to actual parameter".
what kind of changes can be made to formal parameter and how it can affect to formal parameter. please brief me with an example
Regards,
Shiva Sahu