✒️ABAP Las subrutinas
ABAP Las subrutinas
Subroutines in ABAP
Subroutines are reusable code segments defined under a specific name, with an ideal size limit of 50 lines of code. To use them, employ the keyword PERFORM followed by the subroutine's name. To declare them, double-click the subroutine name in SAP, confirming the creation of the object. SAP offers options to create the subroutine in a new include or within the same control program, with the latter being recommended.
Once selected, SAP generates a code block in our program, consisting of a FORM/ENDFORM block defining the subroutine and a set of comments for function description and input/output parameters. All this is appended at the end of the ongoing program. The FORM syntax is as follows:
FORM sample_subroutine.
" Your statements here.
ENDFORM.
Types of Subroutines
There are two types of subroutines:
- Internal Subroutines: Located within the same program or in a subroutine include declared in the control program.
PERFORM internal_subroutine.
FORM internal_subroutine.
" Your statements here.
ENDFORM.
- External Subroutines: Defined in a different program than the one calling it. To use them, use the syntax:
PERFORM external_subroutine IN PROGRAM program_with_subroutine.
Passing Parameters to Subroutines
Data can be passed between the main program and a subroutine using parameters. Three types exist:
- Input Parameters: Pass data from the main program to the subroutine.
PERFORM subroutine USING input_data.
FORM subroutine USING VALUE(input_parameter).
" Your statements here.
ENDFORM.
- Output Parameters: Pass data from the subroutine to the main program.
PERFORM subroutine CHANGING output_data.
FORM subroutine CHANGING VALUE(output_parameter).
" Your statements here.
ENDFORM.
- Input/Output Parameters: Exchange data from both sides.
PERFORM subroutine USING input_output_data
CHANGING output_data.
FORM subroutine USING VALUE(input_output_parameter)
CHANGING VALUE(output_parameter).
" Your statements here.
ENDFORM.
To pass parameters in subroutines, we use the following 3 reserved keywords:
- USING: Declares input parameters utilized in the subroutine.
- CHANGING: Declares input/output parameters utilized in the subroutine.
- TABLES: Declares input/output parameters as tables, utilized in the subroutine.
The order of the clauses should be as follows:
PERFORM <subroutine_name> TABLES <internal_table>
USING <input_parameter>
CHANGING <input_output_parameter>.
Parameters defined in the main program, to the right of the word PERFORM, are referred to as current parameters. Those to the right of the word FORM are termed formal parameters.
" Current parameters.
PERFORM select_data TABLES ti_users
USING wa_users
CHANGING v_flag.
" Formal parameters.
FORM process_data TABLES p_ti_users STRUCTURE ztable_users
USING p_wa_users STRUCTURE ztable_users
CHANGING p_flag TYPE sy-subrc.
" Your statements here.
ENDFORM.
Parameter Passing Methods
- Pass by Value: In this method, parameters are passed by value, creating copies in the subroutine. Changes made within the subroutine do not affect the original data. USING VALUE is needed.
DATA: original_value(1) TYPE c VALUE 'B'.
PERFORM pass_by_value USING original_value.
WRITE original_value. " 'B' - Unchanged
FORM pass_by_value USING VALUE(value_param).
value_param = 'X'. " Changes only within the subroutine
ENDFORM.
- Pass by Reference: This method involves passing parameters by reference, allowing the subroutine to directly modify the original data. Changes made within the subroutine are reflected outside.
DATA: original_value(1) TYPE c VALUE 'A',
result_value(1) TYPE c VALUE 'B'.
" Initial Data
WRITE:/ 'Original values:', original_value, result_value. " A B
" Call the subroutine
PERFORM pass_by_reference USING original_value
CHANGING result_value.
" Resilt Data
WRITE:/ 'Modified value:', original_value, result_value. " X X
FORM pass_by_reference USING input_value
CHANGING result_output.
" This subroutine copies the value of input_value to result_output
result_output = input_value = 'X'.
ENDFORM.
" Do not modify a USING reference parameter. Instead, define the parameter as a USING-VALUE(...) or CHANGING parameter.
- Pass by Value and Result: This method combines passing by value with a result, allowing changes in the subroutine to be copied back to the original data.
DATA: g_val_a(1) TYPE c VALUE 'A',
g_val_b(1) TYPE c VALUE 'B'.
" Initial Data
WRITE:/ 'Original value:', g_val_a , g_val_b. " A B
" Call the subroutine
PERFORM pass_by_value_and_result
USING g_val_a
CHANGING g_val_b.
" Resilt Data
WRITE:/ 'Modified value:', g_val_a , g_val_b. " A X
FORM pass_by_value_and_result USING VALUE(p_a)
CHANGING VALUE(p_b).
" This subroutine copies the value of p_a to p_b
p_b = p_a = 'X'.
ENDFORM.
Keep in mind that parameters must maintain the same order in PERFORM and FORM, but the names can differ.
Global and Local Data Declarations
There are two types of data declarations:
- Global: Accessible from any point in the program.
" Global variable declaration
DATA global_variable TYPE i.
- Local: Accessible only within the subroutine where defined.
FORM local_subroutine.
" Local variable declaration
DATA local_variable TYPE string.
" Your statements here.
ENDFORM.
It is advisable to minimize global variable declarations to preserve program modularity.
E.g
ZTEST_ABAP_JEGA_12
*&---------------------------------------------------------------------*
*& Report ZTEST_ABAP_JEGA_12
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_abap_jega_12.
* Include data, screen and subroutines
INCLUDE: ztest_abap_jega_12_data,
ztest_abap_jega_12_screen,
ztest_abap_jega_12_sub.
*----------------------------------------------------------------------*
START-OF-SELECTION.
*----------------------------------------------------------------------*
* Create Users
IF p_create EQ 'X'.
PERFORM create_user USING p_dni.
* Modify Users
ELSEIF p_modify EQ 'X'.
PERFORM modify_user USING p_dni.
* Delete Users
ELSEIF p_delete EQ 'X'.
PERFORM delete_user USING p_dni.
ENDIF.
ZTEST_ABAP_JEGA_12_DATA
*&---------------------------------------------------------------------*
*& Include ZTEST_ABAP_JEGA_12_DATA
*&---------------------------------------------------------------------*
* Declaration of Structure
DATA: it_users TYPE STANDARD TABLE OF zuser_table_jega,
wa_users LIKE LINE OF it_users.
* Declaration of Variables
DATA: v_dni TYPE z_dni,
v_nomape TYPE z_nombre_ape,
v_estado TYPE z_estado_usu,
v_birthdate TYPE z_bd.
ZTEST_ABAP_JEGA_12_SCREEN
*&---------------------------------------------------------------------*
*& Include ZTEST_ABAP_JEGA_12_SCREEN
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
* Selection Screen
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
PARAMETERS: p_dni LIKE v_dni OBLIGATORY, " DNI
p_nomape LIKE v_nomape, " Name and Surname
p_estado LIKE v_estado, " User Status
p_bd LIKE v_birthdate. " Birthdate
SELECTION-SCREEN SKIP 1.
PARAMETERS: p_create RADIOBUTTON GROUP rbg, " Create
p_modify RADIOBUTTON GROUP rbg, " Modify
p_delete RADIOBUTTON GROUP rbg. " Delete
SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN END OF BLOCK b1.
ZTEST_ABAP_JEGA_12_SUB
*&---------------------------------------------------------------------*
*& Include ZTEST_ABAP_JEGA_12_SUB
*&---------------------------------------------------------------------*
FORM create_user USING VALUE(p_dni).
CLEAR v_dni.
SELECT SINGLE dni
FROM zuser_table_jega
INTO v_dni
WHERE dni EQ p_dni.
IF sy-subrc EQ 0.
MESSAGE s001(z_prueba) WITH 'The record you are trying to create already exists'
DISPLAY LIKE 'E'.
ELSE.
CLEAR wa_users.
wa_users-mandt = sy-mandt.
wa_users-dni = p_dni.
wa_users-nombre_ape = p_nomape.
wa_users-estado = p_estado.
wa_users-birthdate = p_bd.
INSERT INTO zuser_table_jega VALUES wa_users.
IF sy-subrc EQ 0.
MESSAGE s001(z_prueba) WITH 'The record was created successfully'.
ENDIF.
ENDIF.
ENDFORM.
FORM modify_user USING VALUE(p_dni).
CLEAR v_dni.
SELECT SINGLE dni
FROM zuser_table_jega
INTO v_dni
WHERE dni EQ p_dni.
IF sy-subrc NE 0.
MESSAGE s001(z_prueba) WITH 'The record you are trying to modify does not exist'
DISPLAY LIKE 'E'.
ELSE.
CLEAR wa_users.
wa_users-mandt = sy-mandt.
wa_users-dni = p_dni.
wa_users-nombre_ape = p_nomape.
wa_users-estado = p_estado.
wa_users-birthdate = p_bd.
MODIFY zuser_table_jega FROM wa_users.
IF sy-subrc EQ 0.
MESSAGE s001(z_prueba) WITH 'The record was modified successfully'.
ENDIF.
ENDIF.
ENDFORM.
FORM delete_user USING VALUE(p_dni).
CLEAR v_dni.
SELECT SINGLE dni
FROM zuser_table_jega
INTO v_dni
WHERE dni EQ p_dni.
IF sy-subrc NE 0.
MESSAGE s001(z_prueba) WITH 'The record you are trying to delete does not exist'
DISPLAY LIKE 'E'.
ELSE.
CLEAR wa_users.
wa_users-mandt = sy-mandt.
wa_users-dni = p_dni.
wa_users-nombre_ape = p_nomape.
wa_users-estado = p_estado.
wa_users-birthdate = p_bd.
DELETE zuser_table_jega FROM wa_users.
IF sy-subrc EQ 0.
MESSAGE s001(z_prueba) WITH 'The record was deleted successfully'.
ENDIF.
ENDIF.
ENDFORM.
 
 
 
Agradecimiento:
Ha agradecido este aporte: Octavio Pasciucco
Sobre el autor
Publicación académica de Jaime Eduardo Gomez Arango, en su ámbito de estudios para la Carrera Consultor ABAP.
Jaime Eduardo Gomez Arango
Profesión: Ingeniero de Sistemas y Computación - España - Legajo: SW34C
✒️Autor de: 99 Publicaciones Académicas
🎓Cursando Actualmente: Consultor ABAP Nivel Avanzado
🎓Egresado del módulo:
Disponibilidad Laboral: FullTime
Presentación:
Ingeniero de sistemas y computación con 8 años de experiencia el desarrollo frontend & backend (react/node) y en cloud (aws), actualmente desarrollando habilidades en sap btp, ui5, abap y fiori.
Certificación Académica de Jaime Gomez