Is it possible, or is it used to, or is it elegant to change input variables?
52 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
Bruno Luong
le 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
le 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.
Réponses (3)
madhan ravi
le 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.
2 commentaires
Bruno Luong
le 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.
John D'Errico
le 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.
0 commentaires
Steven Lord
le 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 commentaire
Steven Lord
le 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.
Voir également
Catégories
En savoir plus sur Variables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!