# Addind Options to Function

**URL:** <https://discourse.julialang.org/t/addind-options-to-function/57209>\
**Category:** New to Julia\
**Tags:** question, jump\
**Created:** [March 15, 2021, 3:47pm UTC](https://discourse.julialang.org/t/addind-options-to-function/57209 "2021-03-15T15:47:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marianoarnaiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marianoarnaiz/32/19377_2.png) [@marianoarnaiz](https://discourse.julialang.org/u/marianoarnaiz)\
**Post date:** [March 15, 2021, 3:47pm UTC](https://discourse.julialang.org/t/addind-options-to-function/57209/1 "2021-03-15T15:47:58Z")

</div>

Hi everyone.  
I think this is really simple but I am not getting around it.

I have this function Rps that does some computation. Now, to the final results I can add some corrections. I would like to be able to do:

Rps(all arguments) → Standard output  
Rps(all aguments, ‘Flat’) → Standard Output  
Rps(all aguments, ‘Sphere’) → Apply spherical correction  
Rps(all aguments, ‘Anaelastic’) → Apply anaelastic correction  
Rps(all aguments,‘Sphere’, ‘Anaelastic’) → Apply both corrections

How can I get these keywords working inside the functions body?

---

<div class="post-metadata">

**Author:** ![bvdmitri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bvdmitri/32/22935_2.png) [@bvdmitri](https://discourse.julialang.org/u/bvdmitri)\
**Post date:** [March 15, 2021, 4:44pm UTC](https://discourse.julialang.org/t/addind-options-to-function/57209/2 "2021-03-15T16:44:13Z")

</div>

You can use Julia’s multiple dispatch for this purposes. There is one of possible solutions:

```julia
struct Flat end
struct Sphere end
struct Anaelastic end

function Rps(all_arguments) 
    return Rps(all_arguments, Flat())
end

# type declaration :: in function argument without a name indicates that 
# the actual value of Flat object is unused, it only used for dispatch
# it is the same as 'unused::Flat'

function Rps(all_arguments, ::Flat) 
    ...
end

function Rps(all_arguments, ::Sphere) 
    ...
end

function Rps(all_arguments, ::Anaelastic) 
    ...
end

function Rps(all_arguments, ::Sphere, ::Anaelastic) 
    ...
end

```

Later on you can use your function as:

```julia
Rps(all_arguments)
Rps(all_arguments, Flat())
Rps(all_arguments, Sphere())
Rps(all_arguments, Sphere(), Anaelastic())

```

Structure with no fields creates a singleton object. It is completely fine to create this objects to dispatch on different implementations of your code. In many cases compiler will inline and eliminate `Flat()` or `Sphere()` objects creation at all. Unless you do it dynamically, e.g.

```julia
method = rand() > .5 ? Flat() : Sphere()
Rps(all_arguments, method)

```

What you can also do is to use `Val` built-in object to pass string-like compile-time values to your functions. For example

```julia
function Rps(all_arguments, ::Val{ :Flat })
    ...
end

```

and use it as

```julia
Rps(all_arguments, Val(:Flat))

```

`Val` is a powerful mechanism, but has to be used carefully and with caution. In some cases it may worsen the performance of your program.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 15, 2021, 5:48pm UTC](https://discourse.julialang.org/t/addind-options-to-function/57209/3 "2021-03-15T17:48:26Z")

</div>

Another more pedestrian alternative is:

```julia
julia> function f(x,args...)
         for arg in args
           if arg == "One"
             x += 1
           elseif arg == "Two"
             x += 2
           end
         end
         x
       end
f (generic function with 1 method)

julia> f(1)
1

julia> f(1,"One")
2

julia> f(1,"Two")
3

julia> f(1,"One","Two")
4

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 15, 2021, 6:40pm UTC](https://discourse.julialang.org/t/addind-options-to-function/57209/4 "2021-03-15T18:40:25Z")

</div>

> [@marianoarnaiz](#):
>
> How can I get these keywords working inside the functions body?

From the sound of it, I would just have something like `function Rps(args...; corrections=())` and then call it as `Rps(args...; corrections=(Sphere, Anaelastic))` where you’ve defined e.g. an `@enum` of correction options (or maybe they are some other type… depends on how you perform this processing and how flexible you want to be). Then in the body of your function you can loop over the `corrections`.
