# Redefine the show function

**URL:** <https://discourse.julialang.org/t/redefine-the-show-function/106993>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 1, 2023, 1:18pm UTC](https://discourse.julialang.org/t/redefine-the-show-function/106993 "2023-12-01T13:18:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [December 1, 2023, 1:18pm UTC](https://discourse.julialang.org/t/redefine-the-show-function/106993/1 "2023-12-01T13:18:41Z")

</div>

```julia
function Base.show(io::IO, b::BasisSetRep)
    print(io, "BasisSetRep($(b.h)) with dimension $(dimension(b)) and $(nnz(b.sm)) stored entries:")
    show(io, MIME"text/plain"(), b.sm)
end

```

This is some people’s code.

I am confused with the name of this function. As far as I know, ‘show’ is a built-in function of julia in the Base package, right? Then how can you redefine a show function? Why not a new function with a new name?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 1, 2023, 1:28pm UTC](https://discourse.julialang.org/t/redefine-the-show-function/106993/2 "2023-12-01T13:28:59Z")

</div>

Because you want to change how a custom structure is shown in the REPL, and that is what `Base.show` does:

```julia-repl
julia> struct A
           x::Int
       end

julia> function Base.show(io::IO, a::A)
           println("My A type has a field x with value: $(a.x)")
       end

julia> A(2)
My A type has a field x with value: 2

```

ps: This is not a redefinition of `Base.show`, is adding a new method for that function, for the new type `A`.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [December 1, 2023, 2:33pm UTC](https://discourse.julialang.org/t/redefine-the-show-function/106993/3 "2023-12-01T14:33:41Z")

</div>

See [Methods · The Julia Language](https://docs.julialang.org/en/v1/manual/methods/)

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [December 1, 2023, 3:28pm UTC](https://discourse.julialang.org/t/redefine-the-show-function/106993/4 "2023-12-01T15:28:45Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> As far as I know, ‘show’ is a built-in function of julia in the Base package, right? Then how can you redefine a show function? Why not a new function with a new name?

Adding onto @lmiq’s solid response, the `Base.show` function is what Julia uses under the hood to know how to pretty-print something on screen, often called in response to a REPL prompt. The first argument tells Julia where to send the stream of resultant characters, and the second argument is the thing you want to see printed.

Julia uses a multiple dispatch system where you can write multiple different methods for a function, each with different argument types, and the compiler will select the appropriate method based on the arguments being used. You can see [here in the Julia source](https://github.com/JuliaLang/julia/blob/aef528d32a6291ce82452c3a1ff1fa56ccf17641/base/show.jl) where the show function is defined that there are tons of different methods based on different second argument types.

The code you quoted at the top of this page just implements a `show` method for a new type `BasisSetRep` and adds it to the existing method table.
