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

46 ビュー (過去 30 日間)
Mr M.
Mr M. 2018 年 11 月 12 日
コメント済み: Adam 2018 年 11 月 13 日
Is it possible, or is it used to, or is it elegant to change input variables/parameters of a function inside the function?
  8 件のコメント
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 日
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 日
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 日
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.

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

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by