# Bounds in function arguments

**URL:** https://discourse.julialang.org/t/bounds-in-function-arguments/25635
**Category:** Performance
**Created:** [June 25, 2019, 12:51am UTC](https://discourse.julialang.org/t/bounds-in-function-arguments/25635 "2019-06-25T00:51:57Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![fipelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fipelle/32/4772_2.png) [@fipelle](https://discourse.julialang.org/u/fipelle)
#### Post date: [June 25, 2019, 12:51am UTC](https://discourse.julialang.org/t/bounds-in-function-arguments/25635/1 "2019-06-25T00:51:57Z")

</div>

Hi,

I am writing down a set of functions, and I want to make sure that some of the arguments are bounded within some given values.

For the sake of simplicity, say that I only have the following function:

```julia
function my_fun(w::Float64)
...
end

```

and that `w` is bounded between `0.0` and `1.0`. Right now, I am throwing an error if `w` is lower than `0.0` or larger than `1.0` via:

```julia
if w < 0 || w > 1
    error("0 ≤ w ≤ 1");
end

```

I was wondering whether there are more efficient ways to approach this problem. I was thinking that since `w` is a restricted `Float64` I could define a type that takes into account its bounds. However, I am not sure whether this is good practise, and if it can be easily done in Julia.

What would you recommend to do? In my application, I have many arguments to check and individual `if-end` are not very tidy.

---

<div class="post-metadata">

### Author: ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)
#### Post date: [June 25, 2019, 1:00am UTC](https://discourse.julialang.org/t/bounds-in-function-arguments/25635/2 "2019-06-25T01:00:12Z")

</div>

You could make a custom type, but that seems complicated. How about putting your error check into a function? It would be tidier.

```julia
function is01(x)
       if x < 0 || x > 1
       error("0 ≤ x ≤ 1")
       end
  end
function my_fun(w1, w2, w3)
       is01(w1); is01(w2); is01(w3);
       ...
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: [June 25, 2019, 1:02am UTC](https://discourse.julialang.org/t/bounds-in-function-arguments/25635/3 "2019-06-25T01:02:58Z")

</div>

At the end of the day, you won’t get anything more efficient then what you already have here. It also won’t make any difference in the writability for this isolated case so you need to decide what is the more complete case. What kind of problem are you trying to solve.

Do you have any need to statically dispatch based on the range of the value? (I.e. the range of the values could be determined at compile time.) If not (most likely the case), you are much better off with check on the value at runtime with branches.

If you just want a shorter syntax, just define a function for that check like what you will do in any other language. Type is not the solution to all problems.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [June 25, 2019, 5:55am UTC](https://discourse.julialang.org/t/bounds-in-function-arguments/25635/4 "2019-06-25T05:55:10Z")

</div>

Check out

> **[GitHub - jw3126/ArgCheck.jl: Package for checking function arguments](https://github.com/jw3126/ArgCheck.jl)**
>
> Package for checking function arguments. Contribute to jw3126/ArgCheck.jl development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![fipelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fipelle/32/4772_2.png) [@fipelle](https://discourse.julialang.org/u/fipelle)
#### Post date: [June 25, 2019, 10:55am UTC](https://discourse.julialang.org/t/bounds-in-function-arguments/25635/5 "2019-06-25T10:55:00Z")

</div>

Thank you. Will do!
