# Get the name of a function in the argument

**URL:** <https://discourse.julialang.org/t/get-the-name-of-a-function-in-the-argument/40027>\
**Category:** New to Julia\
**Tags:** argument, function\
**Created:** [May 23, 2020, 3:53pm UTC](https://discourse.julialang.org/t/get-the-name-of-a-function-in-the-argument/40027 "2020-05-23T15:53:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![janrpeters](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@janrpeters](https://discourse.julialang.org/u/janrpeters)\
**Post date:** [May 23, 2020, 3:53pm UTC](https://discourse.julialang.org/t/get-the-name-of-a-function-in-the-argument/40027/1 "2020-05-23T15:53:03Z")

</div>

I would like to have a function that returns the name of the function in the argument, e.g.:

```julia
function give_me_my_name(f::Function)
    return ??? (f);
end

```

such that

`give_me_my_name(sin)`

returns

`sin`

as a string. How do I do that?

---

<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:** [May 23, 2020, 3:55pm UTC](https://discourse.julialang.org/t/get-the-name-of-a-function-in-the-argument/40027/2 "2020-05-23T15:55:23Z")

</div>

```julia
julia> function give_me_my_name(f::Function)
           return String(Symbol(f))
       end
give_me_my_name (generic function with 1 method)

julia> give_me_my_name(sin)
"sin"

```

---

<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:** [May 23, 2020, 3:56pm UTC](https://discourse.julialang.org/t/get-the-name-of-a-function-in-the-argument/40027/3 "2020-05-23T15:56:41Z")

</div>

```julia
julia> give_me_my_name(give_me_my_name)
"give_me_my_name"

```

😁

---

<div class="post-metadata">

**Author:** ![janrpeters](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@janrpeters](https://discourse.julialang.org/u/janrpeters)\
**Post date:** [May 23, 2020, 4:05pm UTC](https://discourse.julialang.org/t/get-the-name-of-a-function-in-the-argument/40027/4 "2020-05-23T16:05:41Z")

</div>

Awesome! Thanks!

The best about Julia is that brought child-like fun of programming back  
to me — but a close second is that fantastic helpful & fun  
Julia-Community!!! So cool!

---

<div class="post-metadata">

**Author:** ![janrpeters](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@janrpeters](https://discourse.julialang.org/u/janrpeters)\
**Post date:** [May 23, 2020, 4:18pm UTC](https://discourse.julialang.org/t/get-the-name-of-a-function-in-the-argument/40027/5 "2020-05-23T16:18:42Z")

</div>

How do you do this for a mutable struct?

String(Symbol(X))

does not yield the name…

---

<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:** [May 23, 2020, 4:30pm UTC](https://discourse.julialang.org/t/get-the-name-of-a-function-in-the-argument/40027/6 "2020-05-23T16:30:56Z")

</div>

Its just the same:

```julia
julia> mutable struct mutable_struct
       x
       end

julia> typeof(mutable_struct)
DataType

julia> function give_me_my_name(s::DataType)
           return String(Symbol(s))
           end
give_me_my_name (generic function with 2 methods)

julia> give_me_my_name(mutable_struct)
"mutable_struct"

```

---

<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:** [May 23, 2020, 4:32pm UTC](https://discourse.julialang.org/t/get-the-name-of-a-function-in-the-argument/40027/7 "2020-05-23T16:32:36Z")

</div>

You can do it generic:

```julia
julia> function give_me_my_name(whatever)
           return String(Symbol(whatever))
           end
give_me_my_name (generic function with 3 methods)

julia> give_me_my_name(Int)
"Int64"

```

---

<div class="post-metadata">

**Author:** ![janrpeters](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@janrpeters](https://discourse.julialang.org/u/janrpeters)\
**Post date:** [May 23, 2020, 4:45pm UTC](https://discourse.julialang.org/t/get-the-name-of-a-function-in-the-argument/40027/8 "2020-05-23T16:45:24Z")

</div>

Sorry, I explained myself badly: Variablenames  
are only accessible by Macros, right? For

```julia
mutable struct mutable_struct
     x
     y
     z
end

var = mutable_struct(1,2,3);

function give_me_my_name(s::mutable_struct)
     return String(Symbol(s))
end

give_me_my_name(var)

```

I get `"mutable_struct(1, 2, 3)"` and for the macro, I have

```julia
macro give_me_my_name(s)
     String(s)
end
@give_me_my_name(var)

```

Or is that also possible in a function?

---

<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:** [May 23, 2020, 8:28pm UTC](https://discourse.julialang.org/t/get-the-name-of-a-function-in-the-argument/40027/9 "2020-05-23T20:28:05Z")

</div>

No, I don’t think this is possible with a function. In the function it would be the function parameters name and not `var` anymore.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [July 26, 2020, 7:46pm UTC](https://discourse.julialang.org/t/get-the-name-of-a-function-in-the-argument/40027/10 "2020-07-26T19:46:38Z")

</div>

You may find interesting this macro that prints the name of the current function it is called from:

> [@How to print function name and source file/line number](https://discourse.julialang.org/t/how-to-print-function-name-and-source-file-line-number/43486):
>
> Hello, is it possible to print the current function name and the REPL/Juno/VSCode clickable file name/function name, e.g. function foo() @codeLocation ... end Resulting in: Running function foo on /path/to/myfile.jl:123 I am aware of \_\_source\_\_.file and \_\_source\_\_.line, but I coudn’t get them working in the way I described and the function @shown in [DebuggingUtilities](https://github.com/timholy/DebuggingUtilities.jl) seems to go a bit too far showing the whole stack. EDIT: I did manage to get the file name/ line but not yet the fu…

---

<div class="post-metadata">

**Author:** ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)\
**Post date:** [January 30, 2021, 5:17am UTC](https://discourse.julialang.org/t/get-the-name-of-a-function-in-the-argument/40027/11 "2021-01-30T05:17:39Z")

</div>

It seems no one mentioned the built-in method [`Base.nameof`](https://docs.julialang.org/en/v1/base/base/#Base.nameof-Tuple%7BFunction%7D).

```nohighlight
julia> function give_me_my_name(f::Function)
           return string(nameof(f))
       end
give_me_my_name (generic function with 1 method)

julia> give_me_my_name(sin)
"sin"

```

It also works for a data type.

```julia
julia> function give_me_my_name(s::DataType)
           return string(nameof(s))
       end
give_me_my_name (generic function with 1 method)

julia> give_me_my_name(IOStream)
"IOStream"

```

More interesting utilities can be found in the [Reflection](https://docs.julialang.org/en/v1/base/base/#Reflection) part of the documentation.

---

<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:** [September 9, 2021, 9:53am UTC](https://discourse.julialang.org/t/get-the-name-of-a-function-in-the-argument/40027/12 "2021-09-09T09:53:07Z")

</div>

(Sorry for necro-posting)

Another way I just discovered. It’s not necessary to go via `Symbol`:

```julia
jl> f = sin
sin (generic function with 15 methods)

jl> string(f)
"sin"

```

Direct string interpolation also works:

```julia
jl> "Hello, $(f)!"
"Hello, sin!

```

Both methods work for types as well.

Going via `nameof` is _much_ faster, though.
