Entering content framePassing Parameters to Subroutines Locate the document in its SAP Library structure

If a subroutine has a parameter interface, you must supply values to all of the formal parameters in its interface when you call it. You list the actual parameters after the USING or CHANGING addition in the PERFORM statement.

When you pass the values, the sequence of the actual parameters in the PERFORM statement is crucial. The value of the first actual parameter in the list is passed to the first formal parameter, the second to the second, and so on. The additions USING and CHANGING have exactly the same meaning. You only need to use one or the other. However, for documentary reasons, it is a good idea to divide the parameters in the same way in which they occur in the interface definition.

Actual parameters can be any data objects or field symbols of the calling program whose technical attributes are compatible with the type specified for the corresponding formal parameter. When you specify the actual parameters, note that any that you pass by reference to a formal parameter, and any that you pass by value to an output parameter, can be changed by the subroutine. You should therefore ensure that only data objects that you want to be changed appear in the corresponding position of the actual parameter list.

If a subroutine contains TABLES parameters in its interface, you must specify them in a TABLES addition of the PERFORM statement before the USING and CHANGING parameters. TABLES parameters are only supported to ensure compatibility with earlier releases, and should no longer be used.

You can use offset addressing for actual parameters in the same way as offset addressing for field symbols. That is, you can select memory areas that lie outside the boundaries of the specified actual parameter.

Example

PROGRAM form_test.

DATA: a1 TYPE p DECIMALS 3,
      a2 TYPE i,
      a3 TYPE d,
      a4 TYPE spfli-carrid,
      a5(1) TYPE c.

...

PERFORM subr USING a1 a2 a3 a4 a5.

...

PERFORM subr CHANGING a1 a2 a3 a4 a5.

...

PERFORM subr USING a1 a2 a3
             CHANGING a4 a5.

...

FORM subr USING
            value(f1) TYPE p
            value(f2) TYPE i
            f3        LIKE a3
          CHANGING
            value(f4) TYPE spfli-carrid
            f5.
...
ENDFORM.

This example defines a subroutine SUBR with a parameter interface consisting of five formal parameters, F1 to F5. The subroutine is called internally three times. The actual parameters are the data objects A1 to A5. The three subroutine calls are all equally valid. There are further PERFORM statements that are also equally valid, so long as the sequence of the actual parameters remains unchanged. In each call, A1 is passed to F1, A2 to F2, and so on. When the subroutine ends, A3, A4, and A5 receive the values of F3, F4, and F5 respectively. The third of the subroutine calls documents in the program what the parameter interface of the subroutine shows, namely that only A4 and A5 are changed. Whether A3 is changed depends on the way in which the subroutine is programmed.

The following example shows how generically-typed formal parameters inherit their technical attributes from their corresponding actual parameters.

Example

REPORT demo_mod_tech_describe.

DATA:
  date1      TYPE d,             date2      TYPE t,
  string1(6) TYPE c,             string2(8) TYPE c,
  number1    TYPE p DECIMALS 2,  number2    TYPE p DECIMALS 0,
  count1     TYPE i,             count2     TYPE i.

PERFORM typetest USING date1 string1 number1 count1.
SKIP.
PERFORM typetest USING date2 string2 number2 count2.

FORM typetest USING now
                    txt TYPE c
                    value(num) TYPE p
                    int TYPE i.
  DATA: t(1) TYPE c.
  DESCRIBE FIELD now TYPE t.
  WRITE: / 'Type of NOW is', t.
  DESCRIBE FIELD txt LENGTH t IN CHARACTER MODE.
  WRITE: / 'Length of TXT is', t.
  DESCRIBE FIELD num DECIMALS t.
  WRITE: / 'Decimals of NUM are', t.
  DESCRIBE FIELD int TYPE t.
  WRITE: / 'Type of INT is', t.
ENDFORM.

This produces the following output:

Type of NOW is D
Length of TXT is 6
Decimals of NUM are 2
Type of INT is I

Type of NOW is T
Length of TXT is 8
Decimals of NUM are 0
Type of INT is I

An internal subroutine TYPETEST is called twice with different actual parameters. All actual and formal parameters are compatible and no error message occurs during the syntax check. Had you declared COUNT2 with type F instead of type I, the syntax check would have returned an error, since the formal parameter INT is specified with type I. The formal parameters with generic types adopt different technical attributes depending on their corresponding technical attributes.

 

 

 

Leaving content frame