# Get the argument names of an function

**URL:** <https://discourse.julialang.org/t/get-the-argument-names-of-an-function/32902>\
**Category:** New to Julia\
**Created:** [January 3, 2020, 1:04am UTC](https://discourse.julialang.org/t/get-the-argument-names-of-an-function/32902 "2020-01-03T01:04:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elmer\_Cusipuma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmer_cusipuma/32/11958_2.png) [@Elmer\_Cusipuma](https://discourse.julialang.org/u/Elmer_Cusipuma)\
**Post date:** [January 3, 2020, 1:04am UTC](https://discourse.julialang.org/t/get-the-argument-names-of-an-function/32902/1 "2020-01-03T01:04:33Z")

</div>

For an arbitrary function

```julia
function f(x,y,z=3)
    p = x+y*z
    return p 
end

```

I want to be able take the argument names of `f`

```julia
julia> argument_names(f)
3-element Array{Symbol,1}:
 :x
 :y
 :z

```

Is this possible?

---

<div class="post-metadata">

**Author:** ![findmyway](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/findmyway/32/4946_2.png) [@findmyway](https://discourse.julialang.org/u/findmyway)\
**Post date:** [January 3, 2020, 2:54am UTC](https://discourse.julialang.org/t/get-the-argument-names-of-an-function/32902/2 "2020-01-03T02:54:37Z")

</div>

```julia
julia> collect(methods(f))
[1] f(x, y) in Main at REPL[1]:2
[2] f(x, y, z) in Main at REPL[1]:2 

julia> x = collect(methods(f))[2]
f(x, y, z) in Main at REPL[1]:2

julia> x.slot_syms
"#self#\0x\0y\0z\0p\0"

```

This is the closest I can get.

---

<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:** [January 3, 2020, 9:25am UTC](https://discourse.julialang.org/t/get-the-argument-names-of-an-function/32902/3 "2020-01-03T09:25:06Z")

</div>

A hint is that Julia itself can print the arguments. So step into `show` with the debugger:

```julia
julia> using Debugger

julia> function f(x,y::Integer,z=3) # just for fun, add a type parameter
           p = x+y*z
           return p 
       end
f (generic function with 4 methods)

julia> m = first(methods(f))
f(x, y::Integer) in Main at REPL[5]:2

julia> @enter show(m)
In show(x) at show.jl:325
>325 show(x) = show(stdout::IO, x)

About to run: (typeassert)(Base.TTY(RawFD(0x0000000d) open, 0 bytes waiting), IO)
1|debug> se
In show(x) at show.jl:325
>325 show(x) = show(stdout::IO, x)

About to run: (show)(Base.TTY(RawFD(0x0000000d) open, 0 bytes waiting), f(x, y::Integer) in Main at REPL[5]:2)
1|debug> s
In #show#393(kwtype, , io, m) at methodshow.jl:159
 158 function show(io::IO, m::Method; kwtype::Union{DataType, Nothing}=nothing)
>159 tv, decls, file, line = arg_decl_parts(m)
 160 sig = unwrap_unionall(m.sig)
 161 ft0 = sig.parameters[1]
 162 ft = unwrap_unionall(ft0)
 163 d1 = decls[1]

About to run: Core.NewvarNode(:(_15))
1|debug> n
In #show#393(kwtype, , io, m) at methodshow.jl:159
 158 function show(io::IO, m::Method; kwtype::Union{DataType, Nothing}=nothing)
 159 tv, decls, file, line = arg_decl_parts(m)
>160 sig = unwrap_unionall(m.sig)
 161 ft0 = sig.parameters[1]
 162 ft = unwrap_unionall(ft0)
 163 d1 = decls[1]
 164 if sig === Tuple

About to run: (getproperty)(f(x, y::Integer) in Main at REPL[5]:2, :sig)
1|julia> decls
3-element Array{Tuple{String,String},1}:
 ("", "typeof(f)")
 ("x", "")        
 ("y", "Integer") 

```

---

<div class="post-metadata">

**Author:** ![zwwi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zwwi/32/11932_2.png) [@zwwi](https://discourse.julialang.org/u/zwwi)\
**Post date:** [March 8, 2020, 6:07am UTC](https://discourse.julialang.org/t/get-the-argument-names-of-an-function/32902/4 "2020-03-08T06:07:59Z")

</div>

using the following code from [https://github.com/JuliaLang/julia/blob/master/base/methodshow.jl](https://github.com/JuliaLang/julia/blob/master/base/methodshow.jl) (`Vector{Any}` changed to `Vector{Symbol}`)

```julia
function method_argnames(m::Method)
    argnames = ccall(:jl_uncompress_argnames, Vector{Symbol}, (Any,), m.slot_syms)
    isempty(argnames) && return argnames
    return argnames[1:m.nargs]
end

```

and then

```julia
ms = collect(methods(f))
method_argnames(last(ms))[2:end]

```

you’ll get

```julia
3-element Array{Symbol,1}:
 :x
 :y
 :z

```

---

<div class="post-metadata">

**Author:** ![f.ij](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@f.ij](https://discourse.julialang.org/u/f.ij)\
**Post date:** [June 27, 2022, 1:17pm UTC](https://discourse.julialang.org/t/get-the-argument-names-of-an-function/32902/5 "2022-06-27T13:17:56Z")

</div>

This solution doesn’t seem to work when using keyword arguments. Any suggestions?

---

<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:** [June 28, 2022, 10:58am UTC](https://discourse.julialang.org/t/get-the-argument-names-of-an-function/32902/6 "2022-06-28T10:58:15Z")

</div>

The procedure in [Get the argument names of an function - #3 by tim.holy](https://discourse.julialang.org/t/get-the-argument-names-of-an-function/32902/3) still works; just stepping through to see how Julia does something is a useful skill for many questions. You’ll discover `Base.kwarg_decl` that way.

```julia
julia> m = @which sum(rand(3))
sum(a::AbstractArray; dims, kw...) in Base at reducedim.jl:994

julia> Base.kwarg_decl(m)
2-element Vector{Symbol}:
 :dims
 Symbol("kw...")

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 26, 2022, 10:15am UTC](https://discourse.julialang.org/t/get-the-argument-names-of-an-function/32902/7 "2022-12-26T10:15:18Z")

</div>

Suppose I have a function

`f(p1, p2=default)`

which internally creates and saves a file whose content also depends on the value of p2.

How can I dynamically name the saved file according to the value of p2?

As an example, I defined  
`default1=10` and I call `f(50, default1)`,  
or  
`default1(x)=5*x` and call `f(50, default1(2))`

how do i save the file with the name “something\_default1.ext”?

I’m curious to know if and how such a thing can be done anyway, although it would be helpful to know a different way around the problem.
