Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

about modularization techniques

Former Member
0 Likes
5,171

hi,,

can anybody send me the details about modularization techniques ?

mainly i want differences between INCLUDES and FUNCTION MODULES.

&

MACROS and SUBROUTINES.

5 REPLIES 5
Read only

Former Member
0 Likes
4,242

Modularization techniques is used to avoid repetitive coding.

Two techniques can be used:

1. Function Modules

When we call the function module in ABAP program, it is internally inserted in the program.

First you create function group (Path: SE37 -> Goto -> function group -> Create a group) then you create function modules.

2. Subroutines

There are two types:

- Call by value.

In here the main program values are not affected (global variables). Only local variable values are affected.

- Call by reference or pass by reference.

By default all internal tables are call by reference. In here the blobal variable values are also affected (in main program).

The advantage of modularization is:

1. Readability.

2. Code reusability

3. Providing structure to your code.

Refer

http://help.sap.com/printdocu/core/Print46c/en/data/pdf/BCABA/BCABA.pdf

Regards

Vasu

Read only

former_member404244
Active Contributor
Read only

RaymondGiuseppi
Active Contributor
0 Likes
4,242

Look at <a href="http://help.sap.com/saphelp_nw04/helpdata/en/9f/db970e35c111d1829f0000e829fbfe/content.htm">Modularization Technique</a> or <a href="http://help.sap.com/saphelp_40b/helpdata/en/9f/db970e35c111d1829f0000e829fbfe/content.htm">Modularizing ABAP Programs</a>

and of course <a href="http://help.sap.com/printdocu/core/Print46c/en/data/pdf/BCABA/BCABA.pdf">ABAP Programming (BC-ABA)</a>

Regards

Read only

Former Member
0 Likes
4,242

Hi

Purpose of modularization is: 1) Organizing your ABAP Code 2) Limit maintenance cost by coding every thing only once and making your ABAP code easier to understand.

Modularization techniques:

1) Organizing your ABAP code can be done by using INCLUDE reports and local MACRO's (DEFINE statement). Typical examples can be found in Module Pools and Function Groups with TOP-includes and special includes for PBO events, PAI events et cetera. You can discuss if using subroutines, functions or methods is also part of this type of modularization technique. At this moment, most ABAP programmers use subroutines partly as a means to create some sort of main line in their program, thus limiting large chunks of ABAP code. Regarding MACRO's there are some special problems, especially that they don't improve readability of ABAP coding, and you can not debug them.

2)Here, we are talkin about ABAP PROCEDURES: a) Subroutines, b) Functions and c) Methods

- 2a) Subroutines: can be used locally and globally (external subroutine calls). Subroutines have a formal interface ( USING, CHANGING, TABLES parameters). The interface parameters can be passed BY VALUE or BY REFERENCE. Data that is defined within a subroutine (FROM ... ENDFORM.) is local: lifetime and visibility is limited to the moment the subroutine is called. External Subroutines are subroutines that are called from another program. Typical example can be found in the way SAPscript and SMARTforms printprograms are called by RSNAST00. External Subroutines can be grouped into Subroutine Pools.

- 2b) Functions: are part of function groups. They are designed for global use and have a unique name. Function Modules also have a formal interface using IMPORTING, EXPORTING, CHANGING and TABLES parameters. The interface parameters can be passed BY VALUE or BY REFERENCE. Specific EXCEPTIONS can be defined for error handling. A function module can also have local data.

In theory, a function module can only use data a)from the interface parameters, b) defined locally in the function module and c) defined globally in the function group. However, it is possible to see global data from calling programs using field-symbols.

Remote Function Modules are function modules that can be called from other SAP or NON-SAP systems. BAPI's are examples of RFC-enabled function modules.

Function Groups and Function Modules are maintained using transaction SE37 (or SE80).

- 2c) Methods: are part of CLASSES. Here we are talking about ABAP Objects, supporting inheritance, instantiation, encapsulation, polymorphism, events, Interfaces, visibility sections (PUBLIC, PROTECTED, PRIVATE) and lifetime options STATIC and INSTANCE.

