# Expected behaviour of println.(Array) (or @. println(Array) )

**URL:** <https://discourse.julialang.org/t/expected-behaviour-of-println-array-or-println-array/39846>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 20, 2020, 5:50pm UTC](https://discourse.julialang.org/t/expected-behaviour-of-println-array-or-println-array/39846 "2020-05-20T17:50:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeLenz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/codelenz/32/3419_2.png) [@CodeLenz](https://discourse.julialang.org/u/CodeLenz)\
**Post date:** [May 20, 2020, 5:50pm UTC](https://discourse.julialang.org/t/expected-behaviour-of-println-array-or-println-array/39846/1 "2020-05-20T17:50:47Z")

</div>

Hi.

Just to clarify a silly doubt. What is the expected behavior of

> println.(x)

where x is an Array? I was expecting that println would be executed to each position of x, but

julia\> x = rand(5)  
5-element Array{Float64,1}:  
0.08912877022448185  
0.8256610270367024  
0.5274878309082551  
0.9455607916948552  
0.7686464634951251

julia\> println.(x)  
0.08912877022448185  
0.8256610270367024  
0.5274878309082551  
0.9455607916948552  
0.7686464634951251  
5-element Array{Nothing,1}:  
nothing  
nothing  
nothing  
nothing  
nothing

I would like to understand the meaning of those Nothings.

Using Julia1.4.1

Thank you very much.

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [May 20, 2020, 6:28pm UTC](https://discourse.julialang.org/t/expected-behaviour-of-println-array-or-println-array/39846/2 "2020-05-20T18:28:56Z")

</div>

Well, it is simple:

When you are using a REPL the returned value is shown (except when it is nothing). println return none, and because you have running println for each value (with println.), you are obtaining an array of nothing. At different of nothing, that it is not shown in the REPL, an array of any type is always shown. This is the reason.

if you do not want the nothing, you can run

```julia
println.(x);

```

Anyway, it is in the REPL, running the code directly would not show anything different than the values of x.

I hope it is more clear now.

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [May 20, 2020, 6:35pm UTC](https://discourse.julialang.org/t/expected-behaviour-of-println-array-or-println-array/39846/3 "2020-05-20T18:35:53Z")

</div>

@CodeLenz One last comment:

the dot operator (as println.) is usually used when you want the return obtained.  
If you only want to run the function for each value, you can use:

```julia
foreach(println, x)

```

foreach, at different of the vectorized, does not return anything.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 20, 2020, 6:54pm UTC](https://discourse.julialang.org/t/expected-behaviour-of-println-array-or-println-array/39846/4 "2020-05-20T18:54:06Z")

</div>

Calling `f.(x)` returns an array (or other container) holding the value of `f(x_i)` for each `x_i` in `x`:

```julia
julia> f(x) = 2 * x
f (generic function with 1 method)

julia> f.([1, 2, 3])
3-element Array{Int64,1}:
 2
 4
 6

```

If `f(x)` returns `nothing`, then you will get a collection of `nothing`s:

```julia
julia> g.([1, 2, 3])
3-element Array{Nothing,1}:
 nothing
 nothing
 nothing

```

`println(x)` returns `nothing`:

```julia
julia> result = println(1.0)
1.0

julia> result === nothing
true

```

so `println.(x)` returns a vector of `nothing`s, which then get automatically printed because the REPL always prints out the return value of the last expression.

```julia
julia> println.([1, 2, 3])
1
2
3
3-element Array{Nothing,1}:
 nothing
 nothing
 nothing

```

Edit: @dmolina beat me to it!

---

<div class="post-metadata">

**Author:** ![CodeLenz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/codelenz/32/3419_2.png) [@CodeLenz](https://discourse.julialang.org/u/CodeLenz)\
**Post date:** [May 20, 2020, 7:03pm UTC](https://discourse.julialang.org/t/expected-behaviour-of-println-array-or-println-array/39846/5 "2020-05-20T19:03:35Z")

</div>

I see!

I was missing the fact that println returns something (in this case, Nothing 🙂 ), since I was just looking at the output in the REPL.

Silly me, silly me.

Thanks a lot.
