You are on page 1of 11

modularization techniques:

-------------------------code reusabily
reduce redundancy

3 techniques:
--------------1.includes (not executable)
2.subroutines (not executable)
3.Function module(executable)

Include Programs :
------------------These are sub-programs which contains a set of re-usable statements .
These programs can not be executed directly.
These include programs must be embedded inside a main program for execution.
These programs dosen`t contain parameter interface, that is no importing and exporting parameters.
Syntax : INCLUDE <include name>.
Example of using include programs :

TYPES : BEGIN OF TY_MARA,


MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MEINS TYPE MARA-MEINS,
MBRSH TYPE MARA-MBRSH,

END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.
SELECT MATNR, MTART, MEINS, MBRSH FROM MARA INTO TABLE IT_MATA.
LOOP AT IT_MARA INTO WA_MARA .

WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MTART.


ENDLOOP.

SUBROUTINES
---------------> Subroutines are used to avoid the repetation of same kind of code in program.
--> Local to the program , not stored in database.
--> PERFORM is the keywork to create subroutiens.

Syntax:
-------

PERFORM ....< NAME >


form ........<NAME>
-------------------------ENDFORM.

TWO TYPES OF SUBROUTINES


-----------------------1.INTERNAL SUBROUTINE
2.EXTERNAL SUBROUTINE

1.INTERNAL SUBROUTINE : WHEN PERFORM AND FORM---- ENDFORM ARE IN SAME PROGRAM THAT IS
INTERNAL SUBROUTINE.
2.EXTERNAL SUBROUTINE :WHEN PERFORM AND FORM---- ENDFORM ARE IN DIFFERENT PROGRAM
THAT IS INTERNAL SUBROUTINE.

example:
----------tables: vbak.
TYPES: BEGIN OF t_vbap,
vbeln TYPE vbap-vbeln,
posnr TYPE vbap-posnr,
matnr TYPE vbap-matnr,
netwr TYPE vbap-netwr,
END OF t_vbap.

DATA: i_vbap TYPE STANDARD TABLE OF t_vbap,


wa_vbap TYPE t_vbap.

PARAMETERS: pa_vbeln TYPE vbak-vbeln.

PERFORM fr_get_sales_data USING pa_vbeln


CHANGING i_vbap.

IF NOT I_VBAP[] IS INITIAL.

LOOP AT i_vbap INTO wa_vbap.

WRITE:/ wa_vbap-vbeln,
wa_vbap-posnr,
wa_vbap-matnr,
wa_vbap-netwr.
ENDLOOP.

ENDIF.
*&---------------------------------------------------------------------*
*&

Form fr_get_sales_data

*&---------------------------------------------------------------------*
*

text

*----------------------------------------------------------------------*
*

-->P_PA_VBELN text

<--P_I_VBAP text

*----------------------------------------------------------------------*
FORM fr_get_sales_data USING c_VBELN
CHANGING c_VBAP like i_vbap.

select vbeln
posnr
matnr
netwr
from vbap
into table c_vbap
where vbeln = c_vbeln.
ENDFORM.

" fr_get_sales_data

Passing Parameters to a Subroutine


-------------------------------------

report ztx1801.
data: f1 value 'A',
f2 value 'B',
f3 value 'C'.

perform: s1 using f1 f2 f3.

form s1 using p1 p2 p3.


write: / f1, f2, f3,
/ p1, p2, p3.
endform.

OUTPUT:
ABC
ABC

Effect of Pass by Reference


---------------------------

EX:
data f1 value 'A'.

perform s1 using f1.


write / f1.

form s1 using p1.


p1 = 'X'.
endform.

OUTPUT
X

EFFECTS OF PASS BY VALUE


---------------------------data: f1 value 'A',
f2 value 'B',
f3 value 'C',

f4 value 'D',
f5 value 'E',
f6 value 'F'.

perform s1 using f1 f2
changing f3 f4.

perform s2 using f1 f2 f3 f4
changing f5 f6.

perform s3 using f1 f2 f3.

form s1 using p1 value(p2)


changing p3 value(p4).
write: / p1, p2, p3, p4.
endform.

form s2 using p1 value(p2) value(p3) p4


changing value(p5) p6.
write: / p1, p2, p3, p4, p5, p6.
endform.

form s3 using value(p1)


changing p2 value(p3).
write: / p1, p2, p3.

endform.

OUTPUT:
ABCD
ABCDEF
ABC

Function Modules(SE37)
----------------

Function Modules are sub-programs which contains set of re-usable statements with importing,
exporting parameters, exceptions. Unlike include programs Function Modules can be executed
independently .

Function Group in SAP ABAP


Function group is a container of Function modules, every Function Module must be saved in a Function
group. T-codes for Function Modules and Function groups are SE37 OR SE80. We can save N number of
Function Modules in a Function Group.

Types of function modules


-------------------------1.normal function module
2.remote enabled function module
3.updated function module

Components of Function Module :


------------------------------Import: These are input parameters of a Function Module.

Export: These are output parameters of a Function Module.

Changing: These are parameters which acts as importing and exporting parameters to a Function
Module .

Tables: These are internal tables which also acts as importing and exporting parameters.

Exceptions: Exceptions in Function Modules are used to catch certain type of errors .

In this lesson we are going learn how to create Function Group and Function Module, in order to create
a Function Module we need a Function Group ( We can save in existing one also), follow the below steps
to create a Function Group.

Go to SE80.

PARAMETERS: P_VBELN TYPE VBAK-VBELN.

DATA: I_VBAK TYPE STANDARD TABLE OF VBAK,


WA_VBAK TYPE VBAK.

CALL FUNCTION 'ZFUNCMOD_99'


EXPORTING
VBELN = P_VBELN

TABLES
I_VBAK = I_VBAK.

LOOP AT I_VBAK INTO WA_VBAK.


WRITE:/10 WA_VBAK-VBELN, WA_VBAK-ERNAM.
ENDLOOP.

************************************************************************

FUNCTION ZFUNCMOD_99.
*"---------------------------------------------------------------------*"*"Local Interface:
*" IMPORTING
*"

REFERENCE(VBELN) TYPE VBELN

*" TABLES
*"

I_VBAK STRUCTURE VBAK

*"----------------------------------------------------------------------

select * from vbak into table i_vbak where vbeln eq vbeln.

ENDFUNCTION.