# How to create a function in Julia that can accept an array or a scalar as input argument?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-function-in-julia-that-can-accept-an-array-or-a-scalar-as-input-argument/36394>\
**Category:** New to Julia\
**Created:** [March 23, 2020, 3:08pm UTC](https://discourse.julialang.org/t/how-to-create-a-function-in-julia-that-can-accept-an-array-or-a-scalar-as-input-argument/36394 "2020-03-23T15:08:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [March 23, 2020, 3:08pm UTC](https://discourse.julialang.org/t/how-to-create-a-function-in-julia-that-can-accept-an-array-or-a-scalar-as-input-argument/36394/1 "2020-03-23T15:08:03Z")

</div>

Hi, when creating new functions in Julia, do I need to specify the input type of the arguments ? Is this required for speed or only for organization ?

For example, I would like to create a function that takes as input either an array or a scalar number. How would this be possible within the scope of Julia function definitions and code organization ?

I would prefer to specify the input type of my function for easy read of what it expect as input.

```julia
function printNumber(x::Array{Number})
    println("value = ", x)
end

inputValue = 10.0
printNumber([inputValue]) # This works

inputValue = 10
printNumber(inputValue) ## This fails

```

---

<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:** [March 23, 2020, 3:32pm UTC](https://discourse.julialang.org/t/how-to-create-a-function-in-julia-that-can-accept-an-array-or-a-scalar-as-input-argument/36394/2 "2020-03-23T15:32:42Z")

</div>

> [@josimar](#):
>
> Hi, when creating new functions in Julia, do I need to specify the input type of the arguments ? Is this required for speed or only for organization ?

No, this is not required.  
There is a recent thread

> [@Multiplication times for complex matrices: julia vs Matlab](https://discourse.julialang.org/t/multiplication-times-for-complex-matrices-julia-vs-matlab/36163/):
>
> I’ve been looking at some different filter approaches for analyzing ECG signals using Matlab and have recently implemented them in julia in order to get a speed improvement. I have not been seeing the expected improvements and have tracked the issue down to the time for multiplying complex matrices. I have made some simple examples in both cases to evaluate the times. The julia example is rough as I am new to julia. #!/usr/local/bin/julia module TmMlt ENV["CMPLXROOT"] = "/home/martin/Dropbox/…

where there was a nice explanation about this by @bcmichael

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 23, 2020, 4:57pm UTC](https://discourse.julialang.org/t/how-to-create-a-function-in-julia-that-can-accept-an-array-or-a-scalar-as-input-argument/36394/3 "2020-03-23T16:57:33Z")

</div>

> [@josimar](#):
>
> For example, I would like to create a function that takes as input either an array or a scalar number. How would this be possible within the scope of Julia function definitions and code organization ?

Sure, you _can_ do this, although it’s uncommon. Here is a function that takes either a `Number` or any kind of array-like collection of numbers:

```julia
function my_print(x::Union{Number, AbstractArray{<:Number}})
  println("value = ", x)
end

```

This style is no longer common in Julia because many of us have found that it is clearer and more performant to just write the scalar version of a function and then apply that scalar function to a collection via broadcasting (see [More Dots: Syntactic Loop Fusion in Julia](https://julialang.org/blog/2017/01/moredots/) ). For example, if you have a function `f(x::Number)`, then you can apply `f` to an array `a` by doing `f.(a)` without having to change anything about the definition of `f`.

---

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [March 24, 2020, 12:11pm UTC](https://discourse.julialang.org/t/how-to-create-a-function-in-julia-that-can-accept-an-array-or-a-scalar-as-input-argument/36394/4 "2020-03-24T12:11:45Z")

</div>

@rdeits, thank you very much for the detailed reply.

The concept of writing a scalar function and then use broadcasting is still not entirely clear to me. For example, the function that I am trying to make it work for both scalar and arrays is below.

Ideally, in the function below the argument `tvector` would take an array of either floats or integers, whereas `tsearch_all` could be either an array or a scalar, both also float or integer.

How would I apply the concept of broadcasting in this case ?

```julia
function getIndexSpecificTime(tvector,tsearch_all)
    #=
        Get index of a specific time
        Input:
            tvector: Array containing the time
            tsearch_all:: array containg the times to get the indexes

        Output:
            list of index into the tvector array
    =#

    ind_final=zeros(Int64,length(tsearch_all))
    for i=1:length(tsearch_all)
        minv, ind1=findmin( abs.(tvector .- tsearch_all[i]) )
        ind_final[i]=ind1
    end

    return ind_final

end ### getIndexSpecificTime

tAll = 1:10
tSearch = [5,6]

ind = getIndexSpecificTime(tAll, tSearch)
println.(ind)

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 24, 2020, 1:45pm UTC](https://discourse.julialang.org/t/how-to-create-a-function-in-julia-that-can-accept-an-array-or-a-scalar-as-input-argument/36394/5 "2020-03-24T13:45:18Z")

</div>

Here’s an example:

```julia
# getIndexSpecificTime now expects just a single scalar `tsearch`
julia> function getIndexSpecificTime(tvector, tsearch)
         minv, ind1 = findmin(abs.(tvector .- tsearch))
         return ind1
       end
getIndexSpecificTime (generic function with 1 method)

julia> tAll = 1:10
1:10

# We can call it on a scalar, as expected
julia> getIndexSpecificTime(tAll, 2.0)
2

julia> tSearch = [5, 6]
2-element Array{Int64,1}:
 5
 6

# Or we can apply it to a vector `tSearch` via broadcasting.
# Note the use of `Ref()` here: we want to apply `getIndexSpecificTime`
# separately to each element in `tSearch`, but we want to use the *whole*
# `tAll` each time. The `Ref` tells broadcasting to pass the entire `tAll` array
# at every iteration, treating it as if it were an atomic scalar value. 
julia> getIndexSpecificTime.(Ref(tAll), tSearch)
2-element Array{Int64,1}:
 5
 6

```
