# How to default to empty string if variable unspecified to a function

**URL:** <https://discourse.julialang.org/t/how-to-default-to-empty-string-if-variable-unspecified-to-a-function/46500>\
**Category:** General Usage\
**Created:** [September 12, 2020, 9:26am UTC](https://discourse.julialang.org/t/how-to-default-to-empty-string-if-variable-unspecified-to-a-function/46500 "2020-09-12T09:26:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [September 12, 2020, 9:26am UTC](https://discourse.julialang.org/t/how-to-default-to-empty-string-if-variable-unspecified-to-a-function/46500/1 "2020-09-12T09:26:20Z")

</div>

I’d love to use Julia as a replacement for my Unix shell at least some of the time so that I can become more familiar and comfortable with the language. One function I’m having trouble defining is:

```julia
function vjus(path)
     juliaDir = "/path/to/main/julia-repo/$path"
     vim(juliaDir)
end

```

(where `vim` is a function I’ve previously defined). Now the reason I’m having difficulty with this is that I want it so that if path is unspecified and left blank, `vjus` will just open Vim to `/path/to/main/julia-repo/`. I know how to set a default for a positional argument in a function, namely using `function vjus(path=default)`, but if I leave default as empty (which is what I want to be), I get an error (namely `ERROR: LoadError: syntax: unexpected ")"` for `vjus(path=)` and `ERROR: LoadError: syntax: invalid empty character literal` for `vjus(path='')`. Is there a way around this issue?

Thanks for your time.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 12, 2020, 9:32am UTC](https://discourse.julialang.org/t/how-to-default-to-empty-string-if-variable-unspecified-to-a-function/46500/2 "2020-09-12T09:32:49Z")

</div>

Can it be that you are just looking for

```julia
function vjus(path="")
...
end

```

?

---

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [September 12, 2020, 9:34am UTC](https://discourse.julialang.org/t/how-to-default-to-empty-string-if-variable-unspecified-to-a-function/46500/3 "2020-09-12T09:34:57Z")

</div>

You’re correct. I tried `path=''` and that didn’t work (error message mentioned in my original post), but `path=""` seems to work fine. Thanks!

---

<div class="post-metadata">

**Author:** ![Dictino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dictino/32/217281_2.png) [@Dictino](https://discourse.julialang.org/u/Dictino)\
**Post date:** [September 12, 2020, 9:39am UTC](https://discourse.julialang.org/t/how-to-default-to-empty-string-if-variable-unspecified-to-a-function/46500/4 "2020-09-12T09:39:59Z")

</div>

In addition you can use multiple distpach so you can just type ` vjus()` without arguments

```julia
vjus()=vjus(path="")

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 12, 2020, 9:45am UTC](https://discourse.julialang.org/t/how-to-default-to-empty-string-if-variable-unspecified-to-a-function/46500/5 "2020-09-12T09:45:50Z")

</div>

In this case `path` would be a keyword argument, what you mean is:

```julia
function vjus(path)
...
end
vjus() = vjus("")

```

---

<div class="post-metadata">

**Author:** ![Dictino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dictino/32/217281_2.png) [@Dictino](https://discourse.julialang.org/u/Dictino)\
**Post date:** [September 12, 2020, 9:48am UTC](https://discourse.julialang.org/t/how-to-default-to-empty-string-if-variable-unspecified-to-a-function/46500/6 "2020-09-12T09:48:08Z")

</div>

Yes, that is what I mean 😅

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [September 12, 2020, 9:59am UTC](https://discourse.julialang.org/t/how-to-default-to-empty-string-if-variable-unspecified-to-a-function/46500/7 "2020-09-12T09:59:51Z")

</div>

Note that `'` is for specifying `Char`

```julia
julia> 'a'
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

julia> '🐳'
'🐳': Unicode U+1F433 (category So: Symbol, other)

julia> typeof(ans)
Char

julia> sizeof(ans)
4

```

A 4-byte object. As such, we can do things like `reinterpret` back and fourth between numbers and `Char`s:

```julia
julia> reinterpret(Char, 0x61000000) # or Int32(1627389952)
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

julia> reinterpret(Char, 0xf09f90b3) # or Int32(-257978189)
'🐳': Unicode U+1F433 (category So: Symbol, other)

```

While a `String` is a collection of variable numbers of characters, and definitely not of fixed size.

You can see that your error message doesn’t come interpolating a `Char`, but from the fact that we can’t create an empty one:

```julia
julia> ''
ERROR: syntax: invalid empty character literal
Stacktrace:
 [1] top-level scope at none:1

julia> ""
""

```
