## Passing by reference vs value

Robin L.

### Robin L. (view profile)

さんによって質問されました 2019 年 3 月 19 日

### Stephen Cobeldick (view profile)

さんによって 編集されました 2019 年 10 月 6 日
Stephen Cobeldick

### Stephen Cobeldick (view profile)

さんの 回答が採用されました
Hello !
Hope you are all doing fine.
What is better ?
clear; clc;
% let's consider a and b, some matrix
tic;
multiply_tab(a, b);
toc;
tic;
multiply(max(max(c)),min(min(d)));
toc;
function result = multiply(max_a, min_b)
result = max_a * min_b;
end
function result = multiply_tab(a, b)
result = max(max(a)) * min(min(b));
end
In fact, I would like to know if Matlab passes array parameters as a reference of the array in the memory or a new copy of the array.
Because I try to get my code the most efficient, finding the cleanest way between human readability and high computing speed.
Thanks !
Robin

#### 0 件のコメント

サインイン to comment.

R2018a

## 1 件の回答 ### Stephen Cobeldick (view profile)

on 19 Mar 2019
Edited by Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 19 Mar 2019

"Passing by reference vs value"
Neither. MATLAB is more intelligent that either of those. MATLAB uses something called "copy on write", which essentially means that is passes by reference unitl the data is changed, at which point it makes a copy. This is explained in the blogs: "MATLAB makes copies of arrays it passes only when the data referred to changes"
What does this mean for your code? In 99.99% of cases you should just use the simplest syntax, i.e. simply pass the variables and let MATLAB's memory management take care of how the variables are stored. In your example just pass a and b, no copies will be made.
In any case, TMW discourages writing code to take advantage of specific JIT features. The philosophy is that the JIT engine is written to optimize your code, not the other way around.

Robin L.

### Robin L. (view profile)

on 19 Mar 2019
Thank a lot Stephen Cobeldick for your explanations !
I will use the simplest syntax : passing the variables as parameters and let Matlab manage how the variables are stored.
Han Yoo

### Han Yoo (view profile)

on 5 Oct 2019
How would you recursively call a function?
I need to make my own version of bwconncomp() using the flood fill algorithm.
[ioaImgConnComp] = setImgFloodFill(ioaImgConnComp, ivCoordX_pix, ...)
Without pass by reference, MATLAB keeps making a copy of the input image array (e.g., ioaImgConnComp), which is named the same as the output argument.
Pass by reference or point enables creating a recursive function easy and intelligent. What equivalent techniques are available on MATLAB?
Thanks.
Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 6 Oct 2019
@Han Yoo: recursive functions appear to create copies of their inputs (this might change depending on MATLAB version, etc). The simple solution to prevent this occuring is to write the recursive function as a nested function and define those variable/s in the "parent" workspace. Then all calls of the recursive function will access exactly the same variable/s in memory.

サインイン to comment.