Ansicht
Dokumentation

ABAPCALL_FUNCTION- - CALL FUNCTION-

ABAPCALL_FUNCTION- - CALL FUNCTION-

General Material Data   General Data in Customer Master  
This documentation is copyright by SAP AG.
SAP E-Book

CALL FUNCTION

Variant 1

CALL FUNCTION func.


Additions


Static Parameter Passing

1. ... EXPORTING  p1 = f1       ... pn = fn  

2. ... IMPORTING  p1 = f1       ... pn = fn  

3. ... TABLES     p1 = itab1    ... pn = itabn

4. ... CHANGING   p1 = f1       ... pn = fn  

5. ... EXCEPTIONS except1 = rc1 ... exceptn = rcn

Dynamic Parameter Passing

6. ... PARAMETER-TABLE itab  

7. ... EXCEPTION-TABLE itab  

Effect

Calls the function module func; func can be a literal (static call) or a variable (dynamic call).
The name of the function module must be uppercase.

To edit function modules, choose Tools → ABAP Workbench → Function Builder.
The assignment of parameters is by name (p1, p2, etc.), not by sequence.
To return from the function module, use the key word EXIT, unless EXIT occurs in a loop or a subroutine.

Return Value

SY-SUBRC = 0:

Function executed successfully.

SY-SUBRC <> 0:

