2012 Aug 06 3:46 PM
Hello All,
While I was reading about the properties of interface parameters of a function module in the link - http://help.sap.com/abapdocu_702/en/abenfunction_parameters.htm , I came across the following statement regarding EXPORTING and CHANGING parameters -
"An EXPORTING parameter passed by reference behaves like a CHANGING parameter, which means that EXPORTING parameters passed by reference are not initialized when the function module is called. For this reason, no read access to these parameters should be permitted before the first write access. In addition, be careful when adding content to such parameters as, for example, when inserting rows into internal tables."
I wanted to test the above statement. I created a function module with 2 exporting parameters - export_param1 and export_param2. Both are integer type. And I did the following in the FM source code:
FUNCTION < fm_name>.
export_param2 = export_param1 + 10.
ENDFUNCTION.
In my opinion, I am "reading an export parameter, passed by value, before the first write access". In the expression: export_param2 = export_param1 + 10, the RHS is evaluated first and that involves a read access before a write access.
But why didn't I get any error during syntax check or while testing the FM? Is thsi property of EXPORTING param obsolete (I am working on release 702)?
Or have I interpreted the statement in a wrong way?
2012 Aug 06 7:28 PM
Hi Muthuswamy,
I understand the documentation as a 'guidance'. You should not do this, because it migh have 'side effects' - simply because you might expect from an exporting parameter that it does not 'carry over' the value it has in the calling program. But it does due to pass by reference.
Why is there no warning?... I have no good explanation. But I believe it has simply never been implemented...
My best practice would be: if you have exporting parameters called by reference, do a clear at the very beginning of the function
Best regards
Thorsten
Hello All,
While I was reading about the properties of interface parameters of a function module in the link - http://help.sap.com/abapdocu_702/en/abenfunction_parameters.htm , I came across the following statement regarding EXPORTING and CHANGING parameters -
"An EXPORTING parameter passed by reference behaves like a CHANGING parameter, which means that EXPORTING parameters passed by reference are not initialized when the function module is called. For this reason, no read access to these parameters should be permitted before the first write access. In addition, be careful when adding content to such parameters as, for example, when inserting rows into internal tables."
I wanted to test the above statement. I created a function module with 2 exporting parameters - export_param1 and export_param2. Both are integer type. And I did the following in the FM source code:
FUNCTION < fm_name>.
export_param2 = export_param1 + 10.
ENDFUNCTION.
In my opinion, I am "reading an export parameter, passed by value, before the first write access". In the expression: export_param2 = export_param1 + 10, the RHS is evaluated first and that involves a read access before a write access.
But why didn't I get any error during syntax check or while testing the FM? Is thsi property of EXPORTING param obsolete (I am working on release 702)?
Or have I interpreted the statement in a wrong way?
2012 Aug 06 7:28 PM
Hi Muthuswamy,
I understand the documentation as a 'guidance'. You should not do this, because it migh have 'side effects' - simply because you might expect from an exporting parameter that it does not 'carry over' the value it has in the calling program. But it does due to pass by reference.
Why is there no warning?... I have no good explanation. But I believe it has simply never been implemented...
My best practice would be: if you have exporting parameters called by reference, do a clear at the very beginning of the function
Best regards
Thorsten
2012 Aug 07 7:08 AM
My best practice would be: if you have exporting parameters called by reference, do a clear at the very beginning of the functionAdditionally if you want to read the EXPORTING param use it as CHANGING instead.
Imho it is the duty of FM/Method developer to make the usage of the params as clear as possible. If i define a param as EXPORTING i'm not supposed to access it's value. So the caller should use his discretion & not pass a pre-populated (read: non-initial) actual param to it.
There are quite a few best practices, but only if everybody followed them to the letter.
BR,
Suhas
2012 Aug 07 8:36 AM
Hi Thorsten,
Thanks for taking the time out to answer my question. I will make it a practice to clear the export parameters as soon as I begin the source code of the FM. That is a good piece of advice.
Thanks for your time and effort.