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

difference between function module and subroutines

Former Member
0 Likes
9,166

hey all...

can u pls explai me the exact difference between subroutines and function modules...

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

regards..

vishal

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
3,583

Hi Vishal,

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.

Hope this helps u,

Regards,

Nagarajan.

9 REPLIES 9
Read only

Former Member
0 Likes
3,583

Hi Vishal,

Try this link:

help.sap.com/saphelp_46c/helpdata/ en/fc/eb2d67358411d1829f0000e829fbfe/content.htm

Regards,

Sudhakar.

Read only

Former Member
3,583

Main difference is subroutinue is used internally in a program while FM is used externally in a program. It means subroutine is written within a program and can't be used outside the program. FM can be written outside the program and can be used within a program.

Subroutinue will be used by the program and can't be used by other programs where as FM can be used in any of the programs required.

Subroutinue is used when say it is only needed for ur program now & in future and no other program will use this. Example...Data modification within a program which can be written in a subroutinue.

FM is used when not only ur program but other programs also use it. Even FM can be run individually.

Pl. award appropriate points.

Read only

Former Member
0 Likes
3,583

Hi vishal.

Subroutines are loacl to a progarm.. It cant be extended to other program.

But Function Modules are similar to subroutines but are global in view and can be used in many program.

Subroutine have three types.

pass by value, pass by reference and pass by value and reference similar to pointers but not exactly the same.

Function modules are used in BDC , BAPI etc..

Also there are standard FMs which can be used.

regs

vijay

<b>Provide points if u r satisfied with the reply.</b>

Read only

Former Member
0 Likes
3,584

Hi Vishal,

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.

Hope this helps u,

Regards,

Nagarajan.

Read only

Former Member
0 Likes
3,583

Hi SUbroutines also can be Global....

You can call subroutines from another program....

Only Main difference is <b>Exceptions....</b>

and <b>passing parameters</b> in simpler way to FM.

you cann't catch exceptions...

vijay

Read only

Former Member
0 Likes
3,583

Hi Vishal,

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

Hope this helps you,

Regards

Amit

Message was edited by: Amit Kumar

Read only

Former Member
0 Likes
3,583

using subroutines Exceptions are not handled...

Read only

Former Member
0 Likes
3,583

Hi Vishal,

1. 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.

Regards,

Amit M.

Read only

abdul_hakim
Active Contributor
3,583

Hi,

Subroutine is used to hold a reusable section of codes in your program(locally).

Function Modules is modularization unit with unique name that can be called by any other program in the entire SAP R/3 System..

<b>When to use What?</b>

If u want to perform any operation more than once locally in your program then u can use subroutine.

If u want to perform certain opertion in more than one program then u can go for FM..

REgards,

Abdul