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: 

Function Module

Former Member
0 Kudos

Hi Friends,

I need some example for function modules.

I am not having any type of idea about FM.

I need some exapmle of cretiog function modules.

Steps to create function modules.

Please some informatio regarding this.

Thank You.....

Regards,

Kalidas Thirumoorthy

5 REPLIES 5

Former Member
0 Kudos

hi,

Function modules are special external subroutines(program type F)

Function modules are classified in function groups and stored in the Function Library. Function groups act as containers for function modules that logically belong together.

Function modules allow us 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.

The R/3 System provides numerous predefined function modules that we can call from your ABAP/4 programs. We can also create your own function modules using Function Builder. (Transaction Code SE 37)

Function groups are containers for function modules.

We cannot execute a function group. When we call a 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).

Function group names are freely definable up to a maximum length of 26 alphanumeric characters.

When we create a function group or function module in the Function Builder , the main program and include programs are generated automatically.

Creating Function Groups

New function groups can be created from the menu Goto - Function Group - Create

To program a function module, we must include our statements between the FUNCTION and ENDFUNCTION statements as follows:

Syntax : FUNCTION <module>

<statements>

ENDFUNCTION

An existing Function Module can be inserted into ABAP Code using Edit - Pattern

Components of Functional Modules

Import: Values transferred from the calling program to the function module.You cannot

overwrite the contents of import parameters at runtime.

Export: Values transferred from the function module back to the calling program

Changing :Values that act as import and export parameters simultaneously. The original

value of a changing parameter is transferred from the calling program to the function module.

The function module can alter the initial value and send it back to the calling program.

Tables: Internal tables that can be imported and exported. The internal table's contents are transferred from the calling program to the function module. The function module can alter the contents of the internal table and then send it back to the calling program. Tables are always passed by reference.

Exceptions:Error situations that can occur within the function module. The calling program uses exceptions to find out if an error has occurred in the

function module. It can then react accordingly.

Creating Function Modules

Check whether a suitable function module already exists.

If not, create one as follows.

Create a function group, if no appropriate group exists yet.

Create the function module.

Define the function module interface by entering its parameters and

exceptions.

Write the actual ABAP code for the function module, adding any relevant

global data to the TOP include.

Activate the module.

Test the module.

Document the module and its parameters for other users.

Release the module for general use.

regards,

sreelakshmi

Former Member
0 Kudos

Hi Kalidas,

try the following link for an overview of function modules

