Skip to content

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

Pass by Parameter

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

Other versions: 7.31 | 7.40 | 7.54

Source Code

REPORT demo_procedure_param.

CLASS demo_fibb 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_fibb IMPLEMENTATION.
  METHOD main.
    fill_table(  CHANGING  g_param = param ).
    solve_table( EXPORTING g_param = param ).
  ENDMETHOD.

  METHOD fill_table.
    g_param = VALUE #( FOR j = 1 UNTIL j > 3
                       ( x = j
                         y = j ** 2
                         range = 12 / j ) ).
  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 -= 2.
      DO l_line-range TIMES.
        IF l_line-x < l_line-y.
          l_line-x += l_line-y.
          r = l_line-x.
        ELSE.
          l_line-y += l_line-x.
          r = l_line-y.
        ENDIF.
      ENDDO.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

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

Description

The method fibb calculates the sequence term with the number range in a Fibonacci sequence using the start values x and y. As a rule, the next sequence term is always the sum of two previous sequence terms (which is why there are two start values). The method inherits two parameters, a structured parameter l_line used to pass the input values, and a parameter r of type i used to provide the result. The parameter l_line is defined as an IMPORTING parameter but still has to be changed in the method, which means that the method definition must contain the keyword VALUE before the parameter. If not, a syntax error occurs. This ensures that a local copy of the parameter is used within the method. The addition VALUE of the output parameter r assigns the result to the static class attribute res only after the method has been processed completely. Otherwise res would be changed in every single step of the algorithm.

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