Beruflich Dokumente
Kultur Dokumente
SUBROUTINES
Re usable section of a code . Can be called by a program internally or externally . Written within FORM ENDFORM statement and called by PERFORM statement .
<form2> <form3> [tables t1 t2 .] [using c1 c2 ..] [changing c3 c4 .] <no> specifies the number of subroutine from subroutine list which is to be called .
Leaving Subroutines
oCheck oExit oStop
Check and exit statement leave the subroutine and control continues with the next executable statement following perform .
Stop statement leaves the subroutine and goes directly to the end-of-selection block .
Variables In Subroutine
Global
Local
Local
Data
Statics
Global data are accessible both from inside and outside the subroutine .
Local variables , local are accessible to other subroutines called by the subroutine .Local and data variables are generated and memory alloted to them when the subroutine generates and die down as the subroutine dies down .
For static , the memory alloted to it is retained even after the subroutine ends . The next time the subroutine is called , the memory for that variable becomes visible again and it will contain that value which was assigned to it the last time .
Static variable is visible to the subroutine itself , not within the subroutine which is called by the subroutine .
Formal Parameters can be : Typed : Have data type following its name in the
FORM statement .
Description
Passes a pointer to the original memory location -Do-
Changing v1
Using value(v1)
Pass by Value
Allocates a new memory location for use within the subroutine , which is freed when the subroutine ends
Similar to pass by value , but the contents of the new memory is copied back into original memory before returning .
Changing value(v1)
Form <form1> changing it[] By reference Form <form1> using value (it[]) Form <form1> changing value (it[]) By value By value and result
Making Components of Internal Table Known Within The Subroutine Method Syntax Valid Field For X
Tables it structure X
Tables it like X
Field string/DDIC structure/DDIC table/Internal table with header line Internal table without header line/internal table body(it[]) Internal table without header line/internal table body(it[])
Output
Internal table without header line passed to subroutine as if it have a header line
DISADVANTAGES
A global variable with same name in both programs , defined by Tables statement is common to both programs . A change to this variable in one program affects the other .
INCLUDE
Syntax : include <includename> . Means of modularizing source code , with no parameter interface . Allows to use the same source code in different programs . Program attribute is I .
Macro
DEFINE <macro>.
<statements>
END-OF-DEFINITION.
Up to nine placeholders (&1, &2, ..., &9). .Macro should be defined before the point of its use in the program .
When the program is generated, macro definitions are not taken into account at the point at which they are defined. To use a macro :
<macro> [<p1> <p2> ... <p9>].
Example
I want to create a macro , which will draw a box surrounding the value of my variable . I will provide the value of the variable and approximate length of the box .
The name of my macro is write_subhendu and I pass some values to it for creating boxes by :write_subhendu : line1-tabname 10 , line1-ddtext 60 .
data : x type i , y type i , l type i ,l1 type i ,l2 type i ,matnr type
i , cmatnr type c . DEFINE write_subhendu . x = sy-colno . y = sy-linno . skip 2 . l = &2 . l2 = strlen( &1 ) . position x . y = y - 1 . skip to line y . uline at x(l1) . y = y + 1 . position x . write : '|' , &1(l) , '|' . y = y + 1 . skip to line y . l1 = l + 4 .
skip to line y .
uline at x(l1) .
x = x + l1 - 1 . END-OF-DEFINITION
y = y - 1 .
position x . .
skip to line y .
Function Modules
Exist as an external program .
Enable parameter to be passed and returned , and parameters can be passed by value , by value and result and by reference . Leaving a function module is done by RAISE statement . Name should be a mimimum of three characters and maximum of 30 characters , and customer function modules should start with Y_ or Z_ ( the underscore is desirable , but not mandatory) .
Allows to encapsulate and reuse global functions in the R/3 System. Function modules also play an important role in database updates and in remote communications between R/3 Systems or between an R/3 System and a non-SAP system.
Function Group
Function Group is a program that contains function modules . It contains a four character identifier , called function group ID , which must begin with Y or Z for customer function groups . A function group containes :-
A main program ( saplsubh , where subh is the name of the function group) .
System places two includes within it automatically :o o include lsubhtop include lsubhuxx
Import parameters
Must be supplied with data when function module is called unless they are flagged as optional. They cannot be changed in the function module . Export parameters Pass data from the function module back to the calling program. Always optional .
Changing parameters Must be supplied with data when function module is called , unless they are flagged as optional. Can be changed in the function module and the changed values are then returned to the calling program. Tables parameters
Used to pass internal tables and treated like CHANGING parameters. However, internal tables can also be passed with other parameters if parameter type is specified appropriately .
The type of Interface Parameters(import , export, changing,tables) can be specified either by referring to ABAP Dictionary types or elementary ABAP types. Interface parameters are, by default, passed by value. They can also be passed by reference. Tables parameters can only be passed by reference. Default values can be assigned 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
Used to handle errors that occur in function modules. The calling program checks whether any errors have occurred and then takes action accordingly.
After EXPORTING, export parameters can be received from the function module by assigning them to variables of the appropriate type. After IMPORTING, all non-optional import parameters must be supplied with values appropriate to their type. Values to optional import parameters may also be supplied . After CHANGING or TABLES, values to all of the nonoptional changing or tables parameters must be supplied. When the function module has finished running, the changed values are passed back to the actual parameters. Values to optional changing or tables parameters may be supplied. By tables parameters , internal table can be passed with header line .Others(exporting/importing/changing)passes body only .
EXCEPTIONS option is used to handle the exceptions of the function module. If an exception <e i > 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 <e i > is specified in the EXCEPTION option, the calling program handles the exception by assigning <r i > to SY-SUBRC. <r i > must be a numeric literal.
If ERROR_MESSAGE is specified in the exception list the message handling of function modules can be influenced . With ERROR_MESSAGE one can ask 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 <r E >). If OTHERS is specified after EXCEPTIONS, the system assigns a single return code to all other exceptions that you have not specified explicitly in the list.
xname
t is the message type(e,w,I,s,a,x) , nnn is the message no.,cc is the message class,c1 and v2 are the values to be inserted into the message text , xname is the name of the exception to be raised .
It sets the following system fields in caller program: Message class SY-MSGID Message type SY-MSGTY Message number SY-MSGNO SY-MSGV1 to SY-MSGV4 (contents of fields v1 to v4 included in a message).