Pass by Value, Pass by Reference 이해하기

ABAP 프로그램내 Form문에서 사용하는 파라미터 전달방식을 설명합니다.

1. Pass by reference for USING parameters 
– For the formal parameters p1 p2 …, no local data object is created in the subroutine. Instead, when it is called, a reference is passed to the specified actual parameter. A change to the formal parameter in the subroutine also changes the value of the actual parameter. 
→ Local Data Object Value가 바뀌면 Actual Parameter Value도 바뀜.

2. Pass by reference for CHANGING parameters
–  The formal parameters p1 p2 … are handled exactly like those parameters defined for pass by reference using USING. 
→ Pass by reference for USING과 동일함

3. 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. 
→ Local Data Object Value가 바뀌어도 Actual Parameter Value는 바뀌지 않음.

4. 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.
→ Local Data Object Value가 바뀌어도 Actual Parameter Value는 바뀌지 않음 (단, 정상적으로 종료가 되면 바뀜)

5. 예제

DATA: f1 VALUE 'A',
      f2 VALUE 'B'.
DATA: f3 VALUE 'C',
      f4 VALUE 'D'.

WRITE:/ f1, f2.
* Pass by Reference
PERFORM s1 USING f1
        CHANGING f2.
WRITE:/ f1, f2.

WRITE:/ f3, f4.
* Pass by Value
PERFORM s2 USING f3
        CHANGING f4.
WRITE:/ f3, f4.

*&---------------------------------------------------------------------*
*&      Form  s1
*&---------------------------------------------------------------------*
FORM s1 USING p1
     CHANGING p2.
  p1 = p2 = 'X'.
ENDFORM.                    "s1

*&---------------------------------------------------------------------*
*&      Form  s2
*&---------------------------------------------------------------------*
FORM s2 USING VALUE(p1)
     CHANGING VALUE(p2).
  p1 = p2 = 'X'.
ENDFORM.                    "s2