# Forwarding optional function inputs

**URL:** https://discourse.julialang.org/t/forwarding-optional-function-inputs/90876
**Category:** New to Julia
**Tags:** question
**Created:** [November 27, 2022, 12:59am UTC](https://discourse.julialang.org/t/forwarding-optional-function-inputs/90876 "2022-11-27T00:59:36Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Titas22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/titas22/32/39209_2.png) [@Titas22](https://discourse.julialang.org/u/Titas22)
#### Post date: [November 27, 2022, 12:59am UTC](https://discourse.julialang.org/t/forwarding-optional-function-inputs/90876/1 "2022-11-27T00:59:36Z")

</div>

Hi, is there a way to nicely overload functions with optional inputs that call other functions with optional inputs (if that makes sense).

```julia
const DEFAULT_K = 10.0;
const DEFAULT_N = 10.0;

struct A
    x::Float64
end

xtimesk(x::Float64, k::Float64 = DEFAULT_K) = x * k # (0)
xtimesk(a::A, k::Float64) = xtimesk(a.x, k) # (1)
xtimesk(a::A) = xtimesk(a.x) # (2)

xtimesn(x::Float64, n::Float64 = DEFAULT_N) = x * n # (3)
xtimesn(a::A, n::Float64 = DEFAULT_N) = xtimesk(a.x, n) # (4)

a = A(2.2)

@printf("%g\n%g\n%g\n%g\n", xtimesk(a), xtimesk(a, 5.0), xtimesn(a), xtimesn(a, 5.0))

```

I’ve put together a small example of what I’m trying to achieve above, with `xtimesk()` giving me what I want with option to call it with or without k, but is there a way to define it in a single line?

I could do something along the lines of `xtimesn()` but that means defining the default input value twice which I would also like to prevent.

Is there another way to do this? I’m coming from Matlab and there I would do this as something like this:

```julia
classdef A
    properties
        x (1,1) double
    end
    
    methods
        function this = A(inX)
            this.x = inX;
        end

        function out = xtimesk(this, varargin)
            out = xtimesk(this.x, varargin{:});
        end
    end
end

function out = xtimesk(x, k)
    if nargin < 2
        k = 10.0;
    end
    out = x * k;
end

```

I assume there’s some similar solution like varargin in Julia? But was wondering if there is also a neater way to achieve this without obfuscating the code by use of something like varargin?

Thanks,  
Titas

---

<div class="post-metadata">

### Author: ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)
#### Post date: [November 27, 2022, 1:11am UTC](https://discourse.julialang.org/t/forwarding-optional-function-inputs/90876/2 "2022-11-27T01:11:54Z")

</div>

Maybe there’s a better way, but this is what i would do:

```julia
julia> valueof(x::Number) = x
valueof (generic function with 1 method)

julia> valueof(a::A) = a.x
valueof (generic function with 2 methods)

julia> xtimesk(x, k::Float64 = DEFAULT_K) = valueof(x) * k
xtimesk (generic function with 2 methods)

julia> xtimesn(x, n::Float64 = DEFAULT_N) = valueof(x) * n
xtimesn (generic function with 2 methods)

julia> a = A(2.2)
A(2.2)

julia> @show xtimesk(a) xtimesk(a, 5.0) xtimesn(a) xtimesn(a, 5.0);
xtimesk(a) = 22.0
xtimesk(a, 5.0) = 11.0
xtimesn(a) = 22.0
xtimesn(a, 5.0) = 11.0

```

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [November 27, 2022, 3:48am UTC](https://discourse.julialang.org/t/forwarding-optional-function-inputs/90876/3 "2022-11-27T03:48:18Z")

</div>

Perhaps it would make sense to define `*(a::A, b) = *(a.x, b); xtimesk(x, k=DEFAULTK) = x*k`. Or even go into promotion rules if `A` is a `Number` candidate.

---

<div class="post-metadata">

### Author: ![Titas22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/titas22/32/39209_2.png) [@Titas22](https://discourse.julialang.org/u/Titas22)
#### Post date: [November 27, 2022, 12:48pm UTC](https://discourse.julialang.org/t/forwarding-optional-function-inputs/90876/4 "2022-11-27T12:48:06Z")

</div>

Thanks, good to know, but not exactly what I’m looking for. This seems to be equivalent to rewriting my example as:

```julia
xtimesk(x::Float64, k::Float64) = x * k # (0)
xtimesk(a::A, k::Float64 = DEFAULT_K) = xtimesk(a.x, k) # (1)

```

And ideally the defaults would be defined at the lowest level - (0) in this case. Multiplication here was just a simple example to explain what I would like to achieve.

---

<div class="post-metadata">

### Author: ![Titas22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/titas22/32/39209_2.png) [@Titas22](https://discourse.julialang.org/u/Titas22)
#### Post date: [November 27, 2022, 1:00pm UTC](https://discourse.julialang.org/t/forwarding-optional-function-inputs/90876/5 "2022-11-27T13:00:51Z")

</div>

Thanks, didn’t even think about doing it this way. This seems to do exactly what I’m looking for, however it adds a bit of indirection and if I write it like this:

```julia
struct B
    x::Float64
end

valueof(x::Number) = x
valueof(b::B) = b.x
xtimesk2(x, k::Float64 = DEFAULT_K) = valueof(x) * k
xtimesn2(x, n::Float64 = DEFAULT_N) = valueof(x) * n

b = B(2.2)
@show xtimesk2(b) xtimesk2(b, 5.0) xtimesn2(b) xtimesn2(b, 5.0)

```

I then lose the abilty to find xtimesk as a method available with A/B:

```julia
julia> methodswith(A)
[1] xtimesk(a::A) in Main at ...
[2] xtimesk(a::A, k::Float64) in Main at ...
[3] xtimesn(a::A) in Main at ...
[4] xtimesn(a::A, n::Float64) in Main at ...

julia> methodswith(B)
[1] valueof(b::B) in Main at ...

```

---

<div class="post-metadata">

### Author: ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)
#### Post date: [November 27, 2022, 4:23pm UTC](https://discourse.julialang.org/t/forwarding-optional-function-inputs/90876/6 "2022-11-27T16:23:31Z")

</div>

> [@Titas22](#):
>
> And ideally the defaults would be defined at the lowest level - (0) in this case.

Why is that ideal? Presumably the default is part of the API, so it seems reasonable to define it in public API methods.  
You can mimic your MATLAB example like this:

```julia
xtimesk(x::Float64, k::Float64 = DEFAULT_K) = x * k  
xtimesk(a::A, args...) = xtimesk(a.x, args...) 

```

but I’m not sure it’s better than simply repeating ` = DEFAULT_K` in each method signature (as long as `DEFAULT_K` itself is only defined once).

---

<div class="post-metadata">

### Author: ![Titas22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/titas22/32/39209_2.png) [@Titas22](https://discourse.julialang.org/u/Titas22)
#### Post date: [November 27, 2022, 4:36pm UTC](https://discourse.julialang.org/t/forwarding-optional-function-inputs/90876/7 "2022-11-27T16:36:56Z")

</div>

> [@yha](#):
>
> `LT_K` itself is only defined once).

Thanks, this is the answer I was looking for (although was hoping there was a nicer way in Julia where you don’t lose having named arguments).

Didn’t mean ideal as an ideal way to do something like this, was more meant as “I would like to find a way to do it like this”. Just looking to find ways to do things in Julia that I am used to in Matlab, so simply trying to replicate those and learn the language along the way.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [November 27, 2022, 9:14pm UTC](https://discourse.julialang.org/t/forwarding-optional-function-inputs/90876/8 "2022-11-27T21:14:19Z")

</div>

Your example is not exactly the same, because my definition also allows `xtimesk(1.0)`, which I would imagine is the reason you would want the default to be defined “at a lower level”. In general it is considered good Julia to add as few type annotations as possible.
