Application Development 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: 

ABAP

Former Member
0 Kudos
71

How to use Subroutines more effectively?

6 REPLIES 6

matt
Active Contributor
0 Kudos
51

Don't reference global variables in subroutines - always use the interface (i.e. parameters) to pass information into and out of the subroutines. Modularise early and often - build your development out of subroutines. Make sure that you modularise at the right level of detail.

For preference - don't use subroutines - use class methods. That way, even if you only use static method calls, the parameters are named both in the "called" and the "caller".

matt

former_member196299
Active Contributor
0 Kudos
51

hi ,

Along with the above note , some more things things can be taken care : Use subroutines for a specific logic, make use of the parameters most of the time .

Regards,

Ranjita

0 Kudos
51

in oo ABAP when ever u want to write internal table and separate work area then u can write subroutines to eliminate extra workarea and alse some times logic necessary to update parameters by call by reference then use subroutines.

Former Member
0 Kudos
51

Hi ,

subroutine mainly used to modulurized the logic.

so first of all variable declartion is very important , try to pass the global variable through interface and minimize passing the variable by value , it should be pass by refrence .

regards

Deepak.

Former Member
0 Kudos
51

You can use sub-routines more effectively by using use of sub-routine pools.

this is like this:

Subroutine Pools

Subroutine pools, as the name implies, were created to contain selections of subroutines that can be called externally from other programs. Before release 6.10, this was the only way subroutine pools could be used. But besides subroutines, subroutine pools can also contain local classes and interfaces. As of release 6.10, you can connect transaction codes to methods. Therefore, you can now also call subroutine pools via transaction codes. This is the closest to a Java program you can get in ABAP: a subroutine pool with a class containing a method – say – main connected to a transaction code.

Subroutine pools are created using the ABAP Editor and are introduced with the PROGRAM statement. They may not contain any screens of their own, and with the exception of the LOAD-OF-PROGRAM event block they may only use subroutines as processing blocks. Subroutine pools are loaded by externally calling their subroutines from within other ABAP programs.

DATA: CODE(72) OCCURS 10,

PROG(8), MSG(120), LIN(3), WRD(10), OFF(3).

APPEND 'PROGRAM SUBPOOL.'

TO CODE.

APPEND 'FORM DYN1.'

TO CODE.

APPEND

'WRITE / ''Hello, I am the temporary subroutine DYN1!''.'

TO CODE.

APPEND 'ENDFORM.'

TO CODE.

APPEND 'FORM DYN2.'

TO CODE.

APPEND

'WRIT / ''Hello, I am the temporary subroutine DYN2!''.'

TO CODE.

APPEND 'ENDFORM.'

TO CODE.

GENERATE SUBROUTINE POOL CODE NAME PROG

MESSAGE MSG

LINE LIN

WORD WRD

OFFSET OFF.

IF SY-SUBRC <> 0.

WRITE: / 'Error during generation in line', LIN,

/ MSG,

/ 'Word:', WRD, 'at offset', OFF.

ELSE.

WRITE: / 'The name of the subroutine pool is', PROG.

SKIP 2.

PERFORM DYN1 IN PROGRAM (PROG).

SKIP 2.

PERFORM DYN2 IN PROGRAM (PROG).

ENDIF.

In this program, a subroutine pool containing two subroutines is placed into table CODE. Note that the temporary program must contain a REPORT or PROGRAM statement. Statement GENERATE SUBROUTINE POOL generates the temporary program.

A generation error occurred since the WRITE statement has been misspelled in the second subroutine, DYN2. After that line has been revised:

APPEND

'WRITE / ''Hello, I am the temporary subroutine DYN2!''.'

TO CODE.

Generation was successful. The internal program name is displayed. Then, the subroutines of the subroutine pool are called. Note that you do not need to know the program name to call the subroutines.

with regards,

Hema Sundara.

Former Member
0 Kudos
51

A subprogram may find it useful to make use of a certain amount of "scratch" space; that is, memory used during the execution of that subprogram to hold intermediate results. Variables stored in this scratch space are referred to as local variables, and the scratch space itself is referred to as an activation record. An activation record typically has a return address that tells it where to pass control back to when the subprogram finishes.

A subprogram may have any number and nature of call sites. If recursion is supported, a subprogram may even call itself, causing its execution to suspend while another nested execution of the same subprogram occurs. Recursion is a useful technique for simplifying some complex algorithms, and breaking down complex problems. Recursive languages generally provide a new copy of local variables on each call. If the programmer desires the value of local variables to stay the same between calls, they can be declared "static" in some languages, or global values or common areas can be used.