# How to print arrays inside functions?

**URL:** <https://discourse.julialang.org/t/how-to-print-arrays-inside-functions/4091>\
**Category:** General Usage\
**Created:** [June 5, 2017, 2:01pm UTC](https://discourse.julialang.org/t/how-to-print-arrays-inside-functions/4091 "2017-06-05T14:01:18Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthieu](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@matthieu](https://discourse.julialang.org/u/matthieu)\
**Post date:** [June 5, 2017, 2:01pm UTC](https://discourse.julialang.org/t/how-to-print-arrays-inside-functions/4091/1 "2017-06-05T14:01:19Z")

</div>

It is useful to peak at an array when debugging a function. However, the macro `@show` prints arrays like vectors. How to print arrays in the correct REPL way?

```julia
zeros(5, 5)
#>5×5 Array{Float64,2}:
#> 0.0 0.0 0.0 0.0 0.0
#> 0.0 0.0 0.0 0.0 0.0
#> 0.0 0.0 0.0 0.0 0.0
#> 0.0 0.0 0.0 0.0 0.0
#> 0.0 0.0 0.0 0.0 0.0
function f(x)
   @show x
   return nothing
end
f(zeros(5, 5))
#> x = [0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0]

```

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [June 5, 2017, 2:07pm UTC](https://discourse.julialang.org/t/how-to-print-arrays-inside-functions/4091/2 "2017-06-05T14:07:14Z")

</div>

I also was looking for it at some point, it’s the [`Base.showarray`](https://github.com/JuliaLang/julia/blob/75541d2008dd95d5e7a6e3201f0995b56588dc99/base/show.jl#L1699) function.

```julia
julia> Base.showarray(STDOUT,rand(3,3),false)
3×3 Array{Float64,2}:
 0.935581 0.591996 0.532917
 0.130619 0.385825 0.580087
 0.486295 0.323777 0.920276

```

---

<div class="post-metadata">

**Author:** ![matthieu](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@matthieu](https://discourse.julialang.org/u/matthieu)\
**Post date:** [June 5, 2017, 3:07pm UTC](https://discourse.julialang.org/t/how-to-print-arrays-inside-functions/4091/3 "2017-06-05T15:07:19Z")

</div>

Thanks! Do you know why it’s not the default — i.e. why we don’t have

```julia
show(io::IO, X::AbstractArray) = showarray(io, X, false)

```

instead of [this line](https://github.com/JuliaLang/julia/blob/75541d2008dd95d5e7a6e3201f0995b56588dc99/base/show.jl#L1694)?

---

<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:** [June 5, 2017, 7:05pm UTC](https://discourse.julialang.org/t/how-to-print-arrays-inside-functions/4091/4 "2017-06-05T19:05:00Z")

</div>

When a type supports both single-line output (for things like `println`) and a more-verbose multi-line format, then the former is via `show(io, x)` and the latter is via `show(io, "text/plain", x)` … see: [https://docs.julialang.org/en/latest/manual/types/#Custom-pretty-printing-1](https://docs.julialang.org/en/latest/manual/types/#Custom-pretty-printing-1)

So, in this case you can do:

```julia
julia> show(STDOUT, "text/plain", rand(3,3))
3×3 Array{Float64,2}:
 0.559556 0.520583 0.64307 
 0.300074 0.833891 0.478954
 0.266339 0.558122 0.912018

```

instead of using the undocumented internal `Base.showarray` function.

Note that `show(io, "text/plain", x)` is a shortcut for `show(io, MIME("text/plain"), x)`.

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [June 6, 2017, 12:27am UTC](https://discourse.julialang.org/t/how-to-print-arrays-inside-functions/4091/5 "2017-06-06T00:27:42Z")

</div>

I always get confused by all the printing functions, but in the end I do `display(A)` in my tests, is this not the way to go?

---

<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:** [June 6, 2017, 1:26am UTC](https://discourse.julialang.org/t/how-to-print-arrays-inside-functions/4091/6 "2017-06-06T01:26:02Z")

</div>

There’s nothing wrong with calling the `display` function in your program. But you shouldn’t call it in `show(io, x)`, because `show` is different:

- `show` has to write specifically to `io`, whereas `display` outputs to whatever display handler is registered.

- `show(io, x)` should always output in text/plain format, whereas the `display` backend may choose a richer format to display if available.

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [June 6, 2017, 2:41am UTC](https://discourse.julialang.org/t/how-to-print-arrays-inside-functions/4091/7 "2017-06-06T02:41:34Z")

</div>

OK. It’s worth mentioning that, when I search for “julia print arrays” on google, a ton of suggestions come up (`show, println, print, show, showall, showcompact` and `@show`), none of which do what I want (pretty-printing an array as the REPL does).

Coupled with the lack of a debugger, this makes for a very frustrating experience for a casual user that wants to port their script from matlab and wants to print an array inside a loop to check it’s correct. So, if someone finds this topic by searching on google for “julia print arrays”, here’s the answer you’re looking for: `display`.

---

<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:** [June 7, 2017, 1:22am UTC](https://discourse.julialang.org/t/how-to-print-arrays-inside-functions/4091/8 "2017-06-07T01:22:46Z")

</div>

If you look in the manual for “pretty printing,” you’ll find the section explaining the difference between `show(io, x)` (terse one-line display, used e.g. in `print`) and `show(io, "text/plain", x)` (multiline verbose pretty printing, used e.g. in REPL output and `display`).

The three-argument `show` may be preferable to `display` if you specifically want text output (`display` can use e.g. graphical output for some types if the UI supports it, though you can also supply a MIME type to `display`) or if you want to output to a specific file/stream/buffer (not `STDOUT`).

---

<div class="post-metadata">

**Author:** ![Alex1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex1/32/11771_2.png) [@Alex1](https://discourse.julialang.org/u/Alex1)\
**Post date:** [February 27, 2020, 10:41pm UTC](https://discourse.julialang.org/t/how-to-print-arrays-inside-functions/4091/9 "2020-02-27T22:41:08Z")

</div>

The default behaviour of printing array to one line is confusing and hard to use. Would be better to switch the default behaviour.

```julia
print(zeros(4, 4))
[0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0]

```

Very inconvenient if trying to print from the script.jl without notebook or REPL

---

<div class="post-metadata">

**Author:** ![jbrea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbrea/32/3879_2.png) [@jbrea](https://discourse.julialang.org/u/jbrea)\
**Post date:** [February 28, 2020, 8:08am UTC](https://discourse.julialang.org/t/how-to-print-arrays-inside-functions/4091/10 "2020-02-28T08:08:34Z")

</div>

Have you tried `display(zeros(4, 4))` instead of `print`?

---

<div class="post-metadata">

**Author:** ![Alex1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex1/32/11771_2.png) [@Alex1](https://discourse.julialang.org/u/Alex1)\
**Post date:** [February 29, 2020, 5:58am UTC](https://discourse.julialang.org/t/how-to-print-arrays-inside-functions/4091/11 "2020-02-29T05:58:41Z")

</div>

Thanks @jbrea I found a way to print it in a nice way. The problem is that it is not simple and intuitive. Why forcing people to know `print`, `display`, `show` - and with different arguments like `io` or `stringbuffer` (if you want show to print to string and not to `stdout`).

---

<div class="post-metadata">

**Author:** ![Alex1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex1/32/11771_2.png) [@Alex1](https://discourse.julialang.org/u/Alex1)\
**Post date:** [February 29, 2020, 5:59am UTC](https://discourse.julialang.org/t/how-to-print-arrays-inside-functions/4091/12 "2020-02-29T05:59:58Z")

</div>

I wouldn’t mind if those extra stuff were supposed to be used only by super-users or library developers. But that’s not the case. You need to know it in the simplest case - just to be able to print things nicely.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 29, 2020, 6:05am UTC](https://discourse.julialang.org/t/how-to-print-arrays-inside-functions/4091/13 "2020-02-29T06:05:34Z")

</div>

> [@Alex1](#):
>
> Why forcing people to know `print` , `display` , `show`

Different contexts and use cases require different interfaces. It is neither possible nor desirable for the language to guess what the user wants.

Incidentally, please don’t resurrect old topics (the Discourse interface probably warned you about this).
