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: 

Difference between subroutine and function module

shwetha_hs
Employee
Employee
0 Kudos

Could anyone please help me giving me the differences between subroutine and function module

11 REPLIES 11

Former Member
0 Kudos

Hi,

Here is the notes of differences bet. FM and Subroutines.

Hi Function modules are for modularization urpose. You can the Function Module in any program. That is available all over in the SAP system.

Where as subroutine is program specific. Once you create a subroutine in one program then you can use that in that program and related programs like includes in the program or you have to call the subroutine specifically using the main program name.

Finally subroutines are for program modularization. Not for generic use.

FM's are for generic use. Not program dependent.

FMs are mainly used when a routine is to be performed by many programs.

Subroutines (forms) are generally only executed within one program.

You can perform routines from other programs, but it's not often done.

both forms and FMs are reusable modularization units.

To distinguish we generally say that forms are used for internal modularization and

FMs are used for external modularization.

To decide on which to implement, consider whether you need the content to be used just for a limited program

or wheteher it can be called from many independent programs.

For the first purpose it is better to implement a form whereas for the second we implement an FM.

However, ABAP does not isolate the usage context.

That is; you can call a form from another program within whose code the form is not actually implemented.

However, this requires attention since the form may utilize global variables.

The same issue holds for FMs.

FMs are encapsulated in function groups and function groups may have global variables that can be globally

used by all FMs inside it.

Regards,

Former Member
0 Kudos

Hi,

To be very clear:

The main difference is ,

-> U can handle Exceptions in FM wheras in Subroutines its not possible.

-> FM's are stored in Global class and have Global Presence, Subroutines may or may not have global presence( as in case of external subroutines).

-> FM's can be executed dircetly and tested whereas Subroutines cannot be executed Independently even in external subroutines.

Rvert back if any issues,

regards,

Naveen

Message was edited by:

Naveen Deva

Former Member
0 Kudos

Hi,

The sub-routines r locally declared in the program where as function module r declared globally at se37and u can call in the programmes

<a href="http://http://help.sap.com/saphelp_47x200/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm">For further reference</a>

reward points if it helps

Former Member
0 Kudos

Hi

There is a difference between a Subroutine and Function Module.

1. Subroutines have no Exceptions Parameters.

FM have Exceptions parameters to catch the exceptions.

2. Subroutines can be accessed locally i.e. local Modularization Unit.

FM have Global Access i.e. they are Global Modularization Unit.

3. Subroutines can not be tested independently.

FM can be tested independently.

4. Subroutines Explicitly wont return any values.

FM explicitly returns values.

5. Subroutines is not remote Enables.

FM can be made Remote Enables and can be called from non-SAP systems.

6. Optional parameters cannot be in Subroutines.

FM can be defined with optional Parameters.

they can both return values.

FMs are mainly used when a routine is to be performed by many programs.

Subroutines (forms) are generally only executed within one program.

You can perform routines from other programs, but it's not often done.

both forms and FMs are reusable modularization units.

To distinguish we generally say that forms are used for internal modularization and

FMs are used for external modularization.

To decide on which to implement, consider whether you need the content to be used just for a limited program

or wheteher it can be called from many independent programs.

For the first purpose it is better to implement a form whereas for the second we implement an FM.

However, ABAP does not isolate the usage context.

That is; you can call a form from another program within whose code the form is not actually implemented.

However, this requires attention since the form may utilize global variables.

The same issue holds for FMs.

FMs are encapsulated in function groups and function groups may have global variables that can be globally

used by all FMs inside it.

<b>Reward if usefull</b>

Former Member
0 Kudos

Hi,

Function Module:These are for Global modularizaiton. Name of a FM is unique within SAP.

Can be independently tested

Can be Remote enabled.

Can have exceptions

Subroutine:

These are for Local modularizaiton. Name of a subroutine is unique within a PROGRAM.

Cannot be independently tested

Cannot be remote enabled

Cannot have exceptions.

in detail.

Function modules are for modularization urpose. You can use the Function Module in any program. That is available all over in the SAP system.

Where as subroutine is program specific. Once you create a subroutine in one program then you can use that in that program and related programs like includes in the program or you have to call the subroutine specifically using the main program name.

Finally subroutines are for program modularization. Not for generic use.

FM's are for generic use. Not program dependent.

FMs are mainly used when a routine is to be performed by many programs.

Subroutines (forms) are generally only executed within one program.

You can perform routines from other programs, but it's not often done.

both forms and FMs are reusable modularization units.

To distinguish we generally say that forms are used for internal modularization and

FMs are used for external modularization.

To decide on which to implement, consider whether you need the content to be used just for a limited program

or wheteher it can be called from many independent programs.

Reward if helpful.

Regards,

Harini.S

Former Member
0 Kudos

hi shwetha,

You can use the PERFORM command to call an ABAP subroutine (form) from any program, subject to the normal ABAP runtime authorization checking. You can use such calls to subroutines for carrying out calculations, for obtaining data from the database that is needed at display or print time, for formatting data, and so on.

PERFORM commands, like all control commands, are executed when a document is formatted for display or printing. Communication between a subroutine that you call and the document is by way of symbols whose values are set in the subroutine.

Syntax in a form window:

/: PERFORM <form> IN PROGRAM <prog>

/: USING &INVAR1&

/: USING &INVAR2&

......

/: CHANGING &OUTVAR1&

/: CHANGING &OUTVAR2&

......

/: ENDPERFORM

INVAR1 and INVAR2 are variable symbols and may be of any of the four SAPscript symbol types.

OUTVAR1 and OUTVAR2 are local text symbols and must therefore be character strings.

The ABAP subroutine called via the command line stated above must be defined in the ABAP report prog as follows:

FORM <form> TABLES IN_TAB STRUCTURE ITCSY

OUT_TAB STRUCTURE ITCSY.

...

ENDFORM.

<b>Function modules</b> 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.

<b>Function modules</b> 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 <b>subroutines</b>, 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.

<b>Hope this is helpful, Do reward points.</b>

Former Member
0 Kudos

Hi Shwetha,

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

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

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

3) Different syntax is used to <b>call a function module</b> than to call a subroutine.

4) Leaving a function module is accomplished via the <b>raise</b> statement instead of check, exit, or stop.

Regards,

Vijay

Former Member
0 Kudos

Hi

Subroutines are used inside specific programs especially for achieving modularity whereas Function modules can be used across programs and have global visibility as they are grouped under function groups.

Thanks

Vasudha

Former Member
0 Kudos

<b>Function Module:</b>

1. It is pre compiled, when we create the function module at that time it self it will compiled.

2. Function module can create by using the transaction SE37.

3. Function module can call by the key work "CALL FUNCTION"

<b>SUBRUTINE:</b>

1.It's not pre compiled, when the program executing at that time itself subroutine will compile.

2.Subroutine can create with in the program.

3. Subrountine can call by keyword "FORM"

IF USEFULL REWARD

0 Kudos

Hi,

subroutine

1>subroutine can be used only within that program.

2>subroutine can not return any value.

3>In subroutines exception need to handle explicitly.

function modules

1>function module stores centrally in the sap library, so you can use globally in any program.

2>function module returns values.

3>Atomatic error handling is there.

Regards,

Manjunath M

Are you aware you answer a 12 years old question ?