# How to define function parameters to be read as same type?

**URL:** <https://discourse.julialang.org/t/how-to-define-function-parameters-to-be-read-as-same-type/68696>\
**Category:** General Usage\
**Tags:** function\
**Created:** [September 24, 2021, 2:08pm UTC](https://discourse.julialang.org/t/how-to-define-function-parameters-to-be-read-as-same-type/68696 "2021-09-24T14:08:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Minimum-Pollution-96](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Minimum-Pollution-96](https://discourse.julialang.org/u/Minimum-Pollution-96)\
**Post date:** [September 24, 2021, 2:08pm UTC](https://discourse.julialang.org/t/how-to-define-function-parameters-to-be-read-as-same-type/68696/1 "2021-09-24T14:08:29Z")

</div>

Hi,

This is how I’m defining the function.

```julia
function unrestricted_search(f::Function, δ::T, x0::T) where T# Maximization algorithm
    f0 = f(x0)
    Data = DataFrame(f=f0, x=x0)
    diff = f(x0+δ) - f(x0)
    if diff < 0
        δ = -δ
    end
    x1 = x0 + δ
    push!(Data, (f=f(x1), x=x1))
    n=2
    while Data.f[end-1] < Data.f[end]
        x = x0 + n*δ
        push!(Data, (f=f(x), x=x))
        n += 1
    end
    Optima = Data[n-1, :]
    return Data, n, Optima
end

```

When I call the function with unmatching data types,

```julia
unrestricted_search(f, 0.1, 0)

```

It results in the following error.

```julia
LoadError: InexactError: Int64(0.1)
InexactError: Int64(0.1)

```

Is there a way to automatically read the input as Float64 if one of them is float?

PS:  
Also, as a side note. I created the following function for testing

```julia
function addition(x1::T, x2::T) where T
    x3 = x1 + x2
    return
end
addition(1.,2)

```

which results in the following error

```julia
LoadError: MethodError: no method matching addition(::Float64, ::Int64)
e[0mClosest candidates are:
e[0m addition(::T, e[91m::Te[39m) where T at In[57]:1
MethodError: no method matching addition(::Float64, ::Int64)
Closest candidates are:
  addition(::T, ::T) where T at In[57]:1

```

Why are the errors different ?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 24, 2021, 2:17pm UTC](https://discourse.julialang.org/t/how-to-define-function-parameters-to-be-read-as-same-type/68696/2 "2021-09-24T14:17:26Z")

</div>

Your function has a lot of overhead by doing optimization in a `DataFrame`. I would suggest you don’t specify the types at all

you probably don’t care about them being the same type. You can always:

```julia
julia> x, y = promote(0.1, 0)
(0.1, 0.0)

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [September 24, 2021, 2:19pm UTC](https://discourse.julialang.org/t/how-to-define-function-parameters-to-be-read-as-same-type/68696/3 "2021-09-24T14:19:15Z")

</div>

It seems to me the first error happens because you are creating a DataFrame with an integer column, and then try to push floats to it. You can do `Float64(x0)` in creating the DataFrame to get a float column.

The second error is because `addition(x1::T, x2::T) where T` means `x1` and `x2` have to have the same type. That should also affect your first definition though, are you sure you haven’t defined this function multiple times in different ways and are calling old methods?

---

<div class="post-metadata">

**Author:** ![Minimum-Pollution-96](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Minimum-Pollution-96](https://discourse.julialang.org/u/Minimum-Pollution-96)\
**Post date:** [September 24, 2021, 4:11pm UTC](https://discourse.julialang.org/t/how-to-define-function-parameters-to-be-read-as-same-type/68696/4 "2021-09-24T16:11:46Z")

</div>

Thanks for the clarification. No, the function is defined once. Also, when I defined as a normal function (without defining type T), the first function was throwing the same error. I guess its due to the DataFrame as mentioned above.
