# How do I see what's inside a function?

**URL:** <https://discourse.julialang.org/t/how-do-i-see-whats-inside-a-function/88599>\
**Category:** New to Julia\
**Created:** [October 11, 2022, 10:26pm UTC](https://discourse.julialang.org/t/how-do-i-see-whats-inside-a-function/88599 "2022-10-11T22:26:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)\
**Post date:** [October 11, 2022, 10:26pm UTC](https://discourse.julialang.org/t/how-do-i-see-whats-inside-a-function/88599/1 "2022-10-11T22:26:48Z")

</div>

I know I can define a function like

```julia
foo(a,b) = a + b

```

Now suppose after defining `foo` I go eat lunch, and when I come back to my computer I’ve already forgotten what `foo` does. How do I look inside `foo` to see the `a + b` code?

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [October 11, 2022, 10:53pm UTC](https://discourse.julialang.org/t/how-do-i-see-whats-inside-a-function/88599/2 "2022-10-11T22:53:55Z")

</div>

Is `foo` defined in a file? If so, you could do

```julia
julia> @edit foo(1, 2)

```

at the REPL to open the source file for the corresponding method. If there are multiple methods, you could run `methods(foo)` and it will tell you where the different methods are defined. If `foo` is defined at the REPL, then scrolling/searching the command history is probably the way to go.

---

<div class="post-metadata">

**Author:** ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)\
**Post date:** [October 12, 2022, 12:39am UTC](https://discourse.julialang.org/t/how-do-i-see-whats-inside-a-function/88599/3 "2022-10-12T00:39:17Z")

</div>

@StevenWhitaker Thanks for the quick and thoughtful reply!

This question came about because I defined `foo` in the REPL, and then I wanted to tinker with it. It seems crazy to me that Julia knows what expression is inside `foo`, but she won’t tell me what it is. Would be cool to be able to access it directly in the REPL and/or in the workspace in VS Code.

![Screen Shot 2022-10-11 at 5.32.04 PM](https://global.discourse-cdn.com/julialang/original/3X/b/e/becebfc849aad787932fa7dca3fda563fe3c30a3.png)

---

<div class="post-metadata">

**Author:** ![Jollywatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jollywatt/32/202198_2.png) [@Jollywatt](https://discourse.julialang.org/u/Jollywatt)\
**Post date:** [October 12, 2022, 12:52am UTC](https://discourse.julialang.org/t/how-do-i-see-whats-inside-a-function/88599/4 "2022-10-12T00:52:16Z")

</div>

> [@chadagreene](#):
>
> It seems crazy to me that Julia knows what expression is inside `foo`, but she won’t tell me what it is.

I don’t actually think Julia does know what the original expression is — only the parsed (or “lowered”) version, which can be retrieved as follows:

```julia
julia> f(a, b) = a + b
f (generic function with 1 method)

julia> @code_lowered f(1, 2)
CodeInfo(
1 ─ %1 = a + b
└── return %1
)

```

See also `Base.uncompressed_ast(::Method)` as in [here](https://discourse.julialang.org/t/source-code-of-a-function/4499/4).

I might be mistaken, and this might change in the future if [JuliaSyntax,jl](https://github.com/JuliaLang/JuliaSyntax.jl) replaces the current parser, but otherwise the way to go is to put functions you want to use for longer than one sitting in a file and use `@edit` and `@which` to jump to them from the REPL.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 12, 2022, 2:14am UTC](https://discourse.julialang.org/t/how-do-i-see-whats-inside-a-function/88599/5 "2022-10-12T02:14:05Z")

</div>

> <https://github.com/KristofferC/OhMyREPL.jl/pull/253>
>
> !\[image\](https://user-images.githubusercontent.com/5306213/145093130-7c06a4bb-12…21-48b9-af53-3eb435ce63ec.png)

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [October 12, 2022, 2:54am UTC](https://discourse.julialang.org/t/how-do-i-see-whats-inside-a-function/88599/6 "2022-10-12T02:54:45Z")

</div>

Another workaround is to use Debugger.jl:

```julia
julia> foo(a, b) = a + b

julia> using Debugger

julia> @enter foo(1, 2)
In foo(a, b) at REPL[2]:1
>1 foo(a, b) = a + b

About to run: (+)(1, 2)
1|debug> 

```

Here (and in most cases), the debugger _is_ able to show the original expression, so I agree that it seems like Julia should be able to return that expression somehow.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [October 12, 2022, 8:20am UTC](https://discourse.julialang.org/t/how-do-i-see-whats-inside-a-function/88599/7 "2022-10-12T08:20:26Z")

</div>

> [@chadagreene](#):
>
> I defined `foo` in the REPL, and then I wanted to tinker with it. It seems crazy to me that Julia knows what expression is inside `foo`, but she won’t tell me what it is. Would be cool to be able to access it directly in the REPL

It sounds you want to bring back the function definition at the REPL prompt and be able to edit it. If so, `Ctrl-R` will serve you well for this. `Ctrl-R` followed by `foo(` will search in your REPL history for lines that contain `foo(` and display the latest one it finds. You can press Ctrl-R again to repeat the search to find an occurrence before that. Once you find the method definition you want, just pressing any arrow key will bring you out of the search mode and let you edit the definition. (If you use OhMyREPL.jl, you get a nice menu when you press Ctrl-R, which makes this process even easier and more intuitive.)

If you do want the function definition as an _output_, [CodeTracking.jl](https://github.com/timholy/CodeTracking.jl) has a `@code_string` macro for that.

```julia
julia> using CodeTracking

julia> foo(a,b) = a + b
foo (generic function with 1 method)

julia> @code_string foo(1, 2)
"foo(a,b) = a + b"

```

It’s meant to be a lightweight subset of Revise.jl functionality, so should be fine to load in your startup.jl if you need this often.

---

<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 12, 2022, 8:53am UTC](https://discourse.julialang.org/t/how-do-i-see-whats-inside-a-function/88599/8 "2022-10-12T08:53:36Z")

</div>

Maybe I made some customization at some point, but I just type:

```julia
julia> foo<arrowup>

```

and I get to cycle through all entered commands starting with `foo`.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 12, 2022, 9:32am UTC](https://discourse.julialang.org/t/how-do-i-see-whats-inside-a-function/88599/9 "2022-10-12T09:32:13Z")

</div>

That shold work without any customization.

On the main topic, this is what I see in VSCode:

![image](https://global.discourse-cdn.com/julialang/original/3X/3/c/3ce475ae7ba07c978a548b39ab0591dca3909482.png)

---

<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:** [October 12, 2022, 2:27pm UTC](https://discourse.julialang.org/t/how-do-i-see-whats-inside-a-function/88599/10 "2022-10-12T14:27:51Z")

</div>

See also this longstanding feature request: [show function source code from repl · Issue #2625 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/2625)
