# How to get the variable numbers for a julia function

**URL:** https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682
**Category:** General Usage
**Created:** [March 29, 2020, 4:17pm UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682 "2020-03-29T16:17:44Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)
#### Post date: [March 29, 2020, 4:17pm UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/1 "2020-03-29T16:17:44Z")

</div>

I have a function called f for example.

```julia
function f(x1,x2,x3)
x1+x2*x3
end

```

How can get the number of variable of this function? For this example it should return 3.

---

<div class="post-metadata">

### Author: ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)
#### Post date: [March 29, 2020, 4:47pm UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/2 "2020-03-29T16:47:06Z")

</div>

You can do something like this:

```julia
julia> function f(args...)
           return length(args)
       end
f (generic function with 1 method)

julia> f(1,2,3)
3

```

---

<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: [March 29, 2020, 7:16pm UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/3 "2020-03-29T19:16:45Z")

</div>

What are you trying to accomplish? The analogue of `nargin` in Matlab in Julia is to define multiple methods (or optional/keyword arguments).

---

<div class="post-metadata">

### Author: ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)
#### Post date: [March 29, 2020, 7:57pm UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/4 "2020-03-29T19:57:07Z")

</div>

> [@stevengj](#):
>
> accomplish

No. How to use the “accomplish”? I tried

```julia
accomplish(f)

```

But it seems not works. By the way, the “nargin” in Matlab does works.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [March 29, 2020, 9:22pm UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/5 "2020-03-29T21:22:53Z")

</div>

He’s asking what you are trying to do. There is no `accomplish` function.

In Matlab `nargin` is an important function that you need to use all the time to figure out what inputs you have. In Julia you don’t need this at all.

**Edit:** I meant to reply to @gangchern

---

<div class="post-metadata">

### Author: ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)
#### Post date: [March 30, 2020, 12:36am UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/6 "2020-03-30T00:36:03Z")

</div>

Matlab:

```julia
function ret = f(x1,x2)
    if nargin == 1
        ret = x1;
    elseif nargin == 2
        ret = x1+x2;
    end
end

```

and then:

```julia
>> f(2)

ans =

     2

>> f(2,2)

ans =

     4

```

In Julia:

```julia
julia> f(x1) = x1
f (generic function with 1 method)

julia> f(x1,x2) = x1+x2
f (generic function with 2 methods)

julia> f(2)
2

julia> f(2,2)
4

```

---

<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: [March 30, 2020, 1:26am UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/7 "2020-03-30T01:26:13Z")

</div>

There’s also optioinal argument and keyword argument combining with dispatch usally cover most of the cases I need to deal with variadic functions.

And if all else failed, you can still declare the function as variadic which @mbaz mentioned in [the very first reply](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/2) already…

---

<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: [March 30, 2020, 1:29am UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/8 "2020-03-30T01:29:08Z")

</div>

And if you want to figure out how many argument a function can accept from outside the function, then no that’s not a property of a function and you generally should not rely on that.

Unless it’s for debugging, in which case you can just iterate through the `methods`.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [March 30, 2020, 6:13am UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/9 "2020-03-30T06:13:25Z")

</div>

I don’t think you can do that in Matlab either.

---

<div class="post-metadata">

### Author: ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)
#### Post date: [March 30, 2020, 6:38am UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/10 "2020-03-30T06:38:56Z")

</div>

Thanks,  
This code is useful for me. Perhaps Julia do not have inherit method to get the number of variables of a function. As other people replied, it is not necessary for Julia.

---

<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: [March 30, 2020, 7:46am UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/11 "2020-03-30T07:46:15Z")

</div>

> [@gangchern](#):
>
> get the number of variable of this function

To add to the excellent answers above: strictly speaking functions (eg `f`) don’t have an argument list, [methods](https://docs.julialang.org/en/v1/manual/methods/) (eg `f(::Any, ::Any, ::Any)` do. The distinction between the two is crucial to using Julia efficiently.

Also, unless you are debugging or doing something very advanced, trying to recover this meta-information is usually a sign that the code is not well-designed for Julia.

---

<div class="post-metadata">

### Author: ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)
#### Post date: [March 30, 2020, 8:43pm UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/12 "2020-03-30T20:43:49Z")

</div>

Thank 👍

---

<div class="post-metadata">

### Author: ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)
#### Post date: [September 27, 2020, 1:12pm UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/13 "2020-09-27T13:12:16Z")

</div>

Hi @gangchern!

I also had this question, and @Tamas_Papp’s answer put two and two together for me, haha.

Thanks for asking it.

Also, you should mark his answer as the solution so future enquirers will find it easier. 🙂

---

<div class="post-metadata">

### Author: ![JeffFessler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jefffessler/32/6650_2.png) [@JeffFessler](https://discourse.julialang.org/u/JeffFessler)
#### Post date: [October 1, 2020, 6:17pm UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/14 "2020-10-01T18:17:24Z")

</div>

Just for completeness, there is a way to access the number of arguments of a particular `Method`:

```julia
f = (x,y) -> x + y
first(methods(f)).nargs - 1

```

This is used nicely in the `LinearMaps.jl` package that certainly qualifies as “something very advanced” as @Tamas_Papp said…  
I don’t understand why the `-1` is needed but it works…  
But this is probably _not_ what most Matlab users want when they search for `nargin`

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [October 1, 2020, 7:21pm UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/15 "2020-10-01T19:21:23Z")

</div>

Complementing even more, I would directly translate the given Matlab example

> [@mbaz](#):
>
> ```julia
> function ret = f(x1,x2)
> if nargin == 1
> ret = x1;
> elseif nargin == 2
> ret = x1+x2;
> end
> end
> 
> ```

to

```julia
julia> function f(x1, x2 = nothing)
           if x2 === nothing
               return x1
           else
               return x1 + x2
           end
       end
f (generic function with 2 methods)

julia> f(2)
2

julia> f(2, 2)
4

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [October 1, 2020, 8:09pm UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/16 "2020-10-01T20:09:01Z")

</div>

> [@Henrique\_Becker](#):
>
> Mathlab

This might be a typo, but: Matlab = Matrix Laboratory.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [October 1, 2020, 8:18pm UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/17 "2020-10-01T20:18:06Z")

</div>

It was a typo. Corrected.

---

<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: [October 2, 2020, 7:50am UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/18 "2020-10-02T07:50:49Z")

</div>

> [@JeffFessler](#):
>
> there is a way to access the number of arguments of a particular `Method`

Again, you really don’t want to do this. This looks like an [XY problem](https://en.wikipedia.org/wiki/XY_problem), applying the Matlab solution to Julia, where there are more idiomatic approaches, eg

1. for a vararg like `f(args...)`, you just want `length(args)`,

2. for optional arguments, just do that, like @Henrique_Becker shows above,

3. for the Matlab-style `switch nargin`, you want to define separate methods,

4. Matlab’s `nargin(f)` really does not make sense in Julia because of multiple dispatch,

5. in the remaining 0.1% cases if you are writing code introspection tools, you know what you are doing and need the internals.

---

<div class="post-metadata">

### Author: ![JeffFessler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jefffessler/32/6650_2.png) [@JeffFessler](https://discourse.julialang.org/u/JeffFessler)
#### Post date: [October 2, 2020, 5:20pm UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/19 "2020-10-02T17:20:23Z")

</div>

> [@Tamas\_Papp](#):
>
> you know what you are doing

Somehow one has to reach that point where “you know”, and searching forums like this can be helpful. The manual itself is very brief about what `methods` can do…  
[https://docs.julialang.org/en/v1/base/base/#Base.methods](https://docs.julialang.org/en/v1/base/base/#Base.methods)

---

<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: [October 3, 2020, 12:35pm UTC](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682/20 "2020-10-03T12:35:06Z")

</div>

I think you misunderstand the context: most users _never_ need to learn about internals and introspection of the methods table, so they don’t have to reach that point. In fact, using `methods` in regular code is an anti-pattern; it’s for interactive exploration.

If one really, really needs to know these details, they are mostly in the devdocs. Putting them into the manual _per se_ would be misleading.

[Next page](https://discourse.julialang.org/t/how-to-get-the-variable-numbers-for-a-julia-function/36682.md?page=2)
