# Modify arrays in function

**URL:** <https://discourse.julialang.org/t/modify-arrays-in-function/27243>\
**Category:** General Usage\
**Created:** [August 6, 2019, 9:42pm UTC](https://discourse.julialang.org/t/modify-arrays-in-function/27243 "2019-08-06T21:42:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![elemenopeereste](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@elemenopeereste](https://discourse.julialang.org/u/elemenopeereste)\
**Post date:** [August 6, 2019, 9:42pm UTC](https://discourse.julialang.org/t/modify-arrays-in-function/27243/1 "2019-08-06T21:42:45Z")

</div>

How to modify array **t=[1,2,3]** in function like  
function dosth!( arg )  
. . .  
end  
In body i wanna in first case change my array to empty(arg = [] ), in second case change table something like ( arg = arg[2:end] )

After using function result should be

1. 0-element Array{Any,1}
2. 2-element Array{Int64,1}:

---

<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:** [August 6, 2019, 9:49pm UTC](https://discourse.julialang.org/t/modify-arrays-in-function/27243/2 "2019-08-06T21:49:18Z")

</div>

You can empty an array with the `empty!()` function:

```julia
julia> t = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> empty!(t)
0-element Array{Int64,1}

julia> t
0-element Array{Int64,1}

```

and you can delete an element of an array with `deleteat!()`:

```julia
julia> t = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> deleteat!(t, 1)
2-element Array{Int64,1}:
 2
 3

julia> t
2-element Array{Int64,1}:
 2                                                                                                                                                                                                                                                                                                                                    
 3  

```

Or, more efficiently, by using `popfirst!` if you want to delete the first element:

```julia
julia> t = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> popfirst!(t)
1

julia> t
2-element Array{Int64,1}:
 2
 3

```

---

<div class="post-metadata">

**Author:** ![elemenopeereste](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@elemenopeereste](https://discourse.julialang.org/u/elemenopeereste)\
**Post date:** [August 6, 2019, 10:02pm UTC](https://discourse.julialang.org/t/modify-arrays-in-function/27243/3 "2019-08-06T22:02:21Z")

</div>

And if I want to do it without embedded function?  
Something more linke in MATLAB? It’s bad way manipulate in range [from : to]. It’s possible?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [August 6, 2019, 10:10pm UTC](https://discourse.julialang.org/t/modify-arrays-in-function/27243/4 "2019-08-06T22:10:49Z")

</div>

> [@elemenopeereste](#):
>
> Something more linke in MATLAB? It’s bad way manipulate in range [from : to]. It’s possible?

something like this?

```julia
t = [4,5,6]
t=t[2:end] # [5,6]
t=t[[1,3]] #[4,6], first and third elements 

```

you can do it, it’s not the most performant way (it copies the array), but it’s possible

---

<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:** [August 6, 2019, 10:14pm UTC](https://discourse.julialang.org/t/modify-arrays-in-function/27243/5 "2019-08-06T22:14:22Z")

</div>

There’s an important difference: `popfirst!(t)` modifies the array itself, while `t = t[2:3]` creates a completely new array and then assigns the name `t` to that new array. In one case the modification will be visible outside of the function and in the other case it will not.
