Import Parameters Passed by Reference and Passed by Value
We can pass parameters into a method by import parameter. Now there are two ways to pass the method. We can pass by Reference and Pass by value. The variable, passed by reference can’t be changed inside the method. We only can change the value of variable which is passed by value.
In the following example we have created a local class cl_pass_para. A method m_pass_para has been declared with import parameters in the public section of this class. We are passing v_ref (passed by reference) and v_val (passed by value) parameters. In the implementation part we are writing both of the variable’s value. Here according to the function we can’t change the value of v_ref (passed by reference) parameter. Following syntax error will come for this condition.
The correct program is as follows.
REPORT zsr_test NO STANDARD PAGE HEADING.
*----------------------------------------------------------------------*
* CLASS cl_pass_para DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_pass_para DEFINITION.
PUBLIC SECTION.
METHODS m_pass_para IMPORTING v_ref TYPE i
value(v_val) TYPE i.
ENDCLASS. "cl_pass_para DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_pass_para IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_pass_para IMPLEMENTATION.
METHOD m_pass_para.
WRITE: / 'Parameter passed by reference', v_ref,
/ 'Parameter passed by value', v_val.
v_val = 10.
WRITE: / 'Parameter passed by reference can''t be changed', v_ref,
/ 'Parameter passed by value can be changed', v_val.
ENDMETHOD. "m_pass_para
ENDCLASS. "cl_pass_para IMPLEMENTATION
START-OF-SELECTION. DATA obj TYPE REF TO cl_pass_para.
CREATE OBJECT obj.
CALL METHOD obj->m_pass_para
EXPORTING
v_ref = 5
v_val = 7.
*----------------------------------------------------------------------*
* CLASS cl_pass_para DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_pass_para DEFINITION.
PUBLIC SECTION.
METHODS m_pass_para IMPORTING v_ref TYPE i
value(v_val) TYPE i.
ENDCLASS. "cl_pass_para DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_pass_para IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_pass_para IMPLEMENTATION.
METHOD m_pass_para.
WRITE: / 'Parameter passed by reference', v_ref,
/ 'Parameter passed by value', v_val.
v_val = 10.
WRITE: / 'Parameter passed by reference can''t be changed', v_ref,
/ 'Parameter passed by value can be changed', v_val.
ENDMETHOD. "m_pass_para
ENDCLASS. "cl_pass_para IMPLEMENTATION
START-OF-SELECTION. DATA obj TYPE REF TO cl_pass_para.
CREATE OBJECT obj.
CALL METHOD obj->m_pass_para
EXPORTING
v_ref = 5
v_val = 7.
We have the following output for this program.
Post a Comment