# My function seems to be over-writing my variable but I can't see where

I have an error where if I call a function once it runs without issue, but if I call it a second time on a different argument, it doesn’t run and instead returns an inexact error. This leads me to believe that the function itself might be over-writing the input variable/

Here is how I am calling the function

``````dvhhhh=C2D(dvh2)
dvhhhh2=C2D(dvh2)
``````

The first line runs without issue, the second line returns an Inexact error.

Below is the code of the function C2D, as far as I can tell this function does not change the input array, dvht, but this must be what is happening as I’m calling the same function on the same argument but it only works the first time.

``````function C2D(dvht) #Utility Function to convert dvh from cumulative todifferentia
dvh=dvht #store in dummy variable so as not to overwrite the dvh
nb = length(dvh[:, 2])

for i = 2:nb
#Start with column 1 (corresponding to dose bins we will need
#to make). Set the dose value to be the midpoint between the dose
#and the one after it, this is the Dose value corresponding to
#this bin.
dvh[i-1, 1] = dvh[i-1, 1] + (dvh[i, 1] + dvh[i-1, 1]) / 2
#
dvh[i-1, 2] = dvh[i-1, 2] - dvh[i, 2] # % volume in this bin will just
#be the difference in % this and the previous row's relative volume %
end

dvh[:, 1] = 0.01 * dvh[:, 1]#optional, This assumes the dose in is in cGy, we convert to Gy
dvh = dvh[1:end.!=nb, :]

dvh #return the differential dvh
end
``````

If it helps, dvh2 is iniitally set to `[0 100; 12000 0];`, but has its value changed to a 2×2 Array{Int64,2}: [60 100; 120 0]
After I run the C2D function on it. Yet I cannot see how the C2D function over-writes the input variable or why. I am assuming that the variable names in a function behave the same way as in MATLAB (i.e. they are entirely encoded within the function, not outside of it).

This does not do what you think it does — it makes `dvh === dvht`, so you are overwriting it.

Possibly you want

``````dvh = copy(dvht)
``````

Thanks, that did indeed solve the problem but now I’m quite confused about variable assignment in Julia.
If I do something like

``````a=6
b=a
b=10
``````

Then the value of b is 10 but the value of a is still 6. So why does the line I’ve typed in the function overwrite the variable dvh? Also what does === do? I thought that this was a boolean conditional operator returning either True or False? Why would this change the value of something?

Please kindly refer to the documentation and the help system, eg `?===`.

The point is that after

``````a = b
``````

`a` and `b` are identical, ie `a === b`.

It assigns the value to that variable. “Overwriting” is not the best way to think about it.

This is a guess, but I am wondering if you get your intuition from R, which has copy on write semantics for arrays. Julia doesn’t, arrays are values like everything else.