Classes can be defined locally or globally: a) Local Classes are classes, defined as part of an ABAP program. They can be used only within this program. b) The functionality of Global Classes is identical, but these classes are maintained using the Class Builder (SE24 or SE80).

The name of a method is not unique; you always need the name of the object to create the unique name. As a result, several classes will have exactly the same method name.

Methods also have a formal interface using IMPORTING, EXPORTING, CHANGING and RETURNING parameters. The interface parameters can be passed BY VALUE or BY REFERENCE. Specific EXCEPTIONS can be defined for error handling. A method can also have local data.

In general, using classes is considered a much better alternative to using subroutines and function modules, especially with regards to maintenance costs. Actually, you do not need subroutines anymore. Function Modules are only needed is some cases, for example as RFC's because the classes don't support remote calls.

One limitation of ABAP Classes is that they do not support dynpro's. This means that you always need a report/module pool/function group if you want to create screens.

Within methods, several types of obsolete ABAP statements are not allowed like: ON CHANGE OF, TABLES and OCCURS.

Consider that new tools and options like Web Dynpro, Unit Testing, Shared Objects, Exception Classes can only be understood when having the knowledge of ABAP OO. If you are debugging new SAP transactions, you'll see that they are also programmed using ABAP Objects.

Processing blocks that are called from ABAP programs:

1. Subroutines

2. Function modules

3. Methods

Procedures

Procedures contain a set of statements, and are called from other ABAP programs.

The processing blocks that you call from ABAP programs are called procedures

You define procedures in ABAP programs. When the program is generated, they remain as standalone modules. You can call procedures in the program in which they are defined, or from external programs. Procedures have an interface for passing data, and can also contain local data.

ABAP contains the following kinds of procedures:

Subroutines

Subroutines are principally for local modularization, that is, they are generally called from the program in which they are defined. You can use subroutines to write functions that are used repeatedly within a program. You can define subroutines in any ABAP program.

Function Modules

Function modules are for global modularization, that is, they are always called from a different program. Function modules contain functions that are used in the same form by many different programs. They are important in the R/3 System for encapsulating processing logic and making it reusable. Function modules must be defined in a function group, and can be called from any program.

Methods

Methods describe the functions and behavior of classes and their instances in ABAP Objects. Methods must be defined in classes. When you call them, you must observe certain special rules of object-oriented programming.

Subroutines

Subroutines are procedures that you can define in any ABAP program and also call from any program. Subroutines are normally called internally, that is, they contain sections of code or algorithms that are used frequently locally. If you want a function to be reusable throughout the system, use a function module.

subroutine is a block of code introduced by FORM and concluded by ENDFORM.

FORM [USING ... [)] ... ] [CHANGING... [)] ... ].

...

ENDFORM.

subroutines cannot be nested. You should therefore place your subroutine definitions at the end of the program

Calling Subroutines

PERFORM... .

Subroutines can call other subroutines (nested calls) and may also call themselves (recursive calls). Once a subroutine has finished running, the calling program carries on processing after the PERFORM statement. You can use the USING and CHANGING additions to supply values to the parameter interface of the subroutine.

Function Modules

Function modules are procedures that are defined in function groups (special ABAP programs with type F) and can be called from any ABAP program. Function groups act as containers for function modules that logically belong together.

Unlike subroutines, you do not define function modules in the source code of your program. Instead, you use the Function Builder. The actual ABAP interface definition remains hidden from the programmer. You can define the input parameters of a function module as optional. You can also assign default values to them. Function modules also support exception handling. This allows you to catch certain errors while the function module is running.

Function groups are containers for function modules. You cannot execute a function group. When you call an function module, the system loads the whole of its function group into the internal session of the calling program (if it has not already been loaded).

This is used by the system to create the components of the group (main program and corresponding include programs). When you create a function group or function module in the Function Builder , the main program and include programs are generated automatically.

The main program SAPL contains nothing but the INCLUDE statements for the following include programs:

LTOP. This contains the FUNCTION-POOL statement (equivalent for a function group of the REPORT or PROGRAM statement) and global data declarations for the entire function group.

