# Passing sub-arrays by reference

**URL:** <https://discourse.julialang.org/t/passing-sub-arrays-by-reference/40191>\
**Category:** General Usage\
**Created:** [May 26, 2020, 1:53pm UTC](https://discourse.julialang.org/t/passing-sub-arrays-by-reference/40191 "2020-05-26T13:53:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [May 26, 2020, 1:53pm UTC](https://discourse.julialang.org/t/passing-sub-arrays-by-reference/40191/1 "2020-05-26T13:53:46Z")

</div>

I am puzzled by how to pass sub-arrays of two or higher dimensional arrays by reference. I have defined the following function

```julia
julia> function ff(a::Vector) a[1]=777; end

```

which sets the first element to **777**. However, when I pass a subarray to this function, this change is not reflected.

```julia
julia> a = rand(2,3); ff(a[1,:]); display(a);                                 
2×3 Array{Float64,2}:                                                               
0.42995 0.803084 0.927195                                                      
0.155134 0.335006 0.399494

```

How should I define a function so that changes made on sub-arrays are reflected ?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [May 26, 2020, 2:01pm UTC](https://discourse.julialang.org/t/passing-sub-arrays-by-reference/40191/2 "2020-05-26T14:01:07Z")

</div>

`a[1, :]` always creates a new copy. If you actually want to pass a `SubArray` which still references the original array to a function, you should use `view(a, 1, :)` instead.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 26, 2020, 2:05pm UTC](https://discourse.julialang.org/t/passing-sub-arrays-by-reference/40191/3 "2020-05-26T14:05:07Z")

</div>

In addition to what @simeonschaub said, you will need to widen the set of types that `ff` takes to something like: `ff(a::AbstractVector)`, since the `view` function does not return a plain `Vector`. There should be no performance penalty for changing the type signature of `ff` to `::AbstractVector`, and I would recommend always taking `::AbstractVector` instead of `::Vector` as function inputs unless you have some compelling reason not to.

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [May 26, 2020, 2:08pm UTC](https://discourse.julialang.org/t/passing-sub-arrays-by-reference/40191/4 "2020-05-26T14:08:42Z")

</div>

@rdeits @simeonschaub I see, thank you ! I will use the `view` command henceforth.
