# Issues with In Place Transformations and Scope

**URL:** <https://discourse.julialang.org/t/issues-with-in-place-transformations-and-scope/16478>\
**Category:** General Usage\
**Created:** [October 18, 2018, 1:45am UTC](https://discourse.julialang.org/t/issues-with-in-place-transformations-and-scope/16478 "2018-10-18T01:45:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [October 18, 2018, 1:45am UTC](https://discourse.julialang.org/t/issues-with-in-place-transformations-and-scope/16478/1 "2018-10-18T01:45:53Z")

</div>

The following is a simplified and related version of the issue I raised [here](https://discourse.julialang.org/t/minimizing-work-in-copying-manipulating-custom-data-structures/16284):

```julia
function f!(x_work, x)
    n = length(x);
    for (j,k) in zip(1:n,n:-1:1)
        x_work[j] = sin(x[k]);
    end
    x_work, x
end

function iterate_f!(x, x_work, n)
    for j in 1:n
         x, x_work = f!(x_work, x);
    end
    x, x_work
end
m = 5;
x = collect(range(0,stop=1,length=m));
x_work = zeros(m);
iterate_f!(x,x_work,1);
println(x);
println(x_work);

x = collect(range(0,stop=1,length=m));
x_work = zeros(m);
iterate_f!(x,x_work,2);
println(x);
println(x_work);

```

I am surprised that this appears to “fail” when the iteration is run with `n=1`, but works when it is called with a larger value.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [October 18, 2018, 5:45am UTC](https://discourse.julialang.org/t/issues-with-in-place-transformations-and-scope/16478/2 "2018-10-18T05:45:22Z")

</div>

I think you will find this “works” with n even but fails with n odd. You need to assign the return values, then it should all be find, i.e.,

```julia
x, x_work = iterate_f!(x,x_work,1);

```

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [October 18, 2018, 1:22pm UTC](https://discourse.julialang.org/t/issues-with-in-place-transformations-and-scope/16478/3 "2018-10-18T13:22:56Z")

</div>

Ok, that “works,” but the behavior is counter to my expectations.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [October 18, 2018, 3:13pm UTC](https://discourse.julialang.org/t/issues-with-in-place-transformations-and-scope/16478/4 "2018-10-18T15:13:12Z")

</div>

Why?
