Clear Filters
Clear Filters

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

44 views (last 30 days)
Is it possible, or is it used to, or is it elegant to change input variables/parameters of a function inside the function?
Bruno Luong
Bruno Luong on 13 Nov 2018
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 on 13 Nov 2018
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.

Sign in to comment.

Answers (3)

madhan ravi
madhan ravi on 12 Nov 2018
Once an input is passed onto the function you can transform(by calculation) it according to your need , if that’s what you meant.
Bruno Luong
Bruno Luong on 12 Nov 2018
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.

Sign in to comment.

John D'Errico
John D'Errico on 12 Nov 2018
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 on 12 Nov 2018
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 Comment
Steven Lord
Steven Lord on 13 Nov 2018
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.

Sign in to comment.


Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!