LUXX. This contains further INCLUDE statements for the include programs

LU01, LU02, ... These includes contain the actual function modules.

The include programs LF01, LF02, ... can contain the coding of subroutines that can be called with internal subroutine calls from all function modules of the group.

All of the function modules in a function group can access the global data of the group. For this reason, you should place all function modules that use the same data in a single function group.

Function modules can have the following interface parameters:

Import parameters. These must be supplied with data when you call the function module, unless they are flagged as optional. You cannot change them in the function module.

Export parameters. These pass data from the function module back to the calling program. Export parameters are always optional. You do not have to receive them in your program.

Changing parameters. These must be supplied with data when you call the function module, unless they are flagged as optional. They can be changed in the function module. The changed values are then returned to the calling program.

Tables parameters. You use these to pass internal tables. They are treated like CHANGING parameters. However, you can also pass internal tables with other parameters if you specify the parameter type appropriately.

You can specify the types of the interface parameters, either by referring to ABAP Dictionary types or elementary ABAP types. When you call a function module, you must ensure that the actual parameter and the interface parameters are compatible.

Interface parameters are, by default, passed by value. However, they can also be passed by reference. Tables parameters can only be passed by reference. You can assign default values to optional importing and changing parameters. If an optional parameter is not passed in a function module call, it either has an initial value, or is set to the default value.

Exceptions are used to handle errors that occur in function modules. The calling program checks whether any errors have occurred and then takes action accordingly.

Calling Function Modules in ABAP

To call a function module, use the CALL FUNCTION statement:

CALL FUNCTION [EXCEPTIONS e1 = r 1.... e n = r n ].

You can specify the name of the function module either as a literal or a variable. Each interface parameter is explicitly assigned to an actual parameter . You can assign a return value to each exception . The assignment always takes the form = . The equals sign is not an assignment operator in this context.

After EXPORTING, you must supply all non-optional import parameters with values appropriate to their type. You can supply values to optional import parameters if you wish.

After IMPORTING, you can receive the export parameters from the function module by assigning them to variables of the appropriate type.

After CHANGING or TABLES, you must supply values to all of the non-optional changing or tables parameters. When the function module has finished running, the changed values are passed back to the actual parameters. You can supply values to optional changing or tables parameters if you wish.

You can use the EXCEPTIONS option to handle the exceptions of the function module. If an exception is raised while the function module is running, the system terminates the function module and does not pass any values from the function module to the program, except those that were passed by reference. If is specified in the EXCEPTION option, the calling program handles the exception by assigning to SY-SUBRC. must be a numeric literal.

If you specify of ERROR_MESSAGE in the exception list you can influence the message handling of function modules. Normally, you should only call messages in function modules using the MESSAGE ... RAISING statement. With ERROR_MESSAGE you can force the system to treat messages that are called without the RAISING option in a function module as follows:

  • Messages of classes S, I, and W are ignored (but written to the log in a background job).

  • Messages of classes E and A stop the function module as if the exception ERROR_MESSAGE had occurred (SY-SUBRC is set to ).

If you specify OTHERS after EXCEPTIONS, the system assigns a single return code to all other exceptions that you have not specified explicitly in the list.

You can use the same number for several exceptions.

You can trigger exceptions in the function module using either the RAISE or the MESSAGE ... RAISING statement. If the calling program handles the exception, both statements return control to the program. The MESSAGE ..... RAISING statement does not display a message in this case. Instead, it sets the following system fields:

1. Message class ® SY-MSGID

2. Message type ® SY-MSGTY

3. Message number ® SY-MSGNO

4. SY-MSGV1 to SY-MSGV4 (contents of fields to , included in a message).

You can use the system fields to trigger the message from the calling program.

Raising Exceptions

There are two ABAP statements for raising exceptions. They can only be used in function modules:

RAISE .

and

MESSAGE..... RAISING .

The effect of these statements depends on whether the calling program handles the exception or not. If the name of the exception or OTHERS occurs in the EXCEPTIONS addition of the CALL FUNCTION statement, the exception is handled by the calling program.

