# An empty array as the default value for optional arguments

**URL:** https://discourse.julialang.org/t/an-empty-array-as-the-default-value-for-optional-arguments/17068
**Category:** New to Julia
**Created:** [November 1, 2018, 8:02pm UTC](https://discourse.julialang.org/t/an-empty-array-as-the-default-value-for-optional-arguments/17068 "2018-11-01T20:02:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![chkwon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chkwon/32/3632_2.png) [@chkwon](https://discourse.julialang.org/u/chkwon)
#### Post date: [November 1, 2018, 8:02pm UTC](https://discourse.julialang.org/t/an-empty-array-as-the-default-value-for-optional-arguments/17068/1 "2018-11-01T20:02:56Z")

</div>

```julia
function foo(x::Int64, y::Array{String}=Array{String}(undef,0))
    return x + length(y)
end

```

When `y` is an optional argument, I want to assign an empty array as the default value. While the above code works, it doesn’t seem easy to read (and not pretty). Is there a more desirable way of doing this?

---

<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: [November 1, 2018, 8:13pm UTC](https://discourse.julialang.org/t/an-empty-array-as-the-default-value-for-optional-arguments/17068/2 "2018-11-01T20:13:01Z")

</div>

```julia
function foo(x::Int64, y::Array{String}=String[])
    return x + length(y)
end

```

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [November 1, 2018, 8:14pm UTC](https://discourse.julialang.org/t/an-empty-array-as-the-default-value-for-optional-arguments/17068/3 "2018-11-01T20:14:09Z")

</div>

There’s no special syntax for “empty array as default value”. You can use `String[]` to create an empty array though, and you don’t necessarily need the `::Array{String}` (it seems that you more likely want `Vector{String}` at least) and you almost certainly don’t need/want the `Int64`.

---

<div class="post-metadata">

### Author: ![chkwon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chkwon/32/3632_2.png) [@chkwon](https://discourse.julialang.org/u/chkwon)
#### Post date: [November 1, 2018, 11:52pm UTC](https://discourse.julialang.org/t/an-empty-array-as-the-default-value-for-optional-arguments/17068/4 "2018-11-01T23:52:54Z")

</div>

Thanks. The function return value was just arbitrarily created. In fact `x` needs to be an integer since it will be used as an index for an array. But I guess it needs better be `Integer` instead of `Int64`. For the array part in my case, I think you are right. I don’t need to specify the type for `y` I guess.
