# Why does this function change the input?

**URL:** <https://discourse.julialang.org/t/why-does-this-function-change-the-input/85433>\
**Category:** New to Julia\
**Tags:** function\
**Created:** [August 7, 2022, 8:59am UTC](https://discourse.julialang.org/t/why-does-this-function-change-the-input/85433 "2022-08-07T08:59:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mistamista](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@mistamista](https://discourse.julialang.org/u/mistamista)\
**Post date:** [August 7, 2022, 8:59am UTC](https://discourse.julialang.org/t/why-does-this-function-change-the-input/85433/1 "2022-08-07T08:59:29Z")

</div>

> State=[[1,2,3],,];
> 
> function move(State,m,n)  
> if checkLegal(State,m,n)\>0  
> State[n]=[State[m][1];;State[n]]  
> State[m]=State[m][2:end]  
> end  
> return State  
> end

I’m trying to get a new state  
newState=move(State,1,2)

This changes the State variable for some reason even though the variable being modified is implied in function-scope. Could anyone please tell me why this is the case and how to write a function that doesn’t change the global variable?

---

<div class="post-metadata">

**Author:** ![mistamista](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@mistamista](https://discourse.julialang.org/u/mistamista)\
**Post date:** [August 7, 2022, 9:00am UTC](https://discourse.julialang.org/t/why-does-this-function-change-the-input/85433/2 "2022-08-07T09:00:08Z")

</div>

Was missing the definition of checkLegal. Here it is

function checkLegal(State,m,n)  
if State[m]==  
return 0  
elseif State[n]==  
return 1  
elseif State[m][1]\>State[n][1]  
return 0  
else  
return 1  
end  
end

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [August 7, 2022, 10:49am UTC](https://discourse.julialang.org/t/why-does-this-function-change-the-input/85433/3 "2022-08-07T10:49:03Z")

</div>

You’re explicitly mutating the array you pass in, so it’s not surprising that it’s mutated. Why did you expect that mutating your input would leave it unchanged? Do you come from a different language where arrays passed to functions behave that way? Maybe matlab?

---

<div class="post-metadata">

**Author:** ![mistamista](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@mistamista](https://discourse.julialang.org/u/mistamista)\
**Post date:** [August 7, 2022, 11:00am UTC](https://discourse.julialang.org/t/why-does-this-function-change-the-input/85433/4 "2022-08-07T11:00:16Z")

</div>

I was under the impression that unless followed by a ! functions don’t mutate the arguments that are passed to them. Do Julia functions normally do this?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [August 7, 2022, 11:10am UTC](https://discourse.julialang.org/t/why-does-this-function-change-the-input/85433/5 "2022-08-07T11:10:23Z")

</div>

The exclamation mark is only a convention, it does not have any semantic consequences whatsoever.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 7, 2022, 11:49am UTC](https://discourse.julialang.org/t/why-does-this-function-change-the-input/85433/6 "2022-08-07T11:49:18Z")

</div>

> [@baggepinnen](#):
>
> The explanation mark

I have often missed an explanation mark in the language. Good to know it’s coming along😁

---

<div class="post-metadata">

**Author:** ![tsela](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tsela/32/52174_2.png) [@tsela](https://discourse.julialang.org/u/tsela)\
**Post date:** [August 7, 2022, 12:11pm UTC](https://discourse.julialang.org/t/why-does-this-function-change-the-input/85433/7 "2022-08-07T12:11:49Z")

</div>

As written, the exclamation mark is convention only. In other words, Julia functions can do whatever they want, including mutating their inputs, and it’s a convention that a programmer that makes a mutating function for which a non mutating version can exist to end its name with an explanation mark. In other words, it’s the responsibility of the programmer to add the exclamation mark to the mutating function they create. And to create non mutating functions if they don’t want them to change their inputs.

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [August 7, 2022, 12:12pm UTC](https://discourse.julialang.org/t/why-does-this-function-change-the-input/85433/8 "2022-08-07T12:12:18Z")

</div>

> [@mistamista](#):
>
> Could anyone please tell me why this is the case and how to write a function that doesn’t change the global variable?

To return a modified array without mutating the input argument, you can make a copy:

```julia
checkLegal(state,m,n) = !isempty(state[m]) && (isempty(state[n]) || state[m][1] <= state[n][1])

function move(state,m,n)
    result = copy(state)
    if checkLegal(state,m,n)
        result[n] = [state[m][1]; state[n]]
        result[m] = state[m][2:end]
    end
    return result
end

```

Note that `copy` doesn’t create copies of the inner arrays, it makes a copy of `state` which points to the same arrays. But since you are not mutating the inner arrays, only reassigning to them, it shouldn’t matter.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [August 7, 2022, 12:19pm UTC](https://discourse.julialang.org/t/why-does-this-function-change-the-input/85433/9 "2022-08-07T12:19:29Z")

</div>

> [@mistamista](#):
>
> Do Julia functions normally do this?

Yes.

> Argument Passing Behavior  
> Julia function arguments follow a convention sometimes called “pass-by-sharing”, which means that values are not copied when they are passed to functions. Function arguments themselves act as new variable _bindings_ (new locations that can refer to values), but the values they refer to are identical to the passed values. Modifications to mutable values (such as `Array`s) made within a function will be visible to the caller. This is the same behavior found in Scheme, most Lisps, Python, Ruby and Perl, among other dynamic languages.  
> [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#Argument-Passing-Behavior)
