Function module vs Subroutine
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.
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>
Similar Messages
-
Custome BAPI - declare ITAB and define the Function Module and Subroutine
Hello Experts
I want to create a Custom BAPI and it has the following scenario:
1) a Function Module which collects some records into it internal table, say ITAB
2) a Subroutine which moved the records from ITAB to BAPI table
Now, I want to declare ITAB and define the Function Module and Subroutine.
Where and How can I do this?
Plz suggest.
Regards
BDHi,
1) Got to SE37 and create an RFC .
2) Declare the ITAB directly in the TABLES tab of the FM.
3) Inside the FM source Code tab, collect all the data using SELECT query and directly or by using logic, put the data into the
ITAB.
4) Since the data collected is directly put into the itab you dont need a subroutine to be written.
5) If subroutine is a necessity, then just write PERFORM SUB ROUTINE NAME.
AND DEFINE THE FORM ENDFORM OF THE SUBROUTINE AFTER THE ENDFUNCTION OF THE FM
Let me know if any issues....
Regards,
Vimal. -
Diff between Function Module and subroutine
sir,
explain the difference between Function Module and Subroutine.Hi Sandeep,
Subroutines:
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:
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.
Unlike subroutines, you do not define function modules in the source code of your program. Instead, you use the Function Builder.
Regards,
Sunil -
Macros, Function modules and subroutines
Hi Experts,
In performance wise which one is best macro or subroutine or Function module?
Thanks.Hi Nandini,
Each and every one has its importance in certain situations..
Macros
If you want to reuse the same set of statements more than once in a program, you can include them in a macro. For example, this can be useful for long calculations or complex WRITE statements. You can only use a macro within the program in which it is defined, and it can only be called in lines of the program following its definition.
Subroutines can call other subroutines (nested calls) and may also call themselves (recursive calls). Once a subroutine has finished running, the calling program carries on processing after the PERFORM statement. You can use the USING and CHANGING additions to supply values to the parameter interface of the subroutine.
Function modules allow you to encapsulate and reuse global functions in the SAP System. They are managed in a central function library. The SAP System contains several predefined functions modules that can be called from any ABAP program. Function modules also play an important role during updating and in interaction between different SAP systems, or between SAP systems and remote systems through remote communications.
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.
Best regards,
raam -
Difference between function module and subroutines
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..
vishalHi 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
Re: Regarding difference between Subroutines and Function modules
Hope this helps you,
Regards
Amit
Message was edited by: Amit Kumar -
How to organize function modules & subroutines in a function group?
Hi,
There are several function modules and subroutines in a function group. And the subroutines will be called by severel modules.
Originally:
1. I put subroutine forms in an Include.
2. and in the function modules which will use the forms, I include the Include before the function definition.
3. While when activating the functin modules, it's reported that the forms had already been defined. Only if one function module includes the Include, it's OK.
How should the forms and function modules be organized?
Thanks!
SayidHi Atish
Yes, you are right.
It works.
Thank you.
Regards,
Sayid -
How to Measure Function Module Performance?
Please can you tell me how I can measure the performance and trace the actions of a Function Module in R/3?
The function module in R/3 is run when a user calls a WebDynpro action from a WebDynpro screen within the SAP Portal.
I have tried running a trace on a user (ST05) but that only shows table actions (e.g. reads/fetch etc.). Also it does not appear in ST04 or ST03N. I would like to know how long the program actually takes to run.
Thanks.
PaulHi,
if I want to measure the runtime required to run some Abap, I use SE30. However i used it only for normal Dynpro application, not WebDynpro.
The detail level of the created trace can be configured. The aggregation level should be set to "Full" or "By call" at the beginning. Disabling aggregation leads to huge trace files.
You can select which statements should be traced. If you disable an option it's runtime is not lost but add to the traced action in the next level. If for example "Open SQL" is disabled, the time used by it is added into the net time of the method, function module of subroutine. Otherwise if "Open SQL" is enabled the net time of a function module does not include SQL time. SQL time is then listed separately.
Greetings -
"IS REQUESTED" IN FUNCTION MODULE
Hello frnds,
I have to copy a function module into subroutine, but in that function module there is a command "IS REQUESTED" which won't work in subroutine. can you help me by giving some solution by providing some command which we can use instead of "is requested"
Thanks in advance
Navneet
[email protected]hi,
IS REQUESTED check can be done on formal parameters of an FM whether this parameter was used when ur function module is called from a program.
I assume that the field on which IS REQUESTED was used is an optional parameter in your function module.
Let that statement be in the function module but use a flag.
if <field> is requested.
l_flag eq 'X'.
endif.
Now inside ur subroutine you can use flag.
if l_flag eq 'X'.
<the logic of the original code>.
endif. -
WHAT ARE THE UPDATED FUNCTION MODULES
Hi Friends,
Can anybody explain me about UPDATED FUNCTION MODULES (types of function modules)Hi
Update Function Module
Unlike transactions and executable programs, dialog modules do not start a new SAP LUW. Calls to update-task function modules from a dialog module use the same update key as the ones in the calling program. The result is that calls to update function modules from a dialog module are executed only if a COMMIT WORK statement occurs in the calling program.
If you place a COMMIT WORK in a dialog module, it does commit changes to the database (for example, with UPDATE).However, it does not start the update task. The function modules are not actually executed until a COMMIT WORK statement occurs in the calling program.
If you use dialog modules, try to avoid including calls to update function modules in subroutines called with PERFORM ON COMMIT. In general, any occurrence of PERFORM ON COMMIT(with or without update-task function calls) in a dialog module can be problematic.
This is because dialog modules have their own roll area, which disappears when the module finishes. Consequently, all local data (including data used for parameter values when calling an update function module) disappears as soon as the commit in the main program is reached.
If you must use this method in a dialog module (i.e. include the call to an update-task function in a subroutine), you must ensure that the values of the actual parameters still exist when the update-task function actually runs. To do this, you can store the required values with EXPORT TO MEMORY and then import them back into the main program (IMPORT FROM MEMORY) before the COMMIT WORK statement
Creating Update Function Modules
To create a function module, you first need to start the Function Builder. Choose Tools ® ABAP Workbench, Function Builder. For more information about creating function modules, refer to the ABAP Workbench Tools documentation.
To be able to call a function module in an update work process, you must flag it in the Function Builder. When you create the function module, set the Process Type attribute to one of the following values:
Update with immediate start
Set this option for high priority ("V1") functions that run in a shared (SAP LUW). These functions can be restarted by the update task in case of errors.
Update w. imm. start, no restart
Set this option for high priority ("V1") functions that run in a shared (SAP LUW). These functions may not be restarted by the update task.
Update with delayed start
Set this option for low priority ("V2") functions that run in their own update transactions. These functions can be restarted by the update task in case of errors.
To display the attributes screen in the Function Builder, choose Goto ® Administration.
Defining the Interface
Function modules that run in the update task have a limited interface:
Result parameters or exceptions are not allowed since update-task function modules cannot report on their results.
You must specify input parameters and tables with reference fields or reference structures defined in the ABAP Dictionary.
http://help.sap.com/saphelp_nw70/helpdata/en/41/7af4daa79e11d1950f0000e82de14a/frameset.htm
Regards,
Harish -
Brefely explain function module
hi
this is ramireddy.hi ram,
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.
3 types.
1)normal function module
2)remote function module
Difference between FM and subroutines
difference between function module and subroutines
function module vs loacal subroutines
3)update function module
reward points if helpful. -
What is the difference between subroutine and function module?
What is the difference between subroutine and function module?
Hi,
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.
Thanks,
Reward If Helpful. -
How to use a function module in a subroutine
Hi,
I am working on a report where i have to create list boxes for 5 fields.Instead of using the function module VRM_SET_VALUES for each field can i use subroutine?
If so, can anyone tell me the procedure for that?Its very urgent.
Thanks and Regards,
HemaHi,
I have written the code like this.
FORM VALUE USING V_NAME.
CALL FUNCTION 'VRM_SET_VALUES'
EXPORTING id = v_name
values = v_list.
ENDFORM.
AT SELECTION-SCREEN OUTPUT.
REFRESH V_LIST.
v_name = 'P_COMMER'.
v_value-key = 'C'.
v_value-text = 'Commercial'.
APPEND v_value TO v_list.
v_value-key = 'N'.
v_value-text = 'Non-Commercial'.
APPEND v_value TO v_list.
v_value-key = ' ' .
v_value-text = 'Both'.
APPEND v_value to v_list.
PERFORM VALUE USING V_NAME.
REFRESH V_LIST1.
v_name1 = 'P_DIRECT'.
v_value1-key = 'D'.
v_value1-text = 'Direct'.
APPEND v_value1 to v_list1.
v_value1-key = 'I'.
v_value1-text = 'Indirect'.
APPEND v_value1 to v_list1.
PERFORM VALUE USING V_NAME1.
REFRESH V_LIST2.
v_name2 = 'P_DIR'.
v_value2-key = 'D'.
v_value2-text = 'D'.
APPEND v_value2 TO v_list2.
v_value2-key = 'C'.
v_value2-text = 'C'.
APPEND v_value2 TO v_list2.
v_value2-key = ' ' .
v_value2-text = ' '.
APPEND v_value2 to v_list2.
PERFORM VALUE USING V_NAME2.
REFRESH V_LIST3.
v_name3 = 'P_LOW'.
v_value3-key = 'Y'.
v_value3-text = 'Yes'.
APPEND v_value3 TO v_list3.
v_value3-key = 'N'.
v_value3-text = 'No'.
APPEND v_value3 TO v_list3.
v_value3-key = ' ' .
v_value3-text = ''.
APPEND v_value3 to v_list3.
PERFORM VALUE USING V_NAME3.
REFRESH V_LIST4.
v_name4 = 'P_PARENT'.
v_value4-key = 'P'.
v_value4-text = 'Parent'.
APPEND v_value4 TO v_list4.
v_value4-key = 'C'.
v_value4-text = 'Child'.
APPEND v_value4 TO v_list4.
v_value4-key = ' ' .
v_value4-text = ''.
APPEND v_value4 to v_list4.
PERFORM VALUE USING V_NAME4.
REFRESH V_LIST5.
v_name5 = 'P_PREF'.
v_value5-key = 'P'.
v_value5-text = 'Preferred'.
APPEND v_value5 TO v_list5.
v_value5-key = 'N'.
v_value5-text = 'NotPreferred'.
APPEND v_value5 TO v_list5.
v_value5-key = ' ' .
v_value5-text = ''.
APPEND v_value5 to v_list5.
PERFORM VALUE USING V_NAME5.
Now I am getting the same set of values in all the fields.How can I solve this?
Thanks and Regards,
Hema -
Function Module IMPORT structures not recognized in form subroutines
Hello everyone,
Did some searching on SDN and didn't find the answer, so I apologize if this has been covered before.
Just finished my first Function Module. It's a remote-enabled (RFC) function that will be called by XI to do some processing. I have two components declared on the IMPORT tab, which we'll call "A" and "B". Both "A" and "B" are defined with a user type, each of which is a simple structure of 4-5 fields each. My Function Module also has two tables, and these are defined on the TABLES tab.
Here's my problem: When I do a syntax check, I get an error telling me that "A" and "B" are unknown. However, I only get this error where I reference "A" or "B" in the FORM subroutines I have declared at the end of my function.
I have my other data declarations defined globally in the "LZ...TOP" Include for my Function Module, so those don't pose a problem. But these are my Function Module parameters, so I can't go "declare" those as I already have them defined on the appropriate tabs.
Is there a way for me to get around this error and have them recognized globally, or do I have to resort to moving the values to some globally-declared "hold" field? I'm hesitant to have to add them as parameters passed into the FORM routines because I pretty much reference each of the fields within the structures for "A" and "B".
Thanks everyone. Points definitely awarded for helpful answers.Dave,
The parameters are visible only in the main program of the function module. So, if the subroutines are directly in the main function, they you should not have a issue. But if you subroutines in an include and you are calling them in the main function, then the paramters are not visible in the subroutines. In that case you will have to hold the values in a globally declared tables.
Or you will have to pass them as parameters to Sub routines as well.
Regards,
Ravi
note : please mark the helpful answers
Message was edited by: Ravikumar Allampallam -
Using subroutines in Function Module
Hi , experts ,
I want to know how to use performs in function module,
If created subroutine must be used in all FM in Func.Group ?
if you have articles about this or any information , I will be glad .
Please help.
Regards,
Tatiana.If i am not mistaken you want to use forms in function module.
To enable forms in function module you have to follow these steps:
Create a Z include in your function module.
INCLUDE ZTEST_INCLUDE.
Inside this include you need to write your form routine which will be accessable from your function module.
Now you can use perform statement in your funtion module.
Thnaks & Regards,
Lalit Mohan Gupta -
Can we call functional module in a subroutine? Any example?
Can we call functional module in a subroutine? Any example?
sample code....
REPORT ZPL_BDC_PA30.
TABLES : PA0022.
DATA : V_PERNR LIKE PA0022-PERNR,
V_SLABS LIKE PA0022-SLABS.
DATA : FILENAME TYPE STRING.
DATA : BEGIN OF IT_DATA OCCURS 0,
PERNR LIKE PA0022-PERNR, "Personnel Number
SLABS LIKE PA0022-SLABS, "Certificate code
SLABS1 LIKE PA0022-SLABS, "Certificate code new
END OF IT_DATA.
DATA : IT_BDCDATA LIKE BDCDATA OCCURS 1 WITH HEADER LINE.
DATA : BEGIN OF IT_ERROR OCCURS 0,
POS LIKE SY-TABIX,
TEXT(40),
END OF IT_ERROR.
PARAMETERS : P_FNAME TYPE DXFIELDS-LONGPATH.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FNAME.
CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
EXPORTING
I_LOCATION_FLAG = ' '
I_SERVER = '?'
I_PATH = I_PATH
FILEMASK = '.'
FILEOPERATION = 'R'
IMPORTING
O_LOCATION_FLAG = O_LOCATION_FLAG
O_SERVER = O_SERVER
O_PATH = P_FNAME
ABEND_FLAG = ABEND_FLAG
EXCEPTIONS
RFC_ERROR = 1
ERROR_WITH_GUI = 2
OTHERS = 3
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
START-OF-SELECTION.
FILENAME = P_FNAME.
PERFORM UPLOAD. "Uploading inputfile to internal table.
PERFORM VALIDATE. "validating the values
PERFORM POPULATE_BDC. "Populating bdc internal table
PERFORM ERROR_RECORDS
TABLES IT_ERROR. "Error records
*& Form bdc_fdata
FORM BDC_FDATA USING FNAM FVAL.
CLEAR IT_BDCDATA.
IT_BDCDATA-FNAM = FNAM.
IT_BDCDATA-FVAL = FVAL.
APPEND IT_BDCDATA.
ENDFORM. " bdc_fdata
*& Form bdc_hdata
FORM BDC_HDATA USING PROGRAM SCRNO DYNBEGIN.
CLEAR IT_BDCDATA.
IT_BDCDATA-PROGRAM = PROGRAM.
IT_BDCDATA-DYNPRO = SCRNO.
IT_BDCDATA-DYNBEGIN = DYNBEGIN.
APPEND IT_BDCDATA.
ENDFORM. " bdc_hdata
*& Form upload
FORM UPLOAD .
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = FILENAME
FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = 'X'
HEADER_LENGTH = 0
READ_BY_LINE = 'X'
DAT_MODE = ' '
CODEPAGE = ' '
IGNORE_CERR = ABAP_TRUE
REPLACEMENT = '#'
CHECK_BOM = ' '
VIRUS_SCAN_PROFILE = VIRUS_SCAN_PROFILE
NO_AUTH_CHECK = ' '
IMPORTING
FILELENGTH = FILELENGTH
HEADER = HEADER
TABLES
DATA_TAB = IT_DATA
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_READ_ERROR = 2
NO_BATCH = 3
GUI_REFUSE_FILETRANSFER = 4
INVALID_TYPE = 5
NO_AUTHORITY = 6
UNKNOWN_ERROR = 7
BAD_DATA_FORMAT = 8
HEADER_NOT_ALLOWED = 9
SEPARATOR_NOT_ALLOWED = 10
HEADER_TOO_LONG = 11
UNKNOWN_DP_ERROR = 12
ACCESS_DENIED = 13
DP_OUT_OF_MEMORY = 14
DISK_FULL = 15
DP_TIMEOUT = 16
OTHERS = 17
IF SY-SUBRC <> 0.
MESSAGE I000(BCTRAIN) WITH 'FILE NOT UPLOADED'.
ELSE.
MESSAGE I000(BCTRAIN) WITH 'FILE UPLOADED'.
ENDIF.
ENDFORM. " upload
*& Form populate_bdc
FORM POPULATE_BDC .
LOOP AT IT_DATA .
PERFORM BDC_HDATA USING 'SAPMP50A'
'1000'
'X'.
PERFORM BDC_FDATA USING 'RP50G-PERNR'
IT_DATA-PERNR.
PERFORM BDC_FDATA USING 'RP50G-CHOIC'
'0022'.
PERFORM BDC_FDATA USING 'BDC_OKCODE'
'=MOD'.
PERFORM BDC_HDATA USING 'MP002200'
'2000'
'X'.
PERFORM BDC_FDATA USING 'P0022-SLABS'
IT_DATA-SLABS1.
PERFORM BDC_FDATA USING 'BDC_OKCODE'
'=UPD'.
CALL TRANSACTION 'PA30' USING IT_BDCDATA.
REFRESH IT_BDCDATA.
ENDLOOP.
ENDFORM. " populate_bdc
*& Form validate
FORM VALIDATE .
data : num like sy-tabix,
num1 like sy-tabix.
LOOP AT IT_DATA.
num = num + 1.
num1 = sy-tabix.
SELECT PERNR
FROM PA0022
INTO V_PERNR WHERE
PERNR EQ IT_DATA-PERNR.
ENDSELECT.
IF SY-SUBRC <> 0.
MOVE num TO IT_ERROR-POS.
MOVE 'Invalid Pernr' TO IT_ERROR-TEXT.
APPEND IT_ERROR.
DELETE IT_DATA .
ELSE.
IF IT_DATA-SLABS IS INITIAL.
MOVE num TO IT_ERROR-POS.
MOVE 'Certificate code is initial' TO IT_ERROR-TEXT.
APPEND IT_ERROR.
DELETE IT_DATA.
ENDIF.
ENDIF.
ENDLOOP.
ENDFORM. " validate
*& Form error_records
FORM ERROR_RECORDS TABLES IT_ERRORS.
FORMAT COLOR COL_GROUP.
WRITE : / 'Error in the following records : ' .
FORMAT COLOR COL_NEGATIVE.
LOOP AT IT_ERROR.
WRITE : / IT_ERROR-POS,
IT_ERROR-TEXT.
ENDLOOP.
ENDFORM. " error_records
Maybe you are looking for
-
Family share and non itunes purchased music
My wife and I are going to get an ipad mini for the house that we will use for a lot of music storage and entertainment for the house. We have a TON of cd's we purchased back in the day. If I load those into my iTunes, will my wife be able to access
-
Changing iTunes account from Ireland to uk?
Hello all, I've recently moved from Ireland to the Uk. Unblocked my phone changed supplier etc and all is fine. However when trying to change my iTunes account from Ireland to uk it keeps defaulting the address to Ireland - regardless if I input my u
-
How do I duplicate layers & groups without automatically adding a "copy", "copy 1", "copy #" at the end of the new group? It's been pestering me for quite a while now, since I often work with the same elements over and over again. Its frustrating to
-
Headstart Business Rule IN parameter changes value
I've created a Business Rule (CEV rule � triggered by create) with following code: IN parameters: p_datum_van, p_cat and p_prest cursor c_cap is select datum_tot from cew_cat_prest where . . . and p_datum_van between datum_van and datum_tot and datum
-
hi gurus, how can i change material after creation of bom..if we cant change then how can we delete the bom.. thank & Regard Rahul