Is it possible, or is it used to, or is it elegant to change input variables?

Is it possible, or is it used to, or is it elegant to change input variables/parameters of a function inside the function?

8 件のコメント

Stephen23
Stephen23 2018 年 11 月 12 日
編集済み: Stephen23 2018 年 11 月 12 日
General good practice in most programming languages is to avoid changing input arguments. This makes debugging easier and avoids basic bugs caused by data changing in ways that was not expected.
Of course every rule can be broken, but like most rules-of-thumb, by the time you understand the reasons for why that rule exists, then you are in a position to know when to break it... and probably won't.
John D'Errico
John D'Errico 2018 年 11 月 12 日
+1 to Stephen's comment. It is as to the point as mine, yet more tactfully stated. ;-)
Adam
Adam 2018 年 11 月 12 日
The question seems rather ambiguous to me, but that appears to just be me, given the certainty with which the answers/comments have been given so far.
What do you mean by 'change input variables' though?
Change their name once they are in the function? Change their value once they are in the function? Change their value and expect that it will also change it in the scope they were passed in from? Pass new inputs to some piece of code to replace a previous set of inputs? Some other definition...?
John D'Errico
John D'Errico 2018 年 11 月 12 日
Yes, it is arguably less than clear. I can only answer the question they asked however. Answering a question they did not ask is really hard.
Mr M.
Mr M. 2018 年 11 月 13 日
changing their value, just inside the function
Adam
Adam 2018 年 11 月 13 日
It's not obvious why you would really want to. An example of where you think it would be useful would help understand the context. If you are going to change it then why pass it in in the first place?
Bruno Luong
Bruno Luong 2018 年 11 月 13 日
Adam: "If you are going to change it then why pass it in in the first place? "
You don't consider the case where the modified values depend on the original input values?
Adam
Adam 2018 年 11 月 13 日
True, I guess I would just use a new variable if I were doing that, but it is a use case that could have some purpose.

サインインしてコメントする。

回答 (3 件)

madhan ravi
madhan ravi 2018 年 11 月 12 日

1 投票

Once an input is passed onto the function you can transform(by calculation) it according to your need , if that’s what you meant.

2 件のコメント

Bruno Luong
Bruno Luong 2018 年 11 月 12 日
I'm with madhan. You do whatever you like in your function. It's your stuff, changing the value inside the function doesn't affect anything outside.
So why impose the rule?
I find strict coding rules are always stupid at some extend, at some point, at some circumstance.
I don't like imposing strict restriction on developers, let them free as long as the code works and easily to maintain.
madhan ravi
madhan ravi 2018 年 11 月 13 日
Thank you Buno didn't notice your comment earlier

サインインしてコメントする。

John D'Errico
John D'Errico 2018 年 11 月 12 日

1 投票

Is it possible? Of course. Why not?
Is it elegant? You can do anything you please in this matter. Note that any changes you make inside a function to an argument of the function will not be reflected outside the function.
So is it elegant? Who cares about elegance? It may be risky in terms of using a coding style that may be difficult to debug at some time in the future. But that is your choice to make, and only yours.
Steven Lord
Steven Lord 2018 年 11 月 12 日

1 投票

Possible? Yes.
Recommended? I would advise against it unless the input argument has handle behavior. If the input has handle behavior (like the audioplayer object used in the "Handle Objects Modified in Functions" section on that page or Handle Graphics objects like a figure or plot handle) then it has been designed (or should have been designed) for this type of modification.

1 件のコメント

Steven Lord
Steven Lord 2018 年 11 月 13 日
Mr M. clarified:
changing their value, just inside the function
Sure, it is possible to change the contents of the variable inside the function workspace. Note, however, that doing so will NOT change the contents of the variable in its caller's workspace unless that variable is a handle variable.
function y = twoTimesXPlusOne(x)
x = 2*x;
y = x+1;
That's perfectly legal code. If I were to call this function:
z = 1:10;
w = twoTimesXPlusOne(z);
Then z would be 1:10 before calling twoTimesXPlusOne and it would remain 1:10 after calling twoTimesXPlusOne.
If possible I would avoid doing this if the input variable is large, however. MATLAB has copy-on-write behavior, as described on this documentation page.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Programming についてさらに検索

質問済み:

2018 年 11 月 12 日

コメント済み:

2018 年 11 月 13 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by