An exception occurred in the function module (see EXCEPTIONS.

Notes

Please consult Data Area and Modularization Unit Organization documentation as well.

You can use a namespace prefix with function modules.

Addition 1

... EXPORTING p1 = f1 ... pn = fn

Effect

EXPORTING passes fields, structures, or internal tables to the function module. You must declare the parameters p1 ... pn in the function interface as import parameters. When you call the function module, you must assign values to all import parameters that are not flagged in the interface definition as optional and do not have any default values.

Addition 2

... IMPORTING p1 = f1 ... pn = fn

Effect

IMPORTING passes fields, structures, or internal tables from the function module back to the calling program. The parameters p1 ... pn must be declared as export parameters in the function interface.

Addition 3

... TABLES p1 = itab1 ... pn = itabn

Effect

TABLES passes references to internal tables. The parameters p1 ... pn must be declared as table parameters in the function interface. When you call the function module, you must assign values to all table parameters that are not flagged as optional in the interface definition.

Addition 4

... CHANGING p1 = f1 ... pn = fn

Effect

CHANGING passes fields, structures, or internal tables to the function module and the changed values are returned. The parameters p1 ... pn must be declared as CHANGING parameters in the function interface. When you call the function module, you must assign values to all CHANGING parameters of the function module that are not flagged as optional in the interface definition and have no default values.

Addition 5

... EXCEPTIONS except1 = rc1 ...                                       exceptn = rcn

Effect

EXCEPTIONS lists the exceptions to be handled by the calling program itself. At the end of the exception list, you can use OTHERS to refer to all the remaining exceptions.
If one of the listed exceptions occurs, SY-SUBRC is set to the appropriate value rc (a numeric literal!) and control passes back to the calling program. By specifying a return code, you can divide the exceptions into classes. With the second form, without "=rc", SY-SUBRC is set to a value other than 0 if an exception occurs.
If the function module triggers an exception (with the statements RAISE and MESSAGE ... RAISING) and the exception cannot be handled by the calling program itself, then:

  • the program is terminated with a runtime error during RAISE
  • the message is issued during MESSAGE ... RAISING


Note

The following EXCEPTIONS are predefined by the system and have a special meaning:

  • OTHERS: Covers all user-defined exceptions in the called function module.
  • ERROR_MESSAGE: This exception instructs the system to ignore S messages, I messages, and W messages until return from the function module (although they still appear in the log during background processing). When an E message or an A message occurs, the called function module terminates, as if the exception ERROR_MESSAGE has been triggered. If an A message is handled using ERROR_MESSAGE, the ROLLBACK WORK statement is triggered implicitly.


Example

DATA: WA_SFLIGHT TYPE SFLIGHT,
      P_LOSS LIKE SFLIGHT-PAYMENTSUM,
      P_REVENUE LIKE SFLIGHT-PRICE,
      P_CARRID LIKE SFLIGHT-CARRID.
...
SELECT * FROM SFLIGHT INTO WA_SFLIGHT WHERE CARRID = P_CARRID ... .
CALL FUNCTION 'CALCULATE_REVENUE_LOSS'
     EXPORTING
          PAYMENTSUM = WA_SFLIGHT-PAYMENTSUM
          SEATSOCC   = WA_SFLIGHT-SEATSOCC
          PRICE      = WA_SFLIGHT-PRICE
     IMPORTING
          LOSS       = P_LOSS
          REVENUE    = P_REVENUE
     EXCEPTIONS
          OTHERS     = 1.
...
ENDSELECT.
...

Example
TABLES SFLIGHT.
DATA: ITAB TYPE STANDARD TABLE OF BCAXX WITH
NON-UNIQUE DEFAULT KEY INITIAL SIZE 10.
P_YEAR ... .

CALL FUNCTION 'FILL_SEATTAB'
     EXPORTING

          YEAR     = P_YEAR

     TABLES
          SEATTAB  = ITAB
     EXCEPTIONS
          NO_ENTRY = 1
          OTHERS   = 2.

CASE SY-SUBRC.

     WHEN 1. ...
     WHEN 2. ...

ENDCASE.
...

Addition 6

... PARAMETER-TABLE itab

Effect

With this addition, the interface of a function module can be dynamically supplied with parameters. The addition PARAMETER-TABLE excludes parallel use of the static additions 1 through 5.

The parameter table itab must be a sorted table of the table type ABAP_FUNC_PARMBIND_TAB or of the line type ABAP_FUNC_PARMBIND. These types are defined in the type group ABAP in the ABAP dictionary. The table has the following four columns:

  • NAME For the name of the formal paramter
  • KIND for the type of parameter passing
  • VALUE of the type REF TO DATA for the value of the actual parameter
  • TABLES_WA of the type REF TO DATA for the value of the table work area, if a TABLES parameter is passed.

The columns NAME and KIND form the unique table key. For each non-optional parameter, you must fill exactly one line of the internal table.

In NAME, you must specify the name of the parameter, and in KIND the parameter type. For the parameter type, you must use solely the following constants from the type group ABAP:
Modules

  • ABAP_FUNC_EXPORTING for EXPORTING parameters
  • ABAP_FUNC_IMPORTING for IMPORTING parameters
  • ABAP_FUNC_CHANGING for CHANGING parameters
  • ABAP_FUNC_TABLES for TABLES parameters


The descriptions correspond to the caller's view. If the specified and actual parameter types do not correspond to each other, the system initiates, at runtime, an exception that cannot be handled.

For the value of the actual parameter, the reference in the column VALUE of the table line must point to a data object that contains the required value. For this you can use the command GET REFERENCE OF f INTO g.

If internal tables are passed to the TABLES parameter of a function module, that is, the column KIND contains the constant ABAP_FUNC_TABLES, the reference in the column VALUE must point to the table body of the internal table. In addition to the table body, you can pass on a reference to a table work area suitable for the type in the column TABLES_WA. This work area fills the header line of the formal parameter TABLES in the function module. In this way, it is possible, as in the static case, to pass internal tables with the header line, that is, table body and header line, to the TABLES parameter.

Example

type-pools ABAP.

data NAME type STRING value `ABAP_DOCU_SHOW`.

data PARA_TAB type ABAP_FUNC_PARMBIND_TAB.
data PARA_LINE like line of PARA_TAB.


data AREA(4) type C.
data OBJECT(30) type C.

AREA = 'ABAP'.
PARA_LINE-NAME = 'AREA'.
PARA_LINE-KIND = ABAP_FUNC_EXPORTING.
get reference of AREA into PARA_LINE-VALUE.
append PARA_LINE to PARA_TAB.

OBJECT = 'CLASS'.
PARA_LINE-NAME = 'NAME'.
PARA_LINE-KIND = ABAP_FUNC_EXPORTING.
get reference of OBJECT into PARA_LINE-VALUE.
append PARA_LINE to PARA_TAB.

call function NAME
     parameter-table PARA_TAB.

In this example, the function module ABAP_DOCU_SHOW is called dynamically in full form, whereby its IMPORTING parameters AREA and NAME are supplied with data.

Addition 7

... EXCEPTION-TABLE itab

Effect

With this addition, the classic exceptions of the function module can be handled dynamically. The addition EXCEPTION-TABLE excludes simultaneous use of static addition 5.

The exception table itab must be a hash table of the table type ABAP_FUNC_EXCPBIND_TAB or of the line type ABAP_FUNC_EXCPBIND. These types are defined in the type group ABAP in the ABAP dictionary. The table has the following three columns:

  • NAME for the name of the exception
  • VALUE of type I for the value to be assigned to SY-SUBRC
  • MESSAGE of the type REF TO DATA. This column is current ly not required.

The column NAME is the unique table key. For each exception, exactly one line of the internal table can be filled. Here, the component VALUE is assigned the numeric value that should be in SY-SUBRC after initiation of the exception.

Example

type pools ABAP.

data NAME type STRING value `READ_SPFLI_INTO_TABLE`.

data PARA_TAB type ABAP_FUNC_PARMBIND_TAB.
data PARA_LINE like line of PARA_TAB.

data EXCP_TAB type ABAP_FUNC_EXCPBIND_TAB.
data EXCP_LINE like line of EXCP_TAB.

data CARRIER type SPFLI-CARRID.
data JTAB type SPFLI_TAB.

CARRIER = 'XYZ'.
PARA_LINE-NAME = 'ID'.
PARA_LINE-KIND = ABAP_FUNC_EXPORTING.
get reference of CARRIER into PARA_LINE-VALUE.
append PARA_LINE to PARA_TAB.

PARA_LINE-NAME = 'ITAB'.
PARA_LINE-KIND = ABAP_FUNC_IMPORTING.
get reference of JTAB into PARA_LINE-VALUE.
append PARA_LINE to PARA_TAB.

EXCP_LINE-NAME = 'NOT_FOUND'.
EXCP_LINE-VALUE = 1.
insert EXCP_LINE into table EXCP_TAB.

EXCP_LINE-NAME = 'OTHERS'.
EXCP_LINE-VALUE = 4.
insert EXCP_LINE into table EXCP_TAB.

call function NAME
  parameter-table
    PARA_TAB
  exception-table
    EXCP_TAB.

case SY-SUBRC.
  when 1.
    message id SY-MSGID type SY-MSGTY number SY-MSGNO.
  when 2.
    message E888(SABAPDOCU) with 'Error in  function module'.
endcase.

In this example, the function module READ_SPFLI_INTO_TABLE is called in full form, whereby its exceptions are handled via the internal table EXCP_TAB.

Exceptions

Catchable Exceptions

CX_SY_DYN_CALL_ILLEGAL_FUNC

  • Cause: The called function is known but not active.
    Runtime Error: CALL_FUNCTION_NOT_ACTIVE
  • Cause: The called function is unknown.
    Runtime Error: CALL_FUNCTION_NOT_FOUND

CX_SY_DYN_CALL_ILLEGAL_TYPE

  • Cause: The type of actual parameter does not meet the requirements of the function interface.
    Runtime Error: CALL_FUNCTION_CONFLICT_GEN_TYP
  • Cause: The actual parameter does not have the length expected by the function.
    Runtime Error: CALL_FUNCTION_CONFLICT_LENG
  • Cause: The actual parameter does not have the type expected by the function.
    Runtime Error: CALL_FUNCTION_CONFLICT_TYPE
  • Cause: Only functions in the update task that are designed for this purpose may be called.
    Runtime Error: CALL_FUNCTION_NO_VB
  • Cause: An actual parameter does not meet the alignment requirements of the corresponding formal parameter.
    Runtime Error: CALL_FUNCTION_WRONG_ALIGNMENT

CX_SY_DYN_CALL_PARAM_MISSING

  • Cause: The function expects a parameter that was not specified by the caller.
    Runtime Error: CALL_FUNCTION_PARM_MISSING

CX_SY_DYN_CALL_PARAM_NOT_FOUND

  • Cause: The caller specified a parameter that the function does not recognize.
    Runtime Error: CALL_FUNCTION_PARM_UNKNOWN


Additional help

Calling Function Modules






Vendor Master (General Section)   CL_GUI_FRONTEND_SERVICES - Frontend Services  
This documentation is copyright by SAP AG.

Length: 19914 Date: 20240623 Time: 203211     sap01-206 ( 231 ms )