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 modules and subroutines

Former Member
0 Kudos

difference between function modules and subroutines. as both are modularizing techniques and reusable components. what is the exact difference between FM and subroutines.

6 REPLIES 6

Former Member
0 Kudos

Hi,

Subroutines are procedures that you can define in any ABAP program and also call from any program. Subroutines are normally called internally, that is, they contain sections of code or algorithms that are used frequently locally. If you want a function to be reusable throughout the system, use a function module.

Function modules are procedures that are defined in function groups (special ABAP programs with type F) and can be called from any ABAP program. Function groups act as containers for function modules that logically belong together. You create function groups and function modules in the ABAP Workbench using the Function Builder.

Function modules allow you to encapsulate and reuse global functions in the R/3 System. They are stored in a central library. The R/3 System contains a wide range of predefined function modules that you can call from any ABAP program. 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.

Unlike subroutines, you do not define function modules in the source code of your program. Instead, you use the Function Builder. The actual ABAP interface definition remains hidden from the programmer. You can define the input parameters of a function module as optional. You can also assign default values to them. Function modules also support exception handling. This allows you to catch certain errors while the function module is running. You can test function modules without having to include them in a program using the Function Builder.

The Function Builder also has a release process for function modules. This ensures that incompatible changes cannot be made to any function modules that have already been released. This applies particularly to the interface. Programs that use a released function module will not cease to work if the function module is changed.

• ABAP/4 allows you to define and call subroutines. You can even call subroutines of other programs. There are different ways of how to pass parameters to and from the Subroutines.

• ABAP/4 contains a special type of subroutine, called function module. Function modules are stored and maintained in a central library. They have clearly defined data interfaces to the calling program. You can test function modules in a stand-alone mode independent of the calling program.

• Function library Defining and maintaining the ABAP/4 function modules. Transaction code is SE37.

Regards,

Sruthi

Former Member
0 Kudos

Hi,

In contrast to normal subroutines function modules have uniquely defined interface. Declaring data as common parts is not possible for function modules. Function modules are stored in a central library.

http://wiki.ittoolbox.com/index.php/FAQ:What_are_Modularization_Techniques%3F

Regards

Sudheer

Former Member
0 Kudos

Hi vamsi,

1. FM are Global . Any program can use it.

where else for subroutines, we have to use the INCLUDE.

2. Most important -

FMs can be tested thru se37

whereas for testing subroutines, we have to write some abap code.

3. FMS have WELL DEFINED interface, and it can be documented for each parameter.

Whereas subroutines do not have such great facility.

4. FMs can be update task, RFC etc.

whereas subroutines are much limited.

regards,

amit m.

Former Member
0 Kudos

Function modules are procedures that are defined in function groups (special ABAP programs with type F) and can be called from any ABAP program. Function groups act as containers for function modules that logically belong together,Function modules allow you to encapsulate and reuse global functions in the R/3 System. They are stored in a central library,Unlike subroutines, you do not define function modules in the source code of your program.

Subroutines are procedures that you can define in any ABAP program and also call from any program. Subroutines are normally called internally, that is, they contain sections of code or algorithms that are used frequently locally. If you want a function to be reusable throughout the system, use a function module

Regards,

Santosh

Former Member
0 Kudos

Adding to the above all points ....

We can handle EXCEPTIONS in case of function modules which is not possible in case of sub routinesss... This is a great advantage!!

Former Member
0 Kudos

hi,

subroutine - same like copying the part.

when we call a subroutine that rountine is simply copied to the called area.control is still with the program

function module - control will be passed to the function.

after execution the control is given back to the program.

________________________________________________________________

Function modules are procedures that are defined in function groups (special ABAP programs with type F) and can be called from any ABAP program. Function groups act as containers for function modules that logically belong together,Function modules allow you to encapsulate and reuse global functions in the R/3 System. They are stored in a central library,Unlike subroutines, you do not define function modules in the source code of your program.

Subroutines are procedures that you can define in any ABAP program and also call from any program. Subroutines are normally called internally, that is, they contain sections of code or algorithms that are used frequently locally. If you want a function to be reusable throughout the system, use a function module

__________________________________________________________________

Subroutines are procedures that you can define in any ABAP program and also call from any program. Subroutines are normally called internally, that is, they contain sections of code or algorithms that are used frequently locally. If you want a function to be reusable throughout the system, use a function module.

The function group of the function module is loaded into an internal session of the context, and is retained. What this means is that, if repeated calls of function modules belonging to the same destination and the same function group are made, the global data of this function group can be accessed collectively. When functions are called in external systems, this behavior is simulated by the API of the RFC library. A connection and its context is retained until it is explicitly closed, or until the calling program is finished. To close a connection explicitly, the function module RFC_CONNECTION_CLOSE can be used, or the API functions RfcAbort and RfcClose.

___________________________________________________________________

Basically they do the same thing and purpose is almost same but tecnically the are way apart:

  • Function modules must belong to a pool called a function group.

  • They possess a fixed interface for data exchange. This makes it easier for you to pass input and output parameters to and from the function module.

For example, you can assign default values to the input parameters. The interface also supports exception handling. This allows you to catch errors and pass them back to the calling program for handling.

  • They use their own memory area. The calling program and the function module cannot exchange data using a shared memory area - they must use the function module interface. This avoids unpleasant side effects such as accidentally overwriting data.

  • You call a function module by its name (which must be unique) in a CALL FUNCTION statement.

and also

Unlike subroutines, you do not define function modules in the source code of your program. Instead, you use the Structure link Function Builder.

Check this link too

Regarding difference between Subroutines and Function modules

___________________________________________________________________

. the exact difference between subroutines and function modules

a Function Modules are created thru SE37 Tcode.

b FMs have well defined interface for developing and calling them.

ie. IMPORT, EXPORT, CHANGING,TABLES, EXCEPTIONS

c FMs are avaialble globally.

ie any program can use them.

d FMs are called using the syntax : CALL FUNCTION

Whereas subroutines

a are created in a program thru se38.

b they are generally not used by other programs.

(however they can be used thru some special technique)

c subroutines are called using PERFORM

and defined using FORM keyword.

2. when is a sub routine used and also when is a function module used..???

1 If the requirement is very specific to the

program, a subroutine can be used, other wise FM.

2. If the code is to be executed MANY TIMES,

ie in a loop - 5000 - 10000 times,

it is better to use subroutine bcos

they are faster than FM.

I Hope it helps.

pls :award points if useful.

sri