[ABAP] How do I create and call a function in ABAP?

To create and call a function in ABAP, follow these steps:

  1. Go to the ABAP Development Tools (ADT) perspective in your SAP development environment.

  2. Create a new ABAP program or navigate to an existing one where you want to define the function.

  3. Declare the function by using the following syntax:

 1FUNCTION function_name.
 2*"----------------------------------------------------------------------
 3*"*"Local Interface:
 4*"----------------------------------------------------------------------
 5
 6* Define input parameters
 7  IMPORTING
 8    parameter1 TYPE data_type1
 9    parameter2 TYPE data_type2.
10
11* Define output parameters
12  EXPORTING
13    output_parameter1 TYPE data_type3
14    output_parameter2 TYPE data_type4.
15
16* Define tables parameters (if required)
17  TABLES
18    table_parameter TYPE TABLE OF data_type5.
19
20* Define exceptions (if required)
21  EXCEPTIONS
22    exception1
23    exception2.
24
25* Function logic goes here
26
27ENDFUNCTION.

Replace function_name with the desired name of your function. Define the input parameters in the IMPORTING section, output parameters in the EXPORTING section, and any table parameters in the TABLES section. If your function can raise exceptions, define them in the EXCEPTIONS section.

  1. Implement the logic of the function between the FUNCTION and ENDFUNCTION keywords.

  2. Save and activate your program.

To call the function from another ABAP program or report, follow these steps:

  1. Navigate to the ABAP program or report where you want to call the function.

  2. Use the following syntax to call the function:

 1DATA: output_parameter1 TYPE data_type3,
 2      output_parameter2 TYPE data_type4.
 3
 4CALL FUNCTION 'function_name'
 5  EXPORTING
 6    parameter1 = value1
 7    parameter2 = value2
 8  IMPORTING
 9    output_parameter1 = output_parameter1
10    output_parameter2 = output_parameter2
11  TABLES
12    table_parameter = table_parameter.

Replace function_name with the name of your function. Provide the input parameter values in the EXPORTING section. Declare the variables to hold the output parameter values in the IMPORTING section. If your function uses table parameters, provide the corresponding table variable.

  1. Process the output parameters and table results as needed.

That's how you can create and call a function in ABAP.