# Trouble with function calls

**URL:** <https://discourse.julialang.org/t/trouble-with-function-calls/110940>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [February 29, 2024, 10:40am UTC](https://discourse.julialang.org/t/trouble-with-function-calls/110940 "2024-02-29T10:40:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andreasvarga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasvarga/32/11634_2.png) [@andreasvarga](https://discourse.julialang.org/u/andreasvarga)\
**Post date:** [February 29, 2024, 10:40am UTC](https://discourse.julialang.org/t/trouble-with-function-calls/110940/1 "2024-02-29T10:40:32Z")

</div>

The following commands produce a side effect which I can not explain:

```julia
julia> using LinearAlgebra

julia> A = 2*Matrix(I(2))
2×2 Matrix{Int64}:
 2 0
 0 2

julia> B = 3*Matrix(I(2))
2×2 Matrix{Int64}:
 3 0
 0 3

julia> Af = t-> A
#15 (generic function with 1 method)

julia> Af(0)
2×2 Matrix{Int64}:
 2 0
 0 2

julia> Bf = t->B
#17 (generic function with 1 method)

julia> Bf(0)
2×2 Matrix{Int64}:
 3 0
 0 3

julia> C = copyto!(Af(0),Bf(0))
2×2 Matrix{Int64}:
 3 0
 0 3

julia> A
2×2 Matrix{Int64}:
 3 0
 0 3

```

The result C is correct, but why is the original matrix A changed?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [February 29, 2024, 11:57am UTC](https://discourse.julialang.org/t/trouble-with-function-calls/110940/2 "2024-02-29T11:57:24Z")

</div>

That’s what `copyto!` does, it writes the elements of one array to another and returns the latter. `Af = t -> A` is a function that does nothing with its input and accesses the global variable `A`, then returns the array assigned to `A`. `Bf` does the same with global `B`. As a result, `C` is assigned to the same array as `A` is, you can verify with `C === A`.

---

<div class="post-metadata">

**Author:** ![andreasvarga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasvarga/32/11634_2.png) [@andreasvarga](https://discourse.julialang.org/u/andreasvarga)\
**Post date:** [February 29, 2024, 12:12pm UTC](https://discourse.julialang.org/t/trouble-with-function-calls/110940/3 "2024-02-29T12:12:19Z")

</div>

I encountered this issue in a function, where I believe A and B were not global. I even not used copyto! but another function which overwrites the result. I thought that the value of A is embedded in the function Af and therefore hidden and protected against modifications. I am using a simple fix for the issue

```julia
C = copyto!(copy(Af(0)),Bf(0))

```

which, I admit, is silly but it works.

Still I wonder how the copying involving two numerical matrices Af(0) and Bf(0) can change the value of A which served for the definition of the function Af(t).

This would also mean that the matrix A in the definition of Af(t) can be accessed somehow. Very eager to learn how.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [February 29, 2024, 1:18pm UTC](https://discourse.julialang.org/t/trouble-with-function-calls/110940/4 "2024-02-29T13:18:17Z")

</div>

> [@andreasvarga](#):
>
> I encountered this issue in a function, where I believe A and B were not global

They are, you can verify it:

```julia
julia> @code_lowered Af(0)
CodeInfo(
1 ─ return Main.A
)

```

> [@andreasvarga](#):
>
> Still I wonder how the copying involving two numerical matrices Af(0) and Bf(0) can change the value of A which served for the definition of the function Af(t).

`Af(0)` and `Bf(0)` didn’t do any copying, nor did the `C` assignment. In the entire example you only had the 2 arrays you started with.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [February 29, 2024, 8:43pm UTC](https://discourse.julialang.org/t/trouble-with-function-calls/110940/5 "2024-02-29T20:43:23Z")

</div>

> [@andreasvarga](#):
>
> I encountered this issue in a function, where I believe A and B were not global

Note that you may want to pay close attention to the scoping rules. The variable does not need to be global to be shared.

```julia-repl
julia> function f(A)
           ()->A
       end
f (generic function with 1 method)

julia> g = f([1 0; 0 1])
#1 (generic function with 1 method)

julia> B = g()
2×2 Matrix{Int64}:
 1 0
 0 1

julia> C = g()
2×2 Matrix{Int64}:
 1 0
 0 1

julia> B .= 2
2×2 Matrix{Int64}:
 2 2
 2 2

julia> C
2×2 Matrix{Int64}:
 2 2
 2 2

```

---

<div class="post-metadata">

**Author:** ![andreasvarga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasvarga/32/11634_2.png) [@andreasvarga](https://discourse.julialang.org/u/andreasvarga)\
**Post date:** [March 1, 2024, 11:34am UTC](https://discourse.julialang.org/t/trouble-with-function-calls/110940/6 "2024-03-01T11:34:57Z")

</div>

Thanks for these additional clarifications. I must confess I completely ignored these facts in implementing a number of functions for manipulating periodic matrices in the [PeriodicSystems](https://github.com/andreasvarga/PeriodicSystems.jl) package, which are defined via function mappings.
