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

Function Groups and Function Modules

Former Member
0 Likes
1,833

Hi,

Can anyone give me the detail steps for creating Function Group and then from that function group creation of function module with example?

Regards,

Chandru

1 REPLY 1
Read only

Former Member
0 Likes
1,022

Hi,

Function Group creation -

A function group is a program that contains function modules. With each R/3 system, SAP supplies more than 5,000 pre-existing function groups.

In total, they contain more than 30,000 function modules. If the functionality you require is not already covered by these SAP-supplied function modules, you can also create your own function groups and function modules.

We can put all the relevant function modules under one function group and all the global variables can be declared in this FG.

FG Creation:

1) Function group can be created in SE80. There choose the 'Function Group' from the list of objects.

2) Then give a name for ur function group (starts with Y or Z) and press ENTER.

3) The click 'YES' in the create object dialog box and give a short desc. for this FG and save.

Function Module:

A function module is the last of the four main ABAP/4 modularization units. It is very similar to an external subroutine in these ways:

Both exist within an external program.

Both enable parameters to be passed and returned.

Parameters can be passed by value, by value and result, or by reference.

The major differences between function modules and external subroutines are the following:

Function modules have a special screen used for defining parameters-parameters are not defined via ABAP/4 statements.

tables work areas are not shared between the function module and the calling program.

Different syntax is used to call a function module than to call a subroutine.

Leaving a function module is accomplished via the raise statement instead of check, exit, or stop.

A function module name has a practical minimum length of three characters and a maximum length of 30 characters. Customer function modules must begin with Y_ or Z_. The name of each function module is unique within the entire R/3 system.

Defining Data within a Function Module

Data definitions within function modules are similar to those of subroutines.

Within a function module, use the data statement to define local variables that are reinitialized each time the function module is called. Use the statics statement to define local variables that are allocated the first time the function module is called. The value of a static variable is remembered between calls.

Define parameters within the function module interface to create local definitions of variables that are passed into the function module and returned from it (see the next section).

You cannot use the local statement within a function module. Instead, globalized interface parameters serve the same purpose. See the following section on defining global data to learn about local and global interface parameters.

Defining the Function Module Interface

To pass parameters to a function module, you must define a function module interface. The function module interface is the description of the parameters that are passed to and received from the function module. It is also simply known as the interface. In the remainder of this chapter, I will refer to the function module interface simply as the interface.

To define parameters, you must go to one of two parameter definition screens:

1) Import/Export Parameter Interface

2) Table Parameters/Exceptions Interface

Then in the FM interface screen, give the following

1) Import parameters

2) Export parameters

3) Changing parameters

Then give

1) Define internal table parameters

2) Document exceptions

You enter the name of the parameter in the first column and the attributes of the parameter in the remaining columns. Enter one parameter per row.

Import parameters are variables or field strings that contain values passed into the function module from the calling program. These values originate outside of the function module and they are imported into it.

Export parameters are variables or field strings that contain values returned from the function module. These values originate within the function module and they are exported out of it.

Changing parameters are variables or field strings that contain values that are passed into the function module, changed by the code within the function module, and then returned. These values originate outside the function module. They are passed into it, changed, and passed back.

Table parameters are internal tables that are passed to the function module, changed within it, and returned. The internal tables must be defined in the calling program.

An exception is a name for an error that occurs within a function module. Exceptions are described in detail in the following section.

Syntax for the call function Statement

The following is the syntax for the call function statement.

call function 'F'

[exporting p1 = v1 ... ]

[importing p2 = v2 ... ]

[changing p3 = v3 ... ]

[tables p4 = it ... ]

[exceptions x1 = n [others = n]].

where:

F is the function module name.

p1 through p4 are parameter names defined in the function module interface.

v1 through v3 are variable or field string names defined within the calling program.

it is an internal table defined within the calling program.

n is any integer literal; n cannot be a variable.

x1 is an exception name raised within the function module.

The following points apply:

All additions are optional.

call function is a single statement. Do not place periods or commas after parameters or exception names.

The function module name must be coded in uppercase. If it is coded in lowercase, the function will not be found and a short dump will result.

Use the call function statement to transfer control to a function module and specify parameters. Figure 19.9 illustrates how parameters are passed to and received from the function module.

sample FM

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

  • EXPORTING

  • I_INTERFACE_CHECK = ' '

  • I_BYPASSING_BUFFER = ' '

  • I_BUFFER_ACTIVE = ' '

  • I_CALLBACK_PROGRAM = ' '

  • I_CALLBACK_PF_STATUS_SET = ' '

  • I_CALLBACK_USER_COMMAND = ' '

  • I_CALLBACK_TOP_OF_PAGE = ' '

  • I_CALLBACK_HTML_TOP_OF_PAGE = ' '

  • I_CALLBACK_HTML_END_OF_LIST = ' '

  • I_STRUCTURE_NAME =

  • I_BACKGROUND_ID = ' '

  • I_GRID_TITLE =

  • I_GRID_SETTINGS =

  • IS_LAYOUT =

  • IT_FIELDCAT =

  • IT_EXCLUDING =

  • IT_SPECIAL_GROUPS =

  • IT_SORT =

  • IT_FILTER =

  • IS_SEL_HIDE =

  • I_DEFAULT = 'X'

  • I_SAVE = ' '

  • IS_VARIANT =

  • IT_EVENTS =

  • IT_EVENT_EXIT =

  • IS_PRINT =

  • IS_REPREP_ID =

  • I_SCREEN_START_COLUMN = 0

  • I_SCREEN_START_LINE = 0

  • I_SCREEN_END_COLUMN = 0

  • I_SCREEN_END_LINE = 0

  • I_HTML_HEIGHT_TOP = 0

  • I_HTML_HEIGHT_END = 0

  • IT_ALV_GRAPHICS =

  • IT_HYPERLINK =

  • IT_ADD_FIELDCAT =

  • IT_EXCEPT_QINFO =

  • IR_SALV_FULLSCREEN_ADAPTER =

  • IMPORTING

  • E_EXIT_CAUSED_BY_CALLER =

  • ES_EXIT_CAUSED_BY_USER =

TABLES

t_outtab =

  • EXCEPTIONS

  • PROGRAM_ERROR = 1

  • OTHERS = 2

.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

Example

1 report ztx1905.

2 parameters: op1 type i default 2, "operand 1

3 op2 type i default 3. "operand 2

4 data rslt type p decimals 2. "result

5

6 call function 'Z_TX_DIV'

7 exporting

8 p1 = op1

9 p2 = op2

10 importing

11 p3 = rslt.

12

13 write: / op1, '/', op2, '=', rslt.

Regards,

Shanthi.P

          • Reward points if useful ****

Edited by: shanthi ps on Jan 26, 2008 12:03 PM