# Prevent overriding an array

**URL:** <https://discourse.julialang.org/t/prevent-overriding-an-array/51875>\
**Category:** New to Julia\
**Created:** [December 15, 2020, 2:26pm UTC](https://discourse.julialang.org/t/prevent-overriding-an-array/51875 "2020-12-15T14:26:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sleepy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sleepy/32/20296_2.png) [@Sleepy](https://discourse.julialang.org/u/Sleepy)\
**Post date:** [December 15, 2020, 2:26pm UTC](https://discourse.julialang.org/t/prevent-overriding-an-array/51875/1 "2020-12-15T14:26:08Z")

</div>

Hi,

if I perform the following code

```julia
function next_step(x, i)
    x[i] = x[i]+1
    return x
end

x = [0,0,0,0]
x_a = next_step(x, 1)

```

the value of x is [1,0,0,0]. The value should be still [0,0,0,0].

Why do the value of x change? And how to avoid it?

---

<div class="post-metadata">

**Author:** ![Rudi79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rudi79/32/3884_2.png) [@Rudi79](https://discourse.julialang.org/u/Rudi79)\
**Post date:** [December 15, 2020, 2:34pm UTC](https://discourse.julialang.org/t/prevent-overriding-an-array/51875/2 "2020-12-15T14:34:08Z")

</div>

> [@Sleepy](#):
>
> ```julia
> function next_step(x, i)
> x[i] = x[i]+1
> return x
> end
> 
> x = [0,0,0,0]
> x_a = next_step(x, 1)
> 
> ```

Try

```julia
function next_step(x, i)
    y = copy(x)
    y[i] = y[i]+1
    return y
end

```

I would strongly suggest having a look into the manual.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 15, 2020, 2:38pm UTC](https://discourse.julialang.org/t/prevent-overriding-an-array/51875/3 "2020-12-15T14:38:33Z")

</div>

Julia using a passing model called “pass by sharing”. This is the same behavior you would see from python, Java, and most other programming languages.
