ABAP Keyword Documentation →  ABAP − Reference →  Program structure →  Modularization Statements →  Procedures →  Parameter Interface of Procedures → 

Passing Parameters

The example demonstrates the difference between passing a parameter in a procedure by value or by reference.

Source Code

REPORT  demo_procedure_param.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    TYPES: BEGIN OF line,
               x TYPE i,
               y TYPE i,
               range TYPE i,
           END OF line.

    CLASS-DATA: param TYPE STANDARD TABLE OF line,
                res TYPE i.
    CLASS-METHODS: main,
                   fill_table CHANGING g_param LIKE param,
                   solve_table IMPORTING g_param LIKE param,
                   fibb IMPORTING VALUE(l_line) TYPE line
                        EXPORTING VALUE(r) TYPE i.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    fill_table( CHANGING g_param = param ).
    solve_table( EXPORTING g_param = param ).
  ENDMETHOD.

  METHOD fill_table.
    DATA l_line LIKE LINE OF g_param.
    DO 3 TIMES.
      l_line-x = sy-index.
      l_line-y = sy-index ** 2.
      l_line-range = 12 / sy-index.
      APPEND l_line TO g_param.
    ENDDO.
  ENDMETHOD.

  METHOD solve_table.
    DATA: l_line LIKE LINE OF g_param.
    LOOP AT g_param INTO l_line.
      fibb( EXPORTING l_line = l_line IMPORTING r = res ).
      cl_demo_output=>write(
      |Fibb( { l_line-x }, { l_line-y }, { l_line-range }) = { res }| ).
    ENDLOOP.
    cl_demo_output=>display( ).
  ENDMETHOD.

  METHOD fibb.
    IF l_line-range = 1.
      IF l_line-x < l_line-y.
        r = l_line-x.
      ELSE.
        r = l_line-y.
      ENDIF.
    ELSEIF l_line-range = 2.
      IF l_line-x < l_line-y.
        r = l_line-y.
      ELSE.
        r = l_line-x.
      ENDIF.
    ELSE.
      l_line-range = l_line-range - 2.
      DO l_line-range TIMES.
        IF l_line-x < l_line-y.
          l_line-x = l_line-x + l_line-y.
          r = l_line-x.
        ELSE.
          l_line-y = l_line-x + l_line-y.
          r = l_line-y.
        ENDIF.
      ENDDO.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

Method fibb calculates the sequence term with number range of a Fibonacci sequence using the start values x and y. As a rule, the next sequence term is always the total of two previous sequence terms (therefore two start values). The method takes the parameters, a structured parameter l_line used to pass the input values, and a parameter r of type i used to output the result. Since the parameter l_line is defined as an IMPORTING parameter but still has to be changed in the method, the method definition must contain the keyword value before the parameter; otherwise a syntax error occurs. This ensures that within the method a local copy of the parameter is used. The addition value to the output parameter r has the effect that the result is assigned to the static class attribute res only after the method has been processed completely. Otherwise res would be changed in each single step of the algorithm.

The internal table param contains the input values for calculating three different sequence terms of the Fibonacci sequence. Method fill_table is used to fill param with values, and method solve_table is used to calculate and output fibb for each line of param.