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: 

"Changing" parameter and "Tables" parameter defintions in function module

jitendra_mehta
Participant
0 Kudos

If one defines "Changing" or "tables" parameters, they are treated as reference parameters.

It becomes obvious for "tables" parameters where one does not have option to check box for "Pass by Value". But for "Changing" parameters, they provide the checkbox for "Pass by Value". Even when you select this option, the function module seems to handle the parameter as a reference parameter (whatever changes you make to it in the function module are retained when you return back to a calling program).

My question is why then SAP has provided the checkbox of "Pass by Value" for "Changing" parameter?

Thanks.

Jitendra Mehta

1 ACCEPTED SOLUTION

Former Member
0 Kudos

If you want a return parameter as a reference you use changing

If you want a return parameter as a value copy you use exports

A changing parameter that is passed as value acts the same way as a export parameter, just confusing things more

10 REPLIES 10

Clemenss
Active Contributor
0 Kudos

Hi Jitendra,

that's what I also wondered until a wise guy told me: If the function call is terminated by raising an exception, the changes are not applied because the vakues are not copied back.

Regards,

Clemens

0 Kudos

Hi Clemens:

I didn't understand how the wise guy answered the question. I am not worried about function module raising exception. If it works normally, then why the parameter gets changed even though I define it as "pass by value"?

Jitendra Mehta

Clemenss
Active Contributor
0 Kudos

I don't understand Chaiphon fully, because a perform in program is nothing diferent from any other call. The thing is: Call by value means that the parameter is copied to a local variable of the same type and structure. Normally you do this with input parameters you want to change inside the function. CHANGING by value means that it is copied to a loacal variable, then it may be changed and then the changed value is copied back to the parameter.

That led me to the question if it ever would make any sense to use changing by value. But then: If the contents have been changed and then an exception occurs, the calling program can process the exception and the values are not changed.

Regards,

Clemens

chaiphon
Contributor
0 Kudos

I think you need to understand the concept of 'Pass by Value' and 'Pass by reference'.

pass by value - the value will be copy to new variable and work on that until end of function and copy it back to the program.

pass by reference - the value will be referenced to the original variable in the program.

If you use changing parameter with pass by value in function module, and in function module you call

 perform call_in_program(program_name). 

the value of variable that pass to changing parameter in the program is still the same.

If you use changing parameter without pass by value, the value of variable that pass to changing parameter in the program will be the new value as in function module.

Hope it help,

Chaiphon

0 Kudos

Hi Chaiphon:

Thanks for explanation but I know the theory of "Pass by reference" versus "Pass by Value".

Let me rephrase my question.

When I use the importing parameter (say as a table with a dictionary table type) with reference (not "passing by value"), and if I modify the value of the parameter, the calling program (which calls function module) receives the changed value after the call.

When I use the importing parameter with "Pass by Value" box checked, the modifications made to the table inside the function module is not retained after the code leaves function module and returns to the calling program.

My question is why not the same behaviour with the "Changing parameter" in function module definition?

And if Changing parameters are always reference parameters, then why SAP has provided the "Pass by Value" check box?

0 Kudos

Hi,

Whether or not you select the check-box 'Pass by Value' the value of the parameter will be changed in the FM. Thats the purpose.

However, if you select the check-box in that case system created different memory areas for the parameters. So it behaves as a local variable and it copies the values when the call is terminated successfully. But if there is failure or an exception the local copies are not sent back to actual variables. You may create a test program and check the difference.

Regards

Prasenjit

0 Kudos

Hi Prasanjit:

Thanks for your reply. I did try with a test FM and a program and it indeed works as you mentioned.

So, If I understand you correctly. the "Pass by Value" parameter only helps me to refer to the original copy of parameter in case of any exception raised in the FM otherwise I will loose the original value if the FM executes normally (without any exception).

Is that true?

Jitendra Mehta

0 Kudos

Hi,

You are right. In case of an exception you get to keep the old values of the changing parameter otherwise they are overwritten.

Regards

Prasenjit

Former Member
0 Kudos

If you want a return parameter as a reference you use changing

If you want a return parameter as a value copy you use exports

A changing parameter that is passed as value acts the same way as a export parameter, just confusing things more

chaiphon
Contributor
0 Kudos

Hi Clemen,

osrry for not state it clearly. when I mention program_name, I mean calling program name. In function module we can call back to the original program for any reason.

One scenario that pass by value will become a must is when we create RFC function modules. There will be no communication between RFC and a calling program until RFC is done.

Chaiphon