Prev: 3d matrix array plot
Next: Mex and gsl
From: James Tursa on 6 Jul 2010 03:27 "Sadik " <sadik.hava(a)gmail.com> wrote in message <i0tjbr$nr5$1(a)fred.mathworks.com>... > > This worked because matlab doesn't pass variables as reference. Put differently, when you pass a variable to a matlab function, it is simply copied for the scope of module_B and nothing happens to the original variables in the scope of module_A. Not quite. When MATLAB passes arguments from module_A to an m-file function module_B, it passes a shared data copy of the argument (new header information but points to same data) to the module_B function. If the module_B function treats the argument as read-only then nothing else is done to the variable. But if the module_B function changes the variable, then a copy of the data is made (maybe) at that time for the module_B variable and then the changes are applied to this new data area (and nothing happens to the original variable in the scope of module_A). The reason I say maybe is that under certain circumstances (later version of MATLAB, calling the module_B function from within another function, and passing out the input variable as the same output variable) then a copy of the data is not made. For most practical purposes, MATLAB can be regarded as pass by value for m-files and intrinsic functions but internally it doesn't really work that way. When MATLAB passes arguments to a mex function, it passes the reference to the original variable ... there is no copy (shared or deep) made. So if module_B function is a mex function and it changes the argument, it will change the original variable in the scope of module_A. James Tursa |