Tuesday, June 9, 2009

Modularization Techniques

Modularization means minimization, if the programs contain the same or similar blocks of statements or it is required to process the same function several times, we can avoid this redundancy or duplication of codes by using modularization techniques.

By modularizing the ABAP/4 program

1. We make them easy to read i.e. improves the readability
2. Improve their structure
3. Error handling and maintenance is easy
4. Updating can be done easily
5. Reusability i.e. procedures can be used in other programs as well
6. Encapsulation i.e. hides the internal architecture and data structure from other classes. It is important to use a series of functions that act as the means to access the data.
7. Reduces code redundancy i.e. avoids duplication of codes

Different modularization techniques available are:

1. SUBROUTINE
2. FUNCTION MODULE
3. METHODS
4. INCLUDES AND MACROS

METHODS: It describes the functions and behavior of classes and their instances in ABAP objects methods must be defined in classes.

MACROS: Macros designed based on place holder (place holder works like pointers in c language but in case of place holder changes effect on output.

INCLUDES: If I am defining the same variables in many programs, instead of defining in all programs we have define all the variables in one program and that program is included or added to the programs whenever needed that’s called include program. It cannot be executed independently it has to include in a program.

SUBROUTINES:
A subroutine is a reusable section of code. It’s like a mini program that can be called from another point in your program. Subroutine is generally for local modularization i.e. they are generally called from the program in which they defined.

We can use subroutine to write functions that are used repeatedly with in a program. You can define subroutine in any ABAP programs.

Syntax for defining subroutine

FORM (subroutine name) (parameter (Optional))
-----------------------------------
----------------------------------
-----------------------------------
END FORM.

Syntax for calling subroutines

PERFORM (subroutine name)(parameter)

Subroutines cannot be nested, therefore place subroutine definitions at the end at the program. One subroutines can call other subroutines and may also call themselves (recursive). Once a subroutine has finished running, the calling program carries on processing after the perform statement.

There are two types of subroutines:

1) Internal subroutine: If the source code or body of the subroutine will be in the same ABAP/4 program i.e. in the calling program which is called internal subroutine.

2) External subroutine: If the source code or body of the subroutine present other than the calling program which is called external subroutine. An external subroutine is one that resides in a different program that the perform statement that calls it.

Syntax for calling external subroutines:
Report ztn1811
Perform (S1) (ztn1811)

Report ztn1811
FORM s1
-----------
----------
ENDFORM.

PARAMENTERS IN SUBROUTINES

Parameters can be either local or reference to global variables. The memory for local parameters is allocated when the subroutine is called & freed when it ends. If we define variables on the form statement, the perform statement must pass a value to each of these variables.

Global variables: A global variable is one that is defined outside of a subroutine by using the tables or data statement. It can be accessed from any point in the program be it inside an event or inside a subroutine.

Local variables: A local variables is a variable that is defined inside a subroutine using local data or static’s statement. It is said to be local to subroutine.
The two types of parameters are:

Formal parameters: Parameter names that appear on the form statements are called formal parameters.

Ex: FORM s1, using P1 changing P2, P3
Here P1, P2, P3 are called formal parameters.

Actual parameters: Parameter names that appears on the perform statement are called actual parameters.

Ex: PERFORM S1 using P1 changing P2, P3.
Here P1, P2, P3 are called actual parameters.

There are three ways of passing parameters to a subroutine.

1) Pass by reference
2) Pass by value
3) Pass by value & result

1) Passing parameters by reference

During subroutine call, only the address of the actual parameter is transferred to the formal parameters i.e. pointer to the original memory or address location is passed. The formal parameter has no memory of its own & we work with the fields of the calling program with in a subroutine. So changes to the variable within the subroutine update the original memory location immediately i.e. the field contents in the calling program also changes.
Ex: Report xyz.
Data F1 value A.
Performs s1 using F1.
Write: / F1.

Form s1 using P1
P1 = X.
Write:/ P1.
End form

Here: Memory address for F1 is 1000 (Example)
F1 = A before calling s1
F1 = X after assignment in s1
P1 – P1 is a pointer to memory location 1000
O/p P1= x
F1= x
2. Passing parameters by value :

When you pass a parameters by value, new memory is allocated for the value i.e. the formal parameters are created as copies of the actual parameters, thus formal parameters have memory of their own i.e. the memory allocated when the subroutine is called and freed when the subroutine returns. Therefore changes to the formal parameters have no effect on the actual parameters.

EX: Report xyz
Data: F1 value A.
Perform s1 using F1.
Write: / F1.

From s1 using value (P1)
P1 =X.
Write: / P1.
End form.
Here: F1 = A before call to s1
F1 = A after assignment in s1
0/P P1 = X
F1 = A

3. Passing parameters by value & result:

Pass by value & result is similar to pass by reference like Pass by value, a new memory area is allocated & it frees when the subroutine ends. When the end form statement executes, it copies the value of the local memory area back into the original memory area changes to the parameter with in the subroutine are reflected in the original but not until subroutine returns.

Ex: Report xyz
Data: F1 value A
PERFORM: S1 changing F1
S2 changing F1
End of selection
Write: / F1.
Form s1 changing value (P1)
P1 =B.
Write:/ P1.
End form
Form S2 changing value (P2)
P2 = ‘X’.
Write: / P2.
Stop.
End form
0/P P1 = B
P2 = X
F1 = B

Leaving subroutine
You can exit out of a subroutine at any time using the following statements.

1) Stop: Immediately leaves the subroutine & goes directly to the end of selection event.
2) Exit: It leaves the loop, subroutine or comes out of the program & display the result without any condition.
3. Check: It also comes or immediately leaves the subroutine but it depends on logical expression. If it is false comes out of the loop or subroutine.

2 comments:

  1. printing maths tables from 1-10.


    REPORT ZANIL10.
    data: begin of anil,
    a type i,
    b type i,
    c type i,
    d type i,
    e type i,
    f type i,
    g type i,
    h type i,
    i type i,
    j type i,
    end of anil.

    data: itab like standard table of anil.

    perform action changing itab.

    perform output using itab.


    form action changing anil_1 like itab.

    data: anil_2 like line of itab.
    do 20 times.

    anil_2-a = sy-index.
    anil_2-b = sy-index * 2.
    anil_2-c = sy-index * 3.
    anil_2-d = sy-index * 4.
    anil_2-e = sy-index * 5.
    anil_2-f = sy-index * 6.
    anil_2-g = sy-index * 7.
    anil_2-h = sy-index * 8.
    anil_2-i = sy-index * 9.
    anil_2-j = sy-index * 10.

    append anil_2 to itab.

    enddo.
    endform.

    form output using value(anil_1) like itab.
    data: anil_2 like line of itab.
    LOOP AT anil_1 INTO anil_2.
    WRITE: / anil_2-a, anil_2-b,anil_2-c,anil_2-d,anil_2-e,anil_2-f,anil_2-g,anil_2-h,anil_2-i,anil_2-j.
    ENDLOOP.

    ReplyDelete