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 commentaires

Stephen23
Stephen23 le 12 Nov 2018
Modifié(e) : Stephen23 le 12 Nov 2018
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.
+1 to Stephen's comment. It is as to the point as mine, yet more tactfully stated. ;-)
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...?
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.
changing their value, just inside the function
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?
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?
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.

Connectez-vous pour commenter.

Réponses (3)

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 commentaires

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.
Thank you Buno didn't notice your comment earlier

Connectez-vous pour commenter.

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.
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 commentaire

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.

Connectez-vous pour commenter.

Question posée :

le 12 Nov 2018

Commenté :

le 13 Nov 2018

Community Treasure Hunt

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

Start Hunting!

Translated by