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 module vs Subroutine

Former Member
0 Likes
1,362

hi,

One of the main difference between Function module and the subroutine is that Function module can return values but subroutine cannot.I could not understand this point.

can anybody tell me how i can see the difference between fm and subroutine with an example?

Thanks in advance.

1 ACCEPTED SOLUTION
Read only

ssimsekler
Product and Topic Expert
Product and Topic Expert
0 Likes
841

Hi

As mentioned, both forms and FMs are <i>reusable modularization units</i>. To distinguish we generally say that forms are used for <b>internal modularization</b> and FMs are used for <b>external modularization</b>.

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 <i>function groups</i> and function groups may have global variables that can be globally used by all FMs inside it.

Regards

*--Serdar <a href="https://www.sdn.sap.com:443http://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.sdn.businesscard.sdnbusinesscard?u=qbk%2bsag%2bjiw%3d">[ BC ]</a>

3 REPLIES 3
Read only

Former Member
0 Likes
841

Well, 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.


REPORT ztest.

DATA: count TYPE i VALUE 0.

DO 5 TIMES.
  PERFORM add CHANGING count.
  WRITE: /001 'The new value is:', count.
ENDDO.

FORM add CHANGING p_count.
  ADD 1 TO p_count.
ENDFORM.            

Rob

Read only

Vinod_Chandran
Active Contributor
0 Likes
841

Both are for same purpose, modularisation and reuse.

If you want to use same set of code in the same program multiple times, then use subroutines.

If the same code is used in multiple programs, then create a function module.

Both can accept and return values. You can use USING, CHANGING and TABLE parameters in subroutine and IMPORT, EXPORT, TABLES and EXCEPTIONS in function module.

Vinod

Read only

ssimsekler
Product and Topic Expert
Product and Topic Expert
0 Likes
842

Hi

As mentioned, both forms and FMs are <i>reusable modularization units</i>. To distinguish we generally say that forms are used for <b>internal modularization</b> and FMs are used for <b>external modularization</b>.

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 <i>function groups</i> and function groups may have global variables that can be globally used by all FMs inside it.

Regards

*--Serdar <a href="https://www.sdn.sap.com:443http://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.sdn.businesscard.sdnbusinesscard?u=qbk%2bsag%2bjiw%3d">[ BC ]</a>