If the calling program does not handle the exception

  • The RAISE statement terminates the program and switches to debugging mode.

  • The MESSAGE ..... RAISING statement display the specified message. How the processing continues depends on the message type.

If the calling program handles the exception, both statements return control to the program. No values are transferred. The MESSAGE ..... RAISING statement does not display a message. Instead, it fills the system fields SY-MSGID, SY-MSGTY, SY-MSGNO, and SY-MSGV1 to SY-MSGV4.

Remote Function Modules

To implement a remote function module in ABAP, perform the following steps:

1. Register the module as remotely callable in the RFC server system.

In the function module Administration screen (transaction code SE37), set the field Can be called via REMOTE CALL. Registering a module as remote causes an RFC stub to be generated for it.

Asynchronous remote function calls (aRFCs) are similar to transactional RFCs, in that the user does not have to wait for their completion before continuing the calling dialog. There are three characteristics, however, that distinguish asynchronous RFCs from transactional RFCs:

  • When the caller starts an asynchronous RFC, the called server must be available to accept the request.

The parameters of asynchronous RFCs are not logged to the database, but sent directly to the server.

  • Asynchronous RFCs allow the user to carry on an interactive dialog with the remote system.

  • The calling program can receive results from the asynchronous RFC.

You can use asynchronous remote function calls whenever you need to establish communication with a remote system, but do not want to wait for the function’s result before continuing processing. Asynchronous RFCs can also be sent to the same system. In this case, the system opens a new session (or window) and allows you to switch back and forth between the calling dialog and the called session.

To start a remote function call asynchronously, use the following syntax:

CALL FUNCTION RemoteFunction STARTING NEW TASK taskname

Destination ...

EXPORTING...

TABLES ...

EXCEPTIONS...

The following calling parameters are available:

TABLES

passes references to internal tables. All table parameters of the function module must contain values.

EXPORTING

passes values of fields and field strings from the calling program to the function module. In the function module, the correponding formal parameters are defined as import parameters.

EXCEPTIONS

see Using Pre-Defined Exceptions for RFC

RECEIVE RESULTS FROM FUNCTION func is used within a FORM routine to receive the results of an asynchronous remote function call. The following receiving parameters are available:

1. IMPORTING

2. TABLES

3. EXCEPTIONS

The addition KEEPING TASK prevents an asynchronous connection from being closed after receiving the results of the processing. The relevant remote context (roll area) is kept for re-use until the caller terminates the connection.

Call a transaction asynchronally and display it in an amodal window:

DATA: MSG_TEXT(80) TYPE C. "Message text

...

  • Asynchronous call to transaction SM59 ->

  • Create a new session

CALL FUNCTION ‘ABAP4_CALL_TRANSACTION’ STARTING NEW TASK ‘TEST’

DESTINATION ‘NONE’

EXPORTING

TCODE = ‘SM59’

EXCEPTIONS

COMMUNICATION_FAILURE = 1 MESSAGE MSG_TEXT

SYSTEM_FAILURE = 2 MESSAGE MSG_TEXT

IF SY-SUBRC NE 0.

WRITE: MSG_TEXT.

ELSE.

WRITE: ‘O.K.’

ENDIF.

You must not use IMPORTING when calling aRFCs.

Transactional Remote Function Calls

RfcInstallTransactionControlinstalls four functions to control transactional behaviour.

RFC_ON_CHECK_TIDis called when a local transaction is starting.

RfcCreateTransID Get a unique transaction-ID for calling an

ABAP function module using the transactional RFC Interface

RfcIndirectCall Call an ABAP function module using the

transactional RFC Interface

RFC_ON_COMMIT is called when a local transaction ends.

RFC_ON_CONFIRM_TID is called when a local transaction is

completed.

RFC_ON_ROLLBACK is call

ed when a l

ocal transaction ends with
failure.

RFC_ONCALL


INCLUDE AND MACROS:


