# Append an array inside a function

**URL:** https://discourse.julialang.org/t/append-an-array-inside-a-function/49289
**Category:** General Usage
**Created:** [October 30, 2020, 7:19am UTC](https://discourse.julialang.org/t/append-an-array-inside-a-function/49289 "2020-10-30T07:19:59Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)
#### Post date: [October 30, 2020, 7:19am UTC](https://discourse.julialang.org/t/append-an-array-inside-a-function/49289/1 "2020-10-30T07:19:59Z")

</div>

I don’t really understand the scope of the variable SV here. I used it inside a function, so I expect that when I add elements to it, it should work fine:

```julia
SV = rand(10, 1)

function add_star(SV)

    for i =1:5

        append!(SV_new[:,1], 4)

    end

    return SV[:,1]
end

```

I am expecting the output of the function to be an array with 15 elements where the last 5 elements are 4, but the output is still an array with 10 elements, why?

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [October 30, 2020, 7:26am UTC](https://discourse.julialang.org/t/append-an-array-inside-a-function/49289/2 "2020-10-30T07:26:06Z")

</div>

`SV[:,1]` returns a new array. Instead you need to do `@view(SV[:,1])`, to get a view.

---

<div class="post-metadata">

### Author: ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)
#### Post date: [October 30, 2020, 7:33am UTC](https://discourse.julialang.org/t/append-an-array-inside-a-function/49289/3 "2020-10-30T07:33:28Z")

</div>

Sorry that was a mistake. The correct code is:

```julia
function add_star(SV)

    for i =1:5

        append!(SV[:,1], 4)

    end

    return SV[:,1]
end
```

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [October 30, 2020, 8:00am UTC](https://discourse.julialang.org/t/append-an-array-inside-a-function/49289/4 "2020-10-30T08:00:26Z")

</div>

Chances are, especially if you have a Matlab background, that you rather want a vector than a one-column matrix, in which case this is simply

```julia
SV = rand(10)
function add_star(SV)
    for i = 1:5
        append!(SV, 4)
    end
    return SV
end

```

If you really want a matrix you have to go through some hoops to achieve the same thing.

---

<div class="post-metadata">

### Author: ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)
#### Post date: [October 30, 2020, 8:16am UTC](https://discourse.julialang.org/t/append-an-array-inside-a-function/49289/5 "2020-10-30T08:16:44Z")

</div>

But this doesn’t work. The output of the function is still an array with 10 elements.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [October 30, 2020, 8:39am UTC](https://discourse.julialang.org/t/append-an-array-inside-a-function/49289/6 "2020-10-30T08:39:40Z")

</div>

No it has appened 5 4s:

```julia
julia> SV = rand(10);

julia> function add_star(SV)
           for i = 1:5
               append!(SV, 4)
           end
           return SV
       end
add_star (generic function with 1 method)

julia> add_star(SV);

julia> SV
15-element Array{Float64,1}:
 0.7833650176317701
 0.5668846033320463
 0.4391438535780501
 0.1708544616711889
 0.5183882750256887
 0.039540666370839306
 0.9220275850039967
 0.2936365813778392
 0.6266212769148911
 0.7356570139131633
 4.0
 4.0
 4.0
 4.0
 4.0

```

Note though, for single element additions you should use `push!` instead of `append!`.

---

<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: [October 30, 2020, 8:49am UTC](https://discourse.julialang.org/t/append-an-array-inside-a-function/49289/7 "2020-10-30T08:49:23Z")

</div>

There are two issues:

Every time you slice, you create a copy, so each `SV[:, 1]` inside the loop creates a new copy, and the `SV[:, 1]` in the return statement also creates a copy, and they are all different. So you have

```julia
for i in 1:5
    create copy of array
    append 4 to this copy
    forget the copy, the original array has not been modifieed
end
make another copy of the original unmodified array and return it

```

The second issue is that you cannot append or push scalars to a matrix, only to vectors. `rand(10, 1)` is a 2-dimensional array/matrix. You need to work with vectors, so it should be `SV = rand(10)`. If you are coming from Matlab, this is a very common point of confusion. Matlab users tend to use `rand(N, 1)`, `zeros(N, 1)`, `ones(N, 1)` all over the place. You’ll have to rid yourself of that habit 😉
