Show TOC

 Calling Function Modules From Your ProgramsLocate this document in the navigation structure

Use

You can call a function module from any ABAP program by using the following ABAP statement:

 CALL FUNCTION <function module name>
   [EXPORTING  f1 = a1.... fn = an]
   [IMPORTING  f1 = a1.... fn = an]
   [CHANGING   f1 = a1.... fn = an]
   [TABLES     t1 = itab1.... tn = itabn]
   [EXCEPTIONS e1 = r1.... en = rn]
   [ERROR_MESSAGE = rE]
   [OTHERS = ro]]. 
Features

CALL FUNCTION Statement

You enter the name of the function module <function module> as a literal. In the EXPORTING , IMPORTING , CHANGING , and TABLES options, you pass parameters by explicitly assigning the actual parameters to the formal parameters. You assign parameters following the scheme: <formal parameter> = <actual parameter> .If you assign multiple parameters within an option, insert spaces between them or start a new line.

  • EXPORTING: Assigns the actual parameter a i to the formal input parameter.f i. The formal parameters must be declared as import parameters in the function module. The parameters may take any data type.If you specify a reference field, the system checks the parameter.
  • IMPORTING: Assigns the formal output parameter f i of the function module to the actual parameter a i . The formal parameters must be declared as export parameters in the function module. The parameters may take any data type.
  • CHANGING: Assigns the actual parameter a i to the formal parameter f i . After the function module has been processed, the system returns the (changed) values of the formal parameters f i to the actual parameters f i . The formal parameters must be declared as CHANGING parameters in the function module. These parameters may take any data type.
  • TABLES: Assigns internal tables to table parameters. Internal tables are always assigned by reference. The parameters in this option must always reference internal tables.
  • EXCEPTIONS: Allows you to react to errors in the function module. If an exception occurs, the processing of the function module is terminated.
Example

If the exception e i is triggered, the system terminates the function module and does not pass any values back to the program.The calling program accepts the exception e i by assigning the value r i to the system field SY-SUBRC. This value functions as a return code (r i must be a numeric literal). In the calling program, you can evaluate the system field.

You can change the error handling in the function module by specifying an ERROR_MESSAGE in the EXCEPTIONS list. Messages should only be called in exception handling (using the statements MESSAGE.....RAISING or RAISE in the function module). For more information, see Overview of Coding for Function Modules .

  • ERROR_MESSAGE: The system treats messages that are called in the function module as follows:
    • Messages of classes S, I, and W are ignored (but entered in the log if you are running the program in the background).
    • Messages of classes E and A cause the function module to terminate as though the ERROR_MESSAGE exception had been triggered (SY-SUBRC is set to r E ).

    If you enter OTHERS in the EXCEPTION list, you can allow for all exceptions, even though they are not listed. That functions as a default exception.

    Note

    You can use the same number r i for different exceptions, as long as the system does not require further specification of the exceptions.

  • If the function module works with exception classes, the CALL FUNCTION statement looks like this:
 try.
   call function 'FB_EXCEPTION_TEST'
   exporting
   p1                     =
   p2                     =
  * IMPORTING
  *   P3                     =
  *   P4                     =
  .
  catch cx_sy_zerodivide.
  endtry. 
Update Tasks and Remote Hosts

There are other parameters you can use with the CALL FUNCTION statement, if the function is to be executed in an update task or on a remote host.

  • When a function module runs in the update task, the system processes it asynchronously. It waits until the next database update is triggered by COMMIT WORK.
  • Running a function module on a remote host means that you call a function module within another SAP system or a non-SAP system.

For more information about how to call function modules from your programs, see the Function Modules section of the ABAP User Guide .