When you modularize source code, you place a sequence of ABAP statements in a module. Then, instead of placing all of the statements in your main program, you just call the module.
Include programs are global R/3 Repository objects. They are solely for modularizing source code, and have no parameter interface.

They have the following functions:


Library:Include programs allow you to use the same source code in different programs. For example, this can be useful if you have lengthy data declarations that you want to use in different programs.

Order. Include programs allow you to manage complex programs in an orderly way. Function groups and module pools use include programs to store parts of the program that belong together. The ABAP Workbench supports you extensively when you create such complex programs by creating the include programs automatically and by assigning them unique names.

Creating Your Own Include Programs

If you create an include program yourself, you must assign it the type I in its program attributes.

An include program cannot run independently, but must be built into other programs. Include programs can contain other includes.

The only restrictions for writing the source code of include programs are:

  • Include programs cannot call themselves.

  • Include programs must contain complete statements.

The INCLUDE statement has the same effect as copying the source code of the include program into the program. In the syntax check, the contents of the include program are also analyzed. Include programs are not loaded at runtime, but are expanded when the program is generated. Once the program has been generated, the load version contains static versions of all of its includes. If you subsequently change an include program, the programs that use it are automatically regenerated.

***INCLUDE STARTTXT.

WRITE: / 'Program started by', SY-UNAME,/ 'on host', SY-HOST, 'date:', SY-DATUM, 'time:', SY-UZEIT.ULINE.

We can then include this program in any other ABAP program to display a standard list header.

PROGRAM SAPMZTST.INCLUDE STARTTXT.

............

This could produce the following output:

Program started by KELLERH

on host ds0025 date: 03/19/1998 time: 09:00:39

Macros

If you want to reuse the same set of statements more than once in a program, you can include them in a macro. For example, this can be useful for long calculations or complex WRITE statements. You can only use a macro within the program in which it is defined, and it can only be called in lines of the program following its definition.

The following statement block defines a macro :

DEFINE .

END-OF-DEFINITION.

Macros do not belong to the definition part of the program. This means that the DEFINE...END-OF-DEFINITION block is not interpreted before the processing blocks in the program. At the same time, however, macros are not operational statements that are executed within a processing block at runtime. When the program is generated, macro definitions are not taken into account at the point at which they are defined

A macro definition inserts a form of shortcut at any point in a program and can be used at any subsequent point in the program. As the programmer, you must ensure that the macro definition occurs in the program before the macro itself is used. Particular care is required if you use both macros and include programs, since not all include programs are included in the syntax check (exception: TOP include).

To use a macro, use the following form:

.

When the program is generated, the system replaces by the defined statements and each placeholder &i by the parameter

. You can use macros within macros. However, a macro cannot call itself.

DATA: RESULT TYPE I,N1 TYPE I VALUE 5,N2 TYPE I VALUE 6.

DEFINE OPERATION. RESULT = &1 &2 &3.OUTPUT &1 &2 &3 RESULT.END-OF-DEFINITION.

DEFINE OUTPUT. WRITE: / 'The result of &1 &2 &3 is', &4.END-OF-DEFINITION.

OPERATION 4 + 3.OPERATION 2 ** 7.OPERATION N2 - N1.

The produces the following output:

The result of 4 + 3 is 7

The result of 2 ** 7 is 128

The result of N2 - N1 is 1

Inserting the macro changes nothing in the generated form of the program.

Check this link

http://www.sapbrainsonline.com/FAQs/TECHNICAL/SAP_ABAP_MODULARIZATION_FAQ.html

http://help.sap.com/saphelp_40b/helpdata/en/9f/db970e35c111d1829f0000e829fbfe/content.htm

http://help.sap.com/saphelp_40b/helpdata/en/9f/db970e35c111d1829f0000e829fbfe/content.htm

Regards

Pavan

Message was edited by:

Pavan praveen

Message was edited by:

Pavan praveen

Read only

Former Member
0 Likes
4,242

Hi,

Follow the below link.

it gives step wise process

<a href="http://http://sapabap.iespana.es/sapabap/manuales/learnabap/">modularizzation</a>

Reward points if it is useful.

Gokul