SAP Help ABAP Subroutine basics









ABAP Subroutine basics in SAP

Sub routines


Basic ABAP code for calling a subroutine is as follows

DATA: w_var type s_carrid.

Perform sub_routine.

FORM sub_routine.
  DATA: w_var TYPE s_carrid.
ENDFORM.


Pass by reference

PERFORM read_data USING d_carrid1.
	 			
FORM read_data USING local_carrid.
	
* local_carrid points to d_carrid. When you change local_carrid you also change d_carrid1.
ENDFORM.

Note: When passing by reference the statements USING/CHANGING do not alter the functionality, you can change values with either statement. Just makes it easier to read if you use the appropriate statement.


Pass by VALUE

PERFORM read_data Changing d_carrid1.

FORM read_data CHANGING VALUE(local_carrid)

*Using the VALUE command creates a local variable called 'local_carrid' which contains the value passed to it. This can then be changed within the local subroutine with no effect on the passing variable(d_carrid). Once the program reaches the ENDFORM statement the passing variable (d_carrid) is updated). If the exit command is execurted before the ENDFORM the value is not updated(must reach ENDFORM).

ENDFORM.

FORM read_data USING VALUE(local_carrid).

If you use the USING command instead of CHANGING the global variable will not be updated even at the end of the sub routine(ENDFORM).


Passing tables using the TABLES statement

PERFORM sub_routine TABLES itab. 	"Itab must be a standard  table

FORM TABLES p_itab STRUCTURE ekko.
ENDFORM.

*note: You can pass tables without using the TABLES statement. Simply use USING/CHANGING.