2008 Mar 31 1:40 PM
can u give example of call by value and return.
give example
2008 Mar 31 1:42 PM
2008 Mar 31 2:54 PM
Hi Pranouti,,,,,,,
Check this simple example where u can get a clear idea on the difference ,,,,,,,,,,,
The arguments passed to function can be of two types
1. Values passed
2. Address passed
The first type refers to call by value and the second type refers to call by reference.
For instance consider program1
main()
{
int x=50, y=70;
interchange(x,y);
printf(x=%d y=%d,x,y);
}
interchange(x1,y1)
int x1,y1;
{
int z1;
z1=x1;
x1=y1;
y1=z1;
printf(x1=%d y1=%d,x1,y1);
}
Here the value to function interchange is passed by value.
Consider program2
main()
{
int x=50, y=70;
interchange(&x,&y);
printf(x=%d y=%d,x,y);
}
interchange(x1,y1)
int x1,y1;
{
int z1;
z1=*x1;
x1=y1;
*y1=z1;
printf(*x=%d *y=%d,x1,y1);
}
Here the function is called by reference. In other words address is passed by using symbol & and the value is accessed by using symbol *.
The main difference between them can be seen by analyzing the output of program1 and program2.
The output of program1 that is call by value is
x1=70 y1=50
x=50 y=70
But the output of program2 that is call by reference is
*x=70 *y=50
x=70 y=50
This is because in case of call by value the value is passed to function named as interchange and there the value got interchanged and got printed as
x1=70 y1=50
and again since no values are returned back and therefore original values of x and y as in main function namely
x=50 y=70 got printed.
But in case of call by reference address of the variable got passed and therefore what ever changes that happened in function interchange got reflected in the address location and therefore the got reflected in original function call in main also without explicit return value. So value got printed as *x=70 *y=50 and x=70 y=50
Plaese refer the link,,,,,,,,,,
http://www.geekinterview.com/kb/what-is-difference-between-call-by-value.html
Please reward if found helpful,,,,,,,,,,,,,,,,,
Thanks & Regards,,,,,,
Sreekar.Kadiri,...
2008 Mar 31 8:01 PM
hai
pranouthi,
There are two types to pass parameters.These are
Pass-by-value -> The value of actual parameter is passed to the formal parameter in called function.
There is no effect on actual parameter value by changing the value of formal parameter in called function.
(ii) Pass-by-Reference -> The address of actual parameter is passed to the formal parameter in called function.So,the change in the value of formal parameter is reflected in actual parameter.
This will be explained in the below examples.
(I) Example for PASS-BY-VALUE.
PROGRAM PASS_BY_VALUE_TEST.
DATA: W_NUM1 TYPE I,
W_NUM2 TYPE I,
W_SUM TYPE I.
W_NUM1 = 12. W_NUM2 = 4.
PERFORM ADD USING W_NUM1 W_NUM2 CHANGING W_SUM.
W_NUM1 = 7. W_NUM2 = 1.
PERFORM ADD USING W_NUM1 W_NUM2 CHANGING W_SUM.
FORM ADD
USING ADD_NUM1
ADD_NUM2
CHANGING ADD_SUM.
ADD_SUM = ADD_NUM1 + ADD_NUM2.
PERFORM OUT USING ADD_NUM1 ADD_NUM2 ADD_SUM.
ENDFORM.
FORM OUT
USING OUT_NUM1
OUT_NUM2
OUT_SUM.
WRITE: / 'Sum of', OUT_NUM1, 'and', OUT_NUM2, 'is', OUT_SUM.
ENDFORM.
The produces the following output:
Sum of 12 and 4 is 16
Sum of 7 and 1 is 8
(II) Example for PASS-BY-REFERENCE
PROGRAM PASS_BY_REF_TEST.
DATA: W_NUM TYPE I VALUE 6,
W_FACT TYPE I VALUE 0.
PERFORM FACT USING W_NUM CHANGING W_FACT.
WRITE: / 'Factorial of', W_NUM, 'is', W_FACT.
FORM FACT
USING VALUE(F_NUM)
CHANGING F_FACT.
F_FACT = 1.
WHILE F_NUM GE 1.
F_FACT = F_FACT * F_NUM.
F_NUM = F_NUM - 1.
ENDWHILE.
ENDFORM.
The produces the following output:
Factorial of 6 is 720
Here , changing is working as pass-by-reference.so, the value of w_fact is changing and reflected the
calculated factorial value 720.
if useful, reward points.
Thank you
G.V.K.Prasad
Edited by: PRASAD GVK on Mar 31, 2008 9:01 PM
2008 Mar 31 8:24 PM
Hi ,
Please go through the following link for a better understanding.
[For parameter interfaces|http://help.sap.com/saphelp_nw70/helpdata/en/9f/db984635c111d1829f0000e829fbfe/content.htm]
Reward points if helpful.
Thanks and Regards.
2008 Mar 31 8:29 PM
hi,
You list these parameters after USING or CHANGING without the VALUEaddition:
FORM subr USING p1 [{TYPE type}|{LIKE field}]
p2 [{TYPE type}|{LIKE field}]
...
CHANGING p1 [{TYPE type}|{LIKE field}]
p2 [{TYPE type}|{LIKE field}]
...
The formal parameter has no memory of its own. During a subroutine call, only the address of the actual parameter is transferred to the formal parameter. The subroutine works with the field from the calling program. If the value of the formal parameter changes, the contents of the actual parameter in the calling program also change.
For calling by reference, USING and CHANGING are equivalent. For documentation purposes, you should use USING for input parameters which are not changed in the subroutine, and CHANGING for output parameters which are changed in the subroutine.
To avoid the value of an actual parameter being changed automatically, you must pass it by value.
example:data : a type i, b type i.
a = 10.
b = 20.
write 😕 a.
perform call_by_value changing a.
write 😕 a.
perform call_by_ref using a.
write 😕 a.
perform call_by_val_ref changing a b.
write 😕 a , b.
&----
*& Form call_by_value
&----
text
-
-->P_A text
-
form call_by_value using value(a) .
a = a + 10.
endform. " call_by_value
&----
*& Form call_by_ref
&----
text
-
-->P_A text
-
form call_by_ref using a.
a = a + 10.
endform. " call_by_ref
&----
*& Form call_by_val_ref
&----
text
-
<--P_A text
<--P_B text
-
form call_by_val_ref changing a
value(b).
a = a + 10.
b = b + 10.
endform. " call_by_val_ref
Regards
reward if usefull
2008 Mar 31 8:30 PM
I am giving you two programs for PASS BY VALUE & PASS BY VALUE AND RESULT
Please Award points if useful
report zc_call_by_value_01.
**Pass by value
data : a type i,
b type i.
a = 5.
b = 10.
write : / a, b.
perform add using a b.
*perform add changing a b.
*using and changing are both pass by reference
a , b are actual parameters
write : / a , b.
&----
*& Form add
&----
form add using p_a type i
p_b type i.
p_a , p_b are formal parameters
a = a + 5.
b = b + 5.
write : / a , b.
endform. " add
Pass by value and result
REPORT ZC_CALL_BY_VALUE_RESULT_01.
*Pass by value and result
data : a type i,
b type i.
a = 5.
b = 10.
write : / a, b.
perform add changing a b.
write : / a , b.
&----
*& Form add
&----
form add changing value(p_a) type i
value(p_b) type i.
a = a + 5.
b = b + 5.
write : / a , b.
endform. " add
Edited by: ravee indra on Mar 31, 2008 9:31 PM
2008 Mar 31 10:26 PM
hi,
Declaring and Calling Methods
GET REFERENCE
This section contains explains how to work with methods in ABAP Objects. For precise details of the relevant ABAP statements, refer to the corresponding keyword documentation in the ABAP Editor. The example shows how to declare, implement, and call methods.
Declaring Methods
You can declare methods in the declaration part of a class or in an interface. To declare instance methods, use the following statement:
METHODS <meth> IMPORTING.. [VALUE(]<ii>[)] TYPE type [OPTIONAL]..
EXPORTING.. [VALUE(]<ei>[)] TYPE type [OPTIONAL]..
CHANGING.. [VALUE(]<ci>[)] TYPE type [OPTIONAL]..
RETURNING VALUE(<r>)
EXCEPTIONS.. <ei>..
and the appropriate additions.
To declare static methods, use the following statement:
CLASS-METHODS <meth>...
Both statements have the same syntax.
When you declare a method, you also define its parameter interface using the additions IMPORTING, EXPORTING, CHANGING, and RETURNING. The additions define the input, output, and input/output parameters, and the return code. They also define the attributes of the interface parameters, namely whether a parameter is to be passed by reference or value (VALUE), its type (TYPE), and whether it is optional (OPTIONAL, DEFAULT). Unlike in function modules, the default way of passing a parameter in a method is by reference. To pass a parameter by value, you must do so explicitly using the VALUE addition. The return value (RETURNING parameter) must always be passed explicitly as a value. This is suitable for methods that return a single output value. If you use it, you cannot use EXPORTING or CHANGING parameters.
As in function modules, you can use exception parameters (EXCEPTIONS) to allow the user to react to error situations when the method is executed.
Implementing Methods
You must implement all of the methods in a class in the implementation part of the class in a
METHOD <meth>.
...
ENDMETHOD.
block. When you implement the method, you do not have to specify any interface parameters, since these are defined in the method declaration. The interface parameters of a method behave like local variables within the method implementation. You can define additional local variables within a method using the DATA statement.
As in function modules, you can use the RAISE <exception> and MESSAGE RAISING statements to handle error situations.
When you implement a static method, remember that it can only work with the static attributes of your class. Instance methods can work with both static and instance attributes.
Calling Methods
To call a method, use the following statement:
CALL METHOD <meth> EXPORTING... <ii> =.<f i>...
IMPORTING... <ei> =.<g i>...
CHANGING ... <ci> =.<f i>...
RECEIVING r = h
EXCEPTIONS... <ei> = rc i...
The way in which you address the method <method> depends on the method itself and from where you are calling it. Within the implementation part of a class, you can call the methods of the same class directly using their name <meth>.
CALL METHOD <meth>...
Outside the class, the visibility of the method depends on whether you can call it at all. Visible instance methods can be called from outside the class using
CALL METHOD <ref>-><meth>...
where <ref> is a reference variable whose value points to an instance of the class. Visible instance methods can be called from outside the class using
CALL METHOD <class>=><meth>...
where <class> is the name of the relevant class.
When you call a method, you must pass all non-optional input parameters using the EXPORTING or CHANGING addition in the CALL METHOD statement. You can (but do not have to) import the output parameters into your program using the IMPORTING or RECEIVING addition. Equally, you can (but do not have to) handle any exceptions triggered by the exceptions using the EXCEPTIONS addition. However, this is recommended.
You pass and receive values to and from methods in the same way as with function modules, that is, with the syntax:
... <Formal parameter> = <Actual parameter>
after the corresponding addition. The interface parameters (formal parameters) are always on the left-hand side of the equals sign. The actual parameters are always on the right. The equals sign is not an assignment operator in this context; it merely serves to assign program variables to the interface parameters of the method.
If the interface of a method consists only of a single IMPORTING parameter, you can use the following shortened form of the method call:
CALL METHOD <method>( f).
The actual parameter <f> is passed to the input parameters of the method.
If the interface of a method consists only of IMPORTING parameters, you can use the following shortened form of the method call:
CALL METHOD <method>(....<ii> =.<f i>...).
Each actual parameter <f i > is passed to the corresponding formal parameter <i i >.
Event Handler Methods
Event handler methods are special methods that cannot all be called using the CALL METHOD statement. Instead, they are triggered using events. You define a method as an event handler method using the addition
... FOR EVENT <evt> OF <cif>...
in the METHODS or CLASS-METHODS statement.
The following special rules apply to the interface of an event handler method:
The interface may only consist of IMPORTING parameters.
Each IMPORTING parameter must be an EXPORTING parameter of the event <evt>.
The attributes of the parameters are defined in the declaration of the event <evt> (EVENTS statement) and are adopted by the event handler method.
See also Triggering and Handling Events
Constructors
Constructors are special methods that cannot be called using CALL METHOD. Instead, they are called automatically by the system to set the starting state of a new object or class. There are two types of constructors - instance constructors and static constructors. Constructors are methods with a predefined name. To use them, you must declare them explicitly in the class.
The instance constructor of a class is the predefined instance method CONSTRUCTOR. You declare it in the public section as follows:
METHODS CONSTRUCTOR
IMPORTING.. [VALUE(]<ii>[)] TYPE type [OPTIONAL]..
EXCEPTIONS.. <ei>.
and implement it in the implementation section like any other method. The system calls the instance constructor once for each instance of the class, directly after the object has been created in the CREATE OBJECT statement. You can pass the input parameters of the instance constructor and handle its exceptions using the EXPORTING and EXCEPTIONS additions in the CREATE OBJECT statement.
The static constructor of a class is the predefined static method CLASS_CONSTRUCTOR. You declare it in the public section as follows:
CLASS-METHODS CLASS_CONSTRUCTOR.
and implement it in the implementation section like any other method. The static constructor has no parameters. The system calls the static constructor once for each class, before the class is accessed for the first time. The static constructor cannot therefore access the components of its own class.
The method used for calling the interface parameters is set in the subroutine interface. The
parameters can be called either by reference or by value.
Calling by reference: The address of the actual parameter is called. Within the subroutine, the variable is addressed using the formal parameter name. Changes have an immediate effect on the global variable. If only the formal parameter name is specified in the subroutine interface, then the parameter is called by reference.
Calling by value: When the subroutine is called, a local variant is created with the formal parameter name and the actual parameter value is copied to the formal parameter. There are two types of call by value:
Calling by value: the formal parameter is listed in the interface after the USING clause with the addition VALUE( ). When the subroutine is called, the actual parameter is copied to the formal parameter. Changes made to the formal parameter only affect the local copy, not the actual parameter.
Calling by value and result: the formal parameter is listed in the interface after the CHANGING clause with the addition VALUE( ). When the subroutine is called, the actual parameter is copied to the formal parameter. Changes made to the formal parameter initially only affect the local copy. When the ENDFORM statement is reached, the formal parameter value is copied back to the actual parameter.
The parameters in the interface are called formal parameters , and the parameters that you pass to the subroutine are called actual parameters .
You must have the same number of actual parameters as formal parameters. You cannot have optional parameters. Parameters are assigned in the sequence in which they are listed.
When you call a subroutine using PERFORM, the system checks whether the types of the actual parameters in the PERFORM statement are compatible with the formal parameters. Different kinds of checks are performed for different types:
Complete type checks:
TYPE D, F, I, T or . These types are fully specified. The system checks to see if the data type of the actual parameter is identical to the type of the formal parameter in its entirety.
Partial type checks of generic types
TYPE C, N, P or X. The system checks whether the actual parameter has the type C, N, P or X. The length of the parameter and the number of decimal places in the DECIMALS addition (type P) are passed from the actual parameter to the formal parameter.
TYPE all unspecified information from generic
Dictionary types is inherited by the formal parameter from an actual parameter.
The interface is defined in the FORM routine. USING and CHANGING in the PERFORM statement are purely documentary
regards
dilip
2008 Mar 31 11:13 PM
Hello
The following sample report ZUS_SDN_ALV_VALUE_VS_REFERENCE shows you the effect of passing the output itab for an ALV grid either by reference (i.e. we pass the original itab) or by value (i.e. we pass a copy of the original itab).
If you run the report using the "pass by value" option the ALV grid dumps because the ct_itab is only temporarily existing within method changing_itab_byval. However, for the ALV grid the output itab must be globally accessible.
*&---------------------------------------------------------------------*
*& Report ZUS_SDN_ALV_VALUE_VS_REFERENCE
*&
*&---------------------------------------------------------------------*
*& Thread: call by value and return
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="802877"></a>
*&---------------------------------------------------------------------*
REPORT zus_sdn_alv_value_vs_reference.
types: ty_t_knb1 type STANDARD TABLE OF knb1.
DATA:
gt_knb1 TYPE ty_t_knb1.
PARAMETERS:
p_byval RADIOBUTTON GROUP grp, " pass by value
p_byref RADIOBUTTON GROUP grp DEFAULT 'X'. " pass by reference
*---------------------------------------------------------------------*
* CLASS lcl_myclass DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_myclass DEFINITION.
PUBLIC SECTION.
CLASS-DATA:
mt_itab TYPE ty_t_knb1,
*
ms_layout TYPE lvc_s_layo,
mo_docking TYPE REF TO cl_gui_docking_container,
mo_alvgrid TYPE REF TO cl_gui_alv_grid.
CLASS-METHODS:
changing_itab_byref
CHANGING
ct_itab TYPE ty_t_knb1,
changing_itab_byval
CHANGING
value(ct_itab) TYPE ty_t_knb1,
create_controls.
ENDCLASS. "lcl_myclass DEFINITION
*---------------------------------------------------------------------*
* CLASS lcl_myclass IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_myclass IMPLEMENTATION.
METHOD changing_itab_byref.
" NOTE: ct_itab is "identical" to gt_knb1 and exists globally
CALL METHOD mo_alvgrid->set_table_for_first_display
EXPORTING
i_structure_name = 'KNB1'
* IS_VARIANT =
* I_SAVE =
* I_DEFAULT = 'X'
is_layout = ms_layout
CHANGING
it_outtab = ct_itab
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDMETHOD. "changing_itab_byref
METHOD changing_itab_byval.
" NOTE: ct_itab exists only temporarily during the method call
" -> ALV grid dumps
CALL METHOD mo_alvgrid->set_table_for_first_display
EXPORTING
i_structure_name = 'KNB1'
* IS_VARIANT =
* I_SAVE =
* I_DEFAULT = 'X'
is_layout = ms_layout
CHANGING
it_outtab = ct_itab
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDMETHOD. "changing_itab_byval
METHOD create_controls.
CREATE OBJECT mo_docking
EXPORTING
parent = cl_gui_container=>screen0
* REPID =
* DYNNR =
* SIDE = DOCK_AT_LEFT
* EXTENSION = 50
ratio = 90
EXCEPTIONS
OTHERS = 99.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
CALL METHOD mo_docking->set_extension
EXPORTING
extension = 99999 " use full screen size
EXCEPTIONS
cntl_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
CREATE OBJECT mo_alvgrid
EXPORTING
i_parent = mo_docking
EXCEPTIONS
OTHERS = 99.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* ms_layout-edit = 'X'. " editable ALV grid
ENDMETHOD. "create_controls
ENDCLASS. "lcl_myclass IMPLEMENTATION
START-OF-SELECTION.
SELECT * FROM knb1 INTO TABLE gt_knb1
WHERE bukrs = '2000'.
lcl_myclass=>mt_itab = gt_knb1.
lcl_myclass=>create_controls( ).
IF ( p_byref = 'X' ).
CALL METHOD lcl_myclass=>changing_itab_byref
CHANGING
ct_itab = lcl_myclass=>mt_itab.
ELSE. " p_byval = 'X'
CALL METHOD lcl_myclass=>changing_itab_byval
CHANGING
ct_itab = lcl_myclass=>mt_itab.
ENDIF.
CALL METHOD lcl_myclass=>mo_docking->link
EXPORTING
repid = syst-repid
dynnr = '0100'
* CONTAINER =
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
lifetime_dynpro_dynpro_link = 3
OTHERS = 4.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* NOTE: dynpro contains no elements
CALL SCREEN '0100'.
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
CASE syst-ucomm.
WHEN 'REFRESH'.
lcl_myclass=>mo_alvgrid->refresh_table_display( ).
WHEN OTHERS.
SET SCREEN 0. LEAVE SCREEN.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
Regards
Uwe
2008 Apr 01 5:35 AM
hi,
check this link................
http://help.sap.com/saphelp_nw2004s/helpdata/en/9f/db984635c111d1829f0000e829fbfe/content.htm
................................................
data : a type i, b type i.
a = 10.
b = 20.
write :/ a.
perform call_by_value changing a.
write :/ a.
perform call_by_ref using a.
write :/ a.
perform call_by_val_ref changing a b.
write :/ a , b.
&---------------------------------------------------------------------
*& Form call_by_value
&---------------------------------------------------------------------
text
----------------------------------------------------------------------
-->P_A text
----------------------------------------------------------------------
form call_by_value using value(a) .
a = a + 10.
endform. " call_by_value
&---------------------------------------------------------------------
*& Form call_by_ref
&---------------------------------------------------------------------
text
----------------------------------------------------------------------
-->P_A text
----------------------------------------------------------------------
form call_by_ref using a.
a = a + 10.
endform. " call_by_ref
&---------------------------------------------------------------------
*& Form call_by_val_ref
&---------------------------------------------------------------------
text
----------------------------------------------------------------------
<--P_A text
<--P_B text
----------------------------------------------------------------------
form call_by_val_ref changing a
value(b).
a = a + 10.
b = b + 10.
endform. " call_by_val_ref
if helpful give reward points
Regards
pankaj
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |