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

**URL:** https://discourse.julialang.org/t/my-function-seems-to-be-over-writing-my-variable-but-i-cant-see-where/34620
**Category:** New to Julia
**Created:** [February 14, 2020, 11:34am UTC](https://discourse.julialang.org/t/my-function-seems-to-be-over-writing-my-variable-but-i-cant-see-where/34620 "2020-02-14T11:34:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![samantp](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@samantp](https://discourse.julialang.org/u/samantp)
#### Post date: [February 14, 2020, 11:34am UTC](https://discourse.julialang.org/t/my-function-seems-to-be-over-writing-my-variable-but-i-cant-see-where/34620/1 "2020-02-14T11:34:21Z")

</div>

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

```julia
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.

```julia
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).

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 14, 2020, 11:43am UTC](https://discourse.julialang.org/t/my-function-seems-to-be-over-writing-my-variable-but-i-cant-see-where/34620/2 "2020-02-14T11:43:52Z")

</div>

> [@samantp](#):
>
> ```julia
> dvh=dvht #store in dummy variable so as not to overwrite the dvh
> 
> ```

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

Possibly you want

```julia
dvh = copy(dvht)

```

---

<div class="post-metadata">

### Author: ![samantp](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@samantp](https://discourse.julialang.org/u/samantp)
#### Post date: [February 14, 2020, 11:52am UTC](https://discourse.julialang.org/t/my-function-seems-to-be-over-writing-my-variable-but-i-cant-see-where/34620/3 "2020-02-14T11:52:43Z")

</div>

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

```julia
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?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 14, 2020, 12:03pm UTC](https://discourse.julialang.org/t/my-function-seems-to-be-over-writing-my-variable-but-i-cant-see-where/34620/4 "2020-02-14T12:03:29Z")

</div>

> [@samantp](#):
>
> Also what does === do?

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

The point is that after

```julia
a = b

```

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

> [@samantp](#):
>
> So why does the line I’ve typed in the function overwrite the variable dvh?

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](https://en.wikipedia.org/wiki/Copy-on-write) semantics for arrays. Julia doesn’t, arrays are values like everything else.
