How do I see what's inside a function?

I know I can define a function like

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?

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

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.

@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

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> f(a, b) = a + b
f (generic function with 1 method)

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

See also Base.uncompressed_ast(::Method) as in here.

I might be mistaken, and this might change in the future if 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.

1 Like

Another workaround is to use Debugger.jl:

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)

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.


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 has a @code_string macro for that.

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.

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

julia> foo<arrowup>

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

1 Like

That shold work without any customization.

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


See also this longstanding feature request: show function source code from repl · Issue #2625 · JuliaLang/julia · GitHub