# Specifying that a function which can take a vector or scalar as input

**URL:** <https://discourse.julialang.org/t/specifying-that-a-function-which-can-take-a-vector-or-scalar-as-input/32042>\
**Category:** Performance\
**Created:** [December 9, 2019, 10:10am UTC](https://discourse.julialang.org/t/specifying-that-a-function-which-can-take-a-vector-or-scalar-as-input/32042 "2019-12-09T10:10:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [December 9, 2019, 10:10am UTC](https://discourse.julialang.org/t/specifying-that-a-function-which-can-take-a-vector-or-scalar-as-input/32042/1 "2019-12-09T10:10:03Z")

</div>

I frequently find myself writing functions which are fundamentally not that different for scalars or vectors. Take this for example:

```julia
julia> function silly_print(x)
    for i = 1:length(x)
        println(x[i])
    end
end

julia> silly_print(2)
2

julia> silly_print([2,4])
2
4

```

Specifying that x is a Vector breaks this behaviour:

```julia
julia> function silly_print_vector(x::Vector)
    for i = 1:length(x)
        println(x[i])
    end
end
silly_print_vector (generic function with 1 method)

julia> silly_print_vector(2)
ERROR: MethodError: no method matching silly_print_vector(::Int64)
Closest candidates are:
  silly_print_vector(::Array{T,1} where T) at none:1
Stacktrace:
 [1] top-level scope at none:0

```

Is there a workaround for this, without writing two identical functions but for different types?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [December 9, 2019, 10:20am UTC](https://discourse.julialang.org/t/specifying-that-a-function-which-can-take-a-vector-or-scalar-as-input/32042/2 "2019-12-09T10:20:16Z")

</div>

You can write it just for scalars and then use broadcasting. E.g.,

```julia
julia> println.(1:3);
1
2
3

```

---

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [December 9, 2019, 11:01am UTC](https://discourse.julialang.org/t/specifying-that-a-function-which-can-take-a-vector-or-scalar-as-input/32042/3 "2019-12-09T11:01:44Z")

</div>

Hadn’t thought of that!

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [December 9, 2019, 12:33pm UTC](https://discourse.julialang.org/t/specifying-that-a-function-which-can-take-a-vector-or-scalar-as-input/32042/4 "2019-12-09T12:33:37Z")

</div>

If you need more complex things than one `.`, it often works well to define say `silly_print(x::Number)`, and then `silly_print(x::AbstractArray)` which does some work (say it prints `summary(x)`) before iself calling the `::Number` method.

---

<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:** [December 9, 2019, 4:04pm UTC](https://discourse.julialang.org/t/specifying-that-a-function-which-can-take-a-vector-or-scalar-as-input/32042/5 "2019-12-09T16:04:38Z")

</div>

> [@gobs](#):
>
> Is there a workaround for this, without writing two identical functions but for different types?

If you really need a function that accepts both arrays and scalars, either don’t declare the argument type at all (there is no performance penalty in Julia for duck typing!) just use a `Union`: declare `function myfunction(x::Union{Number, AbstractVector}`, for example.

But if your function is that generic, you way want it to work for any [iterable object](https://docs.julialang.org/en/v1/base/collections/#lib-collections-iteration-1) anyway, in which case it is better to duck type and avoid indexing:

```julia
function myfunction(itr)
    for x in itr
        # do something with x
    end
end

```