Link: [http://help.sap.com/saphelp_47x200/helpdata/en/9f/db988735c111d1829f0000e829fbfe/content.htm]

Former Member
0 Kudos

Hi,

A simple and esy way ti understand,

FUNCTION MODULES:

-


  • It is a modularization technique.

  • It can be called from any different clients.

  • It can return a value.

FUNCTION GROUP:

-


It is a container of user-defined function modules. Function modules created should be specified a function group, where it can be accessed globally then.

To create a function group, use following navigations:

SE80 -> Select Function Group from drop-down list box -> Specify function group name starting with Z or Y -> Press Enter -> Click on Yes to create object -> Enter short description -> Save under a package -> Assign a request number -> A function group is created in a function library.

TYPES OF FUNCTION MODULES:

-


1. NORMAL FUNCTION MODULE - It can be called only from within the same client.

2. REMOTE-ENABLED FUNCTION MODULE - It can be called either from within the same client or different client.

Navigations to create a function module:

-


SE37 -> Opens Function Builder Screen -> Specify function module name starting with Z or Y -> Click on Create -> Opens an interface -> Specify function group -> Enter short description -> Click on Continue -> Discard the message -> Opens Function Builder Interface -> In the IMPORT tab button, specify the following parameters:

A TYPE I

B TYPE I

In the EXPORT tab button, specify the following parameter:

C TYPE I

In the SOURCE CODE tab button, write the following arithmetic operation statement:

C = A + B.

Save the FM -> Activate.

To invoke the function module from the program, use 'CTRL+F6' shortcut key. Write the following code:

DATA : X TYPE I VALUE '100', Y TYPE I VALUE '200', Z TYPE I.

CALL FUNCTION 'ZBASKAR' (CTRL+F6)

EXPORTING

A = X

B = Y

IMPORTING

C = Z .

WRITE Z.

Save -> Activate -> Execute.

PASSING INTERNAL TABLE AS AN ARGUMENT:

-


Create a FM with the above navigations. In the TABLES tab button, specify the following parameter:

ITAB LIKE KNA1.

In the SOURCE CODE tab button, specify the following code:

LOOP AT ITAB.

WRITE 😕 ITAB-KUNNR, ITAB-NAME1, ITAB-LAND1, ITAB-ORT01.

ENDLOOP.

Save -> Activate.

In the SE38 editor, write the following code to pass internal table as an argument:

DATA KTAB LIKE KNA1 OCCURS 0 WITH HEADER LINE.

SELECT * FROM KNA1 INTO TABLE KTAB .

CALL FUNCTION 'ZBASKAR'

TABLES

ITAB = KTAB.

Save -> Activate -> Execute.

TO CHANGE THE VALUE OR CHARACTER OF A VALUE (UPPERCASE TO LOWER CASE OR VICE VERSA):

-


Click on CHANGING tab button, specify the following parameter:

STR TYPE C.

In the SOURCE CODE tab button, specify the following code:

TRANSLATE STR TO UPPER CASE.

Save -> Activate.

In the SE38 editor, write the following code:

DATA A(10) VALUE 'karthik'.

CALL FUNCTION 'ZBASKAR'

CHANGING

STR = A .

write a.

Save -> Activate -> Execute.

EXCEPTION HANDLING IN FUNCTION MODULE:

-


Click on EXCEPTIONS tab button, specify the following parameters:

TABLE_LESS_SIZE

TABLE_MORE_SIZE

Click on SOURCE CODE tab button, specify the following code:

IF SY-TFILL < 10.

RAISE TABLE_LESS_SIZE.

ELSEIF SY-TFILL > 10.

RAISE TABLE_MORE_SIZE.

ENDIF.

Save -> Activate.

In SE38 editor, write the following code:

DATA KTAB LIKE KNA1 OCCURS 0 WITH HEADER LINE.

SELECT * FROM KNA1 INTO TABLE KTAB UP TO 5 ROWS.

CALL FUNCTION 'ZBASKAR'

EXCEPTIONS

TABLE_LESS_SIZE = 1

TABLE_MORE_SIZE = 2

OTHERS = 3 .

DESCRIBE TABLE KTAB.

IF SY-SUBRC = 1.

WRITE 😕 'INTERNAL TABLE SIZE IS LESS'.

ELSEIF SY-SUBRC = 2.

WRITE 😕 'INTERNAL TABLE SIZE IS MORE'.

ELSEIF SY-SUBRC = 3.

WRITE 😕 'INTERNAL TABLE HAS CORRECT NUMBER OF VALUES'.

ENDIF.

Some of the common function modules used in BDC and Reports:

-


BDC_OPEN_GROUP

BDC_CLOSE_GROUP

BDC_INSERT

UPLOAD

DOWNLOAD

WS_UPLOAD

WS_DOWNLOAD

REUSE_ALV_GRID_DISPLAY

REUSE_ALV_LIST_DISPLAY

Hope this helps u,

Regards,

Arunsri

Former Member
0 Kudos

HI

creating function module:-

called program - se37-creating funcgrp,funcmodule by assigning attributes,importing,exporting,tables,exceptions.

calling program - SE38-in pgm click pattern and write function name- provide export,import,tables,exception values.

Function modules are special external subroutines stored in a central library. The R/3 system provides numerous predefined function modules that you can call from your ABAP/4 programs, and plus you can create your own function modules.

The main difference between a function module and a normal ABAP/4 subroutine is as follows:

Function is stored in central library and has global presence while subroutines are confined to a particular program. Subroutine cannot return values while functions can return values. Unlike functions, subroutine cannot handle exceptions. And last but not least, the difference in the way the parameters are passed to functions.

Declaring data as common parts is not possible for function modules. The calling program and the called function module have separate work in ABAP/4 Dictionary tables.

You use the ABAP/4 Workbench to call, create, and maintain function modules.

You can combine several function modules to form a function group in the function library. Since you can define global data that can be accessed from all function modules of one function group, it is reasonable to include function modules that operate with the same data, for example internal table for sales module can be grouped, in one function group.

Via the ABAP/4 Development Workbench screen, choose Development &#61614; Function Library or select Function Library in the application toolbar or use se37 transaction code.

The function library maintains Function Modules, the screen displays following components:

All function names should start with Z_ or Y_ followed by any name.

• Source code

• Documentation

• Administrative info

• Import-export parameters

• Table parameters and exception interface

• Global data

• Main program

(Not necessarily in the same order)

Documentation

The documentation describes the purpose of the function module, lists the parameters for passing data to and from the module, and the exceptions. The parameters of the parameter type I are import parameters, which are used to pass data to the function module. Parameters of the parameter type E are export parameters, which are used to pass data from, the function module to the calling program. Exceptions describe error scenarios, which can occur in function modules.

Import-export parameters

Import parameters correspond to the formal input parameter of subroutines. They pass data from the calling program to the function module.

Export parameters correspond to the formal output parameters of subroutines. They pass data from the function module back to the calling program (which his not possible in subroutines)

Table parameters

Table parameters are internal tables. Internal tables are treated like changing parameters and are always passed by reference.

Exceptions

Exceptions are used to handle error scenarios, which can occur in function modules. The function module checks for any type of error and raise exception and returns SY-SUBRC to the calling program. Main program checks this SY-SUBRC for any errors that have occurred and then takes action accordingly.

Source Code

The ABAP/4 Edit screen displays the ABAP/4 source code of the function module. You can work with the source code in the same way as you would work with normal ABAP/4 programs.

Import parameters, changing parameters, and table parameters can be Optional. This means that you can omit the corresponding actual parameter when you call the function in the calling program. If the parameter is optional and the actual parameter is not specified, you can specify a default value for use in the function module. Export parameters are always optional.

As with subroutines, you can specify the data types of the formal parameters in the field Reference type. In the field Ref. structure, you can specify ABAP/4 Dictionary reference structures or fields. Then, the system checks the current parameter against the structure or field at runtime.

Testing of function module

You can test a function module without calling it from an ABAP/4 program via the Function Library: Maintain Function Modules screen by choosing Single test. You can assign values to the import parameters on the Test Function Modules screen.

Calling Function Modules

To call a function module from an ABAP/4 program, use the CALL statement as follows:

Syntax:

CALL FUNCTION <module>

[EXPORTING

f1 = s1

f2 = s2

fn = sn (parameters which you pass from program to function are

s1, s2, sn)]

[IMPORTING

f1 = r1

f2 = r2

fn = rn (parameters which program receives you pass from function in

r1, r2, rn)]

[TABLES f1 = a1 … fn = an]

EXCEPTIONS notvalid = 1

not correct = 2

OTHERS = 5].

You can specify the name of the function module <module> as a literal or as a variable. Parameters are passed to and from the function module by exactly assigning the actual parameters to the formal parameters in the lists after the EXPORTING, IMPORTING, TABLES.

If in your function if you have raised exception not valid then this exception can be handled in main program. Functions return different sy-subrc for different exceptions.

Edited by: Jyothsna M on Mar 4, 2008 10:00 AM

Former Member
0 Kudos

Hi Kalidas,

Function Module can be created in SE37.

Please check this Link. This must be helpful.

http://abaplovers.blogspot.com/2008/02/creating-function-module-in-sap-abap.html

For some predefined functionalities we have Function Modules. For eg. You need not write code from the scratch to Add two numbers. All that you need to do is pass the values to the FM which already exists in your SAP System. In an ABAP program

you call the FM by clicking on the pattern and giving the name of the FM that you want to use.

Regards,

Sai