# Trying to get the arity of a function

**URL:** https://discourse.julialang.org/t/trying-to-get-the-arity-of-a-function/66630
**Category:** New to Julia
**Created:** [August 18, 2021, 9:46pm UTC](https://discourse.julialang.org/t/trying-to-get-the-arity-of-a-function/66630 "2021-08-18T21:46:39Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)
#### Post date: [August 18, 2021, 9:46pm UTC](https://discourse.julialang.org/t/trying-to-get-the-arity-of-a-function/66630/1 "2021-08-18T21:46:39Z")

</div>

I written a function to return the arity of a given function:

function arity(f)  
if f == + return 2 # arithmetic functions  
if f == - return 2  
if f == \* return 2  
if f == / return 2  
if f == ^ return 2  
if f == % return 2

```
if f == sin return 1 # trig functions
if f == cos return 1
if f == tan return 1

```

end

It works fine for +, -, sin, cos, and tan, but fails for the others with the error message:  
**ERROR:** syntax: “\*” is not a unary operator.

Why doesn’t this work for all the included functions?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 18, 2021, 10:04pm UTC](https://discourse.julialang.org/t/trying-to-get-the-arity-of-a-function/66630/2 "2021-08-18T22:04:19Z")

</div>

Try adding `:` before the troublesome symbols.

_ **NB:** and back-ticks in your post in the lines before `function` and after `end`_

---

<div class="post-metadata">

### Author: ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)
#### Post date: [August 18, 2021, 10:08pm UTC](https://discourse.julialang.org/t/trying-to-get-the-arity-of-a-function/66630/3 "2021-08-18T22:08:14Z")

</div>

That works, many thanks!

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [August 19, 2021, 8:40am UTC](https://discourse.julialang.org/t/trying-to-get-the-arity-of-a-function/66630/4 "2021-08-19T08:40:21Z")

</div>

In julia, a function does not have a fixed arity, only methods do. I believe you can get the arity of a method like so

```julia
julia> methods(sin).ms[1].nargs-1
1

julia> methods(+).ms[1].nargs-1
2

```

Note the indexing `[1]` picking the first out of many methods in the method list. The `-1` is required since I believe the function itself counts as one argument.

---

<div class="post-metadata">

### Author: ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)
#### Post date: [August 19, 2021, 10:03am UTC](https://discourse.julialang.org/t/trying-to-get-the-arity-of-a-function/66630/5 "2021-08-19T10:03:43Z")

</div>

Many thanks for thid information!

---

<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: [August 19, 2021, 2:29pm UTC](https://discourse.julialang.org/t/trying-to-get-the-arity-of-a-function/66630/6 "2021-08-19T14:29:42Z")

</div>

If you are happy with the manual-ness of the first solution you can use dispatch for it:

```julia
arity(::typeof(+)) = 2
arity(::typeof(sin)) = 1
...

julia> arity(+)
2

```

---

<div class="post-metadata">

### Author: ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)
#### Post date: [August 19, 2021, 3:28pm UTC](https://discourse.julialang.org/t/trying-to-get-the-arity-of-a-function/66630/7 "2021-08-19T15:28:54Z")

</div>

Very interesting!

```julia
arity(f) = extrema(m -> m.nargs, methods(f)) .- 1
findarity(f, k) = (ms = collect(methods(f)); ms[findall(m -> m.nargs - 1 == k, ms)])

arity(+)

```

> (1, 5)

Why is the maximum of the arities of `+` equal to 5? 😳

```julia
findarity(+, 5)

```

> 2-element Vector{Method}:
> 
> - +(a:: **BigInt** , b:: **BigInt** , c:: **BigInt** , d:: **BigInt** , e:: **BigInt** ) in Base.GMP at [gmp.jl:521](https://github.com/JuliaLang/julia/tree/1b93d53fc4bb59350ada898038ed4de2994cce33/base/gmp.jl#L521)
> - +(a:: **BigFloat** , b:: **BigFloat** , c:: **BigFloat** , d:: **BigFloat** , e:: **BigFloat** ) in Base.MPFR at [mpfr.jl:549](https://github.com/JuliaLang/julia/tree/1b93d53fc4bb59350ada898038ed4de2994cce33/base/mpfr.jl#L549)

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [August 19, 2021, 3:48pm UTC](https://discourse.julialang.org/t/trying-to-get-the-arity-of-a-function/66630/8 "2021-08-19T15:48:31Z")

</div>

```julia
julia> +(1,2,3,4,5)
15

```

`+` can take many arguments. The reason might be that intermediate allocations can be avoided for `+(A,B,C)` where the arguments are matrices.
