‎2007 Jul 19 4:48 PM
Hi all,
Please check the two codes and tell me what wrong in the first code.
This both codes are from SDN experts
data : num1 type i ,
num2 type i.
num1 = 7.
num2 = 10.
perform find_sum using num1 num2.
write / : num1 , 'out put will be 7 itself'.
*********changing**********
perform find_sum1 changing num1 num2.
write / : num1 , 'out put will be 17 '.
form find_sum using p_num1 type i
p_num2 type i.
p_num1 = p_num1 + p_num2.
endform. " find_sum
form find_sum1 changing p_num1 type i
p_num2 type i.
p_num1 = p_num1 + p_num2.
endform. " find_sum
This is what i see an output,
17 out put will be 7 itself
27 out put will be 17
ACTUAL IT SHOULD BE LIKE THIS RIGHT
7 out put will be 7 itself
17 out put will be 17
but when i do this
DATA : num1 TYPE i ,
num2 TYPE i.
num1 = 7.
num2 = 10.
PERFORM find_sum USING num1 num2 .
WRITE / : num1 , 'out put will be 7 itself'.
*********changing**********
PERFORM find_sum1 CHANGING num1 num2.
WRITE / : num1 , 'out put will be 17 '.
&----
*& Form find_sum1
&----
text
----
<--P_NUM1 text
<--P_NUM2 text
----
FORM find_sum1 CHANGING p_num1 TYPE i
p_num2 TYPE i.
p_num1 = p_num1 + p_num2.
ENDFORM. " find_sum1
&----
*& Form find_sum
&----
text
----
-->P_NUM1 text
-->P_NUM2 text
----
FORM find_sum using value(p_num1) p_num2 .
p_num1 = p_num1 + p_num2.
ENDFORM. " find_sum
it gives the result what iam looking for and it in accordance to definiation of the using and changing
Here the o/p his
7 out put will be 7 itself
17 out put will be 17
My question is what wrong with the first code
Thanks again
‎2007 Jul 19 4:57 PM
Hi Kajol,
The first one is working with pass by reference concept.. the content of the variable that is passed will get change..
<b> Num1 ur passing by reference and in the subroutine u say p_num1 = p_num1 + p_num2.. here the value is getting changed for the num1 because of reference.. when you say by reference it means same as a pointer concept in C or C++ language..</b>
the second one is working with pass by value concept.. thought ur chainge the num1 in the subroutine it will not be effected in the actual variable.
<b> Here i say only take the value into consideration the value is copied into the variable p_num1 .. and if i change the value p_num1 in the subroutine.. it is only effectinve within the subroutine and will not change the value of the actual variable num1</b>
Notice the difference in the two routines in <b>BOLD</b> letters
form find_sum <b>using p_num1</b> type i
p_num2 type i.
p_num1 = p_num1 + p_num2.
endform. " find_sum
&----
*& Form find_sum
&----
text
----
-->P_NUM1 text
-->P_NUM2 text
----
FORM find_sum <b>using</b> <b>value(p_num1)</b> p_num2 .
p_num1 = p_num1 + p_num2.
ENDFORM. " find_sum
Thanks
Mahesh
Message was edited by:
Mahesh Raganmoni
‎2007 Jul 19 4:52 PM
using Perform find_sum...
u are adding 10 + 7 and storing the result in p_num1 as 17.
and perform find-sum1
17 + 10 ==> 27
either u clear the variables or use local variales.
‎2007 Jul 19 4:55 PM
Hi,
I thought that by using "USING" in perform statment won't chnge the values outsidde the form
while using" CHANGE" changes the values out the form as well
Let me know whether iam right?
Thats the reason i am thing the output should be 7 and 17
Thanks
‎2007 Jul 19 4:57 PM
Hi Kajol,
The first one is working with pass by reference concept.. the content of the variable that is passed will get change..
<b> Num1 ur passing by reference and in the subroutine u say p_num1 = p_num1 + p_num2.. here the value is getting changed for the num1 because of reference.. when you say by reference it means same as a pointer concept in C or C++ language..</b>
the second one is working with pass by value concept.. thought ur chainge the num1 in the subroutine it will not be effected in the actual variable.
<b> Here i say only take the value into consideration the value is copied into the variable p_num1 .. and if i change the value p_num1 in the subroutine.. it is only effectinve within the subroutine and will not change the value of the actual variable num1</b>
Notice the difference in the two routines in <b>BOLD</b> letters
form find_sum <b>using p_num1</b> type i
p_num2 type i.
p_num1 = p_num1 + p_num2.
endform. " find_sum
&----
*& Form find_sum
&----
text
----
-->P_NUM1 text
-->P_NUM2 text
----
FORM find_sum <b>using</b> <b>value(p_num1)</b> p_num2 .
p_num1 = p_num1 + p_num2.
ENDFORM. " find_sum
Thanks
Mahesh
Message was edited by:
Mahesh Raganmoni
‎2007 Jul 19 4:59 PM
Hi,
data : num1 type i ,
num2 type i.
num1 = 7.
num2 = 10.
perform find_sum using num1 num2.
write / : num1 , 'out put will be 7 itself'.
*********changing**********
perform find_sum1 <b>using</b> num1 num2.
write / : num1 , 'out put will be 17 '.
form find_sum using p_num1 type i
p_num2 type i.
p_num1 = p_num1 + p_num2.
endform. " find_sum
form find_sum1 changing p_num1 type i
p_num2 type i.
p_num1 = p_num1 + p_num2.
endform. " find_sum
This is what i see an output,
17 out put will be 7 itself
27 out put will be 17
The bold part is the correction.
pls reward points.
Regards,
Ameet