# Why does a function wait for a loop to finish before printing something inside the loop?

**URL:** <https://discourse.julialang.org/t/why-does-a-function-wait-for-a-loop-to-finish-before-printing-something-inside-the-loop/36881>\
**Category:** New to Julia\
**Created:** [April 2, 2020, 12:02am UTC](https://discourse.julialang.org/t/why-does-a-function-wait-for-a-loop-to-finish-before-printing-something-inside-the-loop/36881 "2020-04-02T00:02:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpkzz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wpkzz/32/9892_2.png) [@wpkzz](https://discourse.julialang.org/u/wpkzz)\
**Post date:** [April 2, 2020, 12:02am UTC](https://discourse.julialang.org/t/why-does-a-function-wait-for-a-loop-to-finish-before-printing-something-inside-the-loop/36881/1 "2020-04-02T00:02:17Z")

</div>

I have been using Julia since 0.4 and this is the first time that something like that happens. I have a function with a loop inside it and a print inside the loop. Usually I use this loop to know which parameters make the slower part of the calculation, but now, I do not get any output before the function finishes. This is with Julia 1.4. The function would be something like this:

> function f(x)  
> aux=0  
> rr=0.001:0.001:x  
> for r in rr  
> print(r, " ")  
> aux+=somecomplicatedfun(r)  
> end  
> return aux  
> end

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [April 2, 2020, 12:31am UTC](https://discourse.julialang.org/t/why-does-a-function-wait-for-a-loop-to-finish-before-printing-something-inside-the-loop/36881/2 "2020-04-02T00:31:24Z")

</div>

Probably because `print` is buffered. Try putting a `flush(STDOUT)` after the print.

---

<div class="post-metadata">

**Author:** ![wpkzz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wpkzz/32/9892_2.png) [@wpkzz](https://discourse.julialang.org/u/wpkzz)\
**Post date:** [April 2, 2020, 1:32am UTC](https://discourse.julialang.org/t/why-does-a-function-wait-for-a-loop-to-finish-before-printing-something-inside-the-loop/36881/3 "2020-04-02T01:32:12Z")

</div>

Okey, then my question becomes, why is

> print

buffered now? In the other Julias this was not the case.

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [April 2, 2020, 9:17am UTC](https://discourse.julialang.org/t/why-does-a-function-wait-for-a-loop-to-finish-before-printing-something-inside-the-loop/36881/4 "2020-04-02T09:17:19Z")

</div>

Printing was always and is asynchronous in julia. It may be caused by many things that, in new julia version, `somecomplicatedfun®` can run without task switch.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [April 2, 2020, 9:42am UTC](https://discourse.julialang.org/t/why-does-a-function-wait-for-a-loop-to-finish-before-printing-something-inside-the-loop/36881/5 "2020-04-02T09:42:29Z")

</div>

Crossref to answer on Stackoverflow to avoid double work in answering: [Julia waits for function to finish before printing message in a loop - Stack Overflow](https://stackoverflow.com/questions/60982177/julia-waits-for-function-to-finish-before-printing-message-in-a-loop)

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [April 2, 2020, 10:37am UTC](https://discourse.julialang.org/t/why-does-a-function-wait-for-a-loop-to-finish-before-printing-something-inside-the-loop/36881/6 "2020-04-02T10:37:05Z")

</div>

It would be nice if `print` / `println` had a keyword argument option to automatically flush, like for instance in Python

```julia
print(..., flush=True)

```

---

<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:** [April 2, 2020, 11:23am UTC](https://discourse.julialang.org/t/why-does-a-function-wait-for-a-loop-to-finish-before-printing-something-inside-the-loop/36881/7 "2020-04-02T11:23:46Z")

</div>

I can also imagine a token that would flush, eg

```julia
struct Flush end
Base.print(io::IO, ::Flush) = flush(io)
const FLUSH = Flush()
print(..., FLUSH, other_stuff..., FLUSH)

```
