Você está na página 1de 9

Modularization in ABAP

Modularization is breaking down the application code into smaller units, so that it is easy to maintain. Suppose if we want to implement the same logic like adding two numbers in several places of the same program, then write the logic inside a modularization unit and call the modularization unit where ever we want to add two numbers. Even if we want to declare the same variables in multiple programs or use the same logic in different programs then code the common part in the modularization unit and use it in different programs. Advantages of Modularization are as follows. Easier to read, maintain and debug. Eliminates redundancy of code and increases reusability of code . Different types of Modularization units called from ABAP programs. Macros If you want to reuse the same set of statements more than once in a program, you can include them in a macro. You can only use a macro within the program in which it is defined. Include Programs Include programs allow you to use the same source code in different programs. They are mainly used for modularizing source code and have no parameter interface. Subroutines Subroutines are normally called internally i.e. called from the same program in which it is declared. But subroutines can also be called from external programs. Function Modules Function Modules are stored in the central library and can be called from any program. Even the function modules(RFC enabled) can be called from non-SAP systems. Methods Methods used ABAP object oriented programming Different types of Modularization units called from ABAP runtime. Event Blocks Event blocks begin with a event code and ends with next processing block. Dialog Modules Dialog Modules are used in PBO and PAI events of Module Pool programs.

ABAP Macros

If we want to reuse the same set of statements more than once in a program, we can include them in a macro. We can only use a macro within the program in which it is defined. Macro definition should occur before the macro is used in the program. We can use the following syntax to define a macro. DEFINE <macro name>. ... END-OF-DEFINITION. Demo program using Macro. *Macro definition DEFINE print. write:/ 'Hello Macro'. END-OF-DEFINITION. WRITE:/ 'Before Using Macro'. print. Output

We can pass up to 9 placeholders to Macros. *Macro definition DEFINE print. write:/ 'Hello', &1, &2. END-OF-DEFINITION. WRITE:/ 'Before Using Macro'. print 'ABAP' 'Macros'. Output

ABAP Include Program

Include programs are not standalone programs and cannot be executed independently. They can be used as a container for ABAP source code. They are used to organize the ABAP source code into small editable units which can be inserted at any place in other ABAP programs using the INCLUDE statement. Include programs can be used in different programs. The include statement copies the contents of the include program into the main program. Include programs have no parameter interface and they cannot call themselves. While creating the INCLUDE program using the ABAP editor choose program type as "I" i.e. INCLUDE program in the program attributes.

Syntax for Include program is as follows. INCLUDE <Include Program Name> Source code of ZINCLUDE_DATA. DATA: g_name(10) TYPE c. Source code of ZINCLUDE_WRITE. WRITE:/ 'Inside include program'. Source code of main program. REPORT zmain_program. INCLUDE zinclude_data. WRITE:/ 'Main Program'. INCLUDE zinclude_write. Output

How to copy a function group in SAP?

Function modules can be copied in function builder(SE37). But if you want to copy an entire function group along with all the function modules follow the steps given below. First go to Object Navigator(SE80).

Select Function Group in the drop down box and enter the name of the Function Group that we want to copy and press enter.

Once the enter is pressed, the function group will be displayed in the bottom window. Now right click on the function group name and select copy.

Enter the name for the new function group and press copy.

This will create the new function group, now we can copy all the function modules one by one. Just press continue.

This will display all the Function Modules that is there in the original function group. Here we copy all the function modules or we can deselect the function modules that we dont want. Press copy.

A popup will be displayed for the first function module, enter the name for the new function module and press copy. This will copy the first function module and again a popup will be displayed for the next function module. Follow the same procedure for all the remaining function modules.

ABAP Subroutine
Subroutines are procedures that we define in an ABAP program and can be called from any program. Subroutines are normally called internally, i.e. called from the same program in which it is defined. But it is also possible to call a subroutine from an external program. Subroutines cannot be nested and are usually defined at the end of the program. A subroutine can be defined using FORM and ENDFORM statements. FORM <subroutine name>. ... ENDFORM. A subroutine can be called using PERFORM statement. PERFORM Example PERFORM WRITE:/ <subroutine name>. Program. sub_display. 'After Perform'.

*&--------------------------------------------------------------------* *& Form sub_display *&--------------------------------------------------------------------* FORM sub_display. WRITE:/ 'Inside Subroutine'. ENDFORM. " sub_display Output

Subroutines can call other subroutines and may also call themselves. Once a subroutine has finished running, the control returns to the next statement after the PERFORM statement. We can terminate a subroutine by using the EXIT or CHECK statement. EXIT statement can be used to terminate a subroutine unconditionally. The control returns to the next statement after the PERFORM statement. PERFORM sub_display. WRITE:/ 'After Perform Statement'. *&--------------------------------------------------------------------* *& Form sub_display *&--------------------------------------------------------------------*

FORM sub_display. WRITE:/ 'Before Exit Statement'. EXIT. WRITE:/ 'After Exit Statement'. " This will not be executed ENDFORM. " sub_display Output

CHECK statement can be used to terminate a subroutine conditionally. If the logical expression in the CHECK statement is untrue, the subroutine is terminated, and the control returns to the next statement after the PERFORM statement. DATA: flag TYPE c. DO 2 TIMES. PERFORM sub_display. ENDDO. WRITE:/ 'After Perform Statement'. *&--------------------------------------------------------------------* *& Form sub_display *&--------------------------------------------------------------------* FORM sub_display. WRITE:/ 'Before Check Statement'. CHECK flag NE 'X'. WRITE:/ 'Check Passed'. flag = 'X'. ENDFORM. Output

" sub_display

Você também pode gostar