# Must the return value of a function be printed on the last line of the REPL? (when called without a \`;\`)

**URL:** https://discourse.julialang.org/t/must-the-return-value-of-a-function-be-printed-on-the-last-line-of-the-repl-when-called-without-a/108867
**Category:** General Usage
**Tags:** question, repl
**Created:** [January 16, 2024, 12:54pm UTC](https://discourse.julialang.org/t/must-the-return-value-of-a-function-be-printed-on-the-last-line-of-the-repl-when-called-without-a/108867 "2024-01-16T12:54:25Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [January 16, 2024, 12:54pm UTC](https://discourse.julialang.org/t/must-the-return-value-of-a-function-be-printed-on-the-last-line-of-the-repl-when-called-without-a/108867/1 "2024-01-16T12:54:25Z")

</div>

```julia
function f(x, y)
    s = x + y
    println("Look! The sum is $s.")
    return s
end

```

When I call this function with `f(1, 2)` the REPL prints:

```julia
julia> f(1, 2)
Look! The sum is 3.
3

```

But I want the function to print the result first and then print the concluding remark, and at the same time I want to let the sum `s` be the return value of function `f`.

Is that possible for Julia?

---

<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: [January 16, 2024, 1:30pm UTC](https://discourse.julialang.org/t/must-the-return-value-of-a-function-be-printed-on-the-last-line-of-the-repl-when-called-without-a/108867/2 "2024-01-16T13:30:52Z")

</div>

> [@WuSiren](#):
>
> But I want the function to print the result first and then print the concluding remark, and at the same time I want to let the sum `s` be the return value of function `f`.

The function executes before the return value is available to display. So the only way to do what you want would be to capture the output, using e.g. the low-level [`redirect_stdout`](https://docs.julialang.org/en/v1/base/io-network/#Base.redirect_stdout) function or a higher-level package like IOCapture.jl, in order to save it to print later.

(Alternatively, of course, you can print whatever you want from the function itself, e.g. call `display(s)` before your `println` statement to mimic the REPL display, and then suppress additional output in the REPL by ending the line in a `;`.)

---

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [January 16, 2024, 1:42pm UTC](https://discourse.julialang.org/t/must-the-return-value-of-a-function-be-printed-on-the-last-line-of-the-repl-when-called-without-a/108867/3 "2024-01-16T13:42:46Z")

</div>

Thanks. I realized that the effect I am pursuing seems to have the awkward situation of self-reference, but it is indeed easy to achieve in other programming languages, such as matlab.

---

<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: [January 16, 2024, 2:03pm UTC](https://discourse.julialang.org/t/must-the-return-value-of-a-function-be-printed-on-the-last-line-of-the-repl-when-called-without-a/108867/4 "2024-01-16T14:03:09Z")

</div>

> [@WuSiren](#):
>
> it is indeed easy to achieve in other programming languages, such as matlab.

Can you give an example from another programming language of what you mean?

---

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [January 16, 2024, 2:58pm UTC](https://discourse.julialang.org/t/must-the-return-value-of-a-function-be-printed-on-the-last-line-of-the-repl-when-called-without-a/108867/5 "2024-01-16T14:58:37Z")

</div>

I’m sorry. It seems to be my fault. 😥

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [January 16, 2024, 3:32pm UTC](https://discourse.julialang.org/t/must-the-return-value-of-a-function-be-printed-on-the-last-line-of-the-repl-when-called-without-a/108867/6 "2024-01-16T15:32:12Z")

</div>

I’m not sure why it is your fault.

Do you actually need the return value?

If you return `nothing` then there is no display for thr return value,

```julia-repl
julia> function f2(x, y)
           s = x + y
           println("Look! The sum is $s.")
           return nothing
       end
f2 (generic function with 1 method)

julia> f2(1, 2)
Look! The sum is 3.

```

If you do need the return value, you can do this:

```julia-repl
julia> r = Ref(0)
Base.RefValue{Int64}(0)

julia> g(ret, ref::Ref) = (ref[] = ret; nothing)
g (generic function with 1 method)

julia> g(f(1,2), r)
Look! The sum is 3.

julia> r[]
3

```

If you actually wanted to display 3 first, you can print it yourself.

---

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [January 17, 2024, 3:51am UTC](https://discourse.julialang.org/t/must-the-return-value-of-a-function-be-printed-on-the-last-line-of-the-repl-when-called-without-a/108867/7 "2024-01-17T03:51:25Z")

</div>

Amazing! 😃 Thanks!

---

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [January 17, 2024, 4:04am UTC](https://discourse.julialang.org/t/must-the-return-value-of-a-function-be-printed-on-the-last-line-of-the-repl-when-called-without-a/108867/8 "2024-01-17T04:04:06Z")

</div>

I found what I really want to realize has been asked in another previous post by myself:

> [@How to let a function return some numerical values and a graph simutaneously?](https://discourse.julialang.org/t/how-to-let-a-function-return-some-numerical-values-and-a-graph-simutaneously/103101):
>
> For example: using UnicodePlots function func(n) r = randn(n) s = sum(r) hist = histogram(r, nbins=10, closed=:left) r, s, hist end If I call this function in the REPL, it returns: julia\> func(10) ([1.5369990699727945, -0.7353913882824294, -0.8568141947726255, 1.145149668937184, 0.3340140471550356, 1.2364301576354533, 1.8932925331172032, 0.05768487009000112, -0.2116877502737839, 0.4638080551886433], 4.863485068767476, ┌ ┐…

In general, I just want a function to output the calculation procedure and then give some conclusive statements (such as displaying the result as a graph), but also the user can easily obtain the result of the function (such as through the most conventional assignment statement).

In fact, I seem to have used some Julia functions (such as some functions for machine learning or optimization) that do have similar effects. 🤔
