# Function output confusion

**URL:** https://discourse.julialang.org/t/function-output-confusion/89589
**Category:** New to Julia
**Created:** [November 1, 2022, 2:38am UTC](https://discourse.julialang.org/t/function-output-confusion/89589 "2022-11-01T02:38:40Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)
#### Post date: [November 1, 2022, 2:38am UTC](https://discourse.julialang.org/t/function-output-confusion/89589/1 "2022-11-01T02:38:40Z")

</div>

I have a simple function that changes the first element of any matrix to the number 99. It looks like this:

```julia
function foo(a)
    a[1] = 99
end

```

Now when I apply the function to a matrix, here’s what I get:

```julia
x = [1 2 3 4 5]; 
y = foo(x); 

```

I’m confused, because applying `foo` to `x` has mutated the first element of `x` as expected, but the output `y` is just the number 99.

```julia
julia> x
1×5 Matrix{Int64}:
 99 2 3 4 5

julia> y
99

```

What exactly is the logic here? How is the function able to mutate `x`, then seemingly forget about the other elements in `x` before spitting out a scalar value `y`?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [November 1, 2022, 2:42am UTC](https://discourse.julialang.org/t/function-output-confusion/89589/2 "2022-11-01T02:42:35Z")

</div>

The expression `a=b` in Julia has a return value of `b`. Thus `a[1]=99` (and therefore `foo`) returns `99`.

---

<div class="post-metadata">

### Author: ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)
#### Post date: [November 1, 2022, 2:49am UTC](https://discourse.julialang.org/t/function-output-confusion/89589/3 "2022-11-01T02:49:18Z")

</div>

Ooooohhh, interesting. That’s pretty straightforward now that I understand it. Thanks for the tip!

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [November 1, 2022, 2:51am UTC](https://discourse.julialang.org/t/function-output-confusion/89589/4 "2022-11-01T02:51:20Z")

</div>

This is also the reason why you can do multiple assignments like `a = b = 1`.

---

<div class="post-metadata">

### Author: ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)
#### Post date: [November 1, 2022, 2:56am UTC](https://discourse.julialang.org/t/function-output-confusion/89589/5 "2022-11-01T02:56:40Z")

</div>

Dang, well ain’t that fancy.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [November 1, 2022, 3:00am UTC](https://discourse.julialang.org/t/function-output-confusion/89589/6 "2022-11-01T03:00:15Z")

</div>

For more consequences of this:

> **[Returned value of assignment in Julia](https://giordano.github.io/blog/2017-12-02-julia-assignment/)**
>
> Today I realized by chance that in the Julia programming language, like in C/C++, assignment returns the assigned value. To be more precise, assignment returns the right-hand side of the assignment expression. This doesn’t appear to be documented in...

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [November 1, 2022, 7:10am UTC](https://discourse.julialang.org/t/function-output-confusion/89589/7 "2022-11-01T07:10:30Z")

</div>

To get the function you want:

```julia
function foo!(a) # there's a convention to mark mutating functions with !
    a[1] = 99
    return a
end
    

```

---

<div class="post-metadata">

### Author: ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)
#### Post date: [November 1, 2022, 1:55pm UTC](https://discourse.julialang.org/t/function-output-confusion/89589/8 "2022-11-01T13:55:59Z")

</div>

Yes! Thanks; these concepts are slowly beginning to click for me…slowly.

---

<div class="post-metadata">

### Author: ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)
#### Post date: [November 1, 2022, 2:21pm UTC](https://discourse.julialang.org/t/function-output-confusion/89589/9 "2022-11-01T14:21:32Z")

</div>

it also means you can have snippets like this

```julia
 a = if rand() > 0.5 1 else 0 end

```

which assigns to `a` based on the condition

---

<div class="post-metadata">

### Author: ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)
#### Post date: [November 1, 2022, 2:56pm UTC](https://discourse.julialang.org/t/function-output-confusion/89589/10 "2022-11-01T14:56:27Z")

</div>

Slick!

---

<div class="post-metadata">

### Author: ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)
#### Post date: [November 1, 2022, 3:13pm UTC](https://discourse.julialang.org/t/function-output-confusion/89589/11 "2022-11-01T15:13:33Z")

</div>

and they could be multi line

```julia
a = if rand() > 0.5
    b = 5
    c = 7
    b + c
else
    x = rand()
    v = rand()
    x * v
end

```

which assigns `a` to be `b + c` or `x * v`

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [November 1, 2022, 5:09pm UTC](https://discourse.julialang.org/t/function-output-confusion/89589/12 "2022-11-01T17:09:53Z")

</div>

I would advise against this. It becomes super difficult to read. The oneliner is fine, but since there are no `return` statements in `if` blocks, it always takes a while to figure out what points are the final ones.

```julia
if x < 0
    a = 0
elseif x < 1
    a = 1
else
    a = 2
end

```

is a lot better than

```julia
a = if x < 0
        0
    elseif x < 1
        1
    else
        2
    end

```

. And the difference gets bigger the more complicated the statement is.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [November 1, 2022, 8:34pm UTC](https://discourse.julialang.org/t/function-output-confusion/89589/13 "2022-11-01T20:34:31Z")

</div>

> [@gustaphe](#):
>
> I would advise against this. It becomes super difficult to read.

Why? You’ve never used lisp, have you?

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [November 2, 2022, 6:53am UTC](https://discourse.julialang.org/t/function-output-confusion/89589/14 "2022-11-02T06:53:11Z")

</div>

No, I have not, and I’m increasingly convinced I don’t want to.

With functions it’s mostly obvious where the return value is, and if it’s not you can mark it with `return`. In an if clause you don’t have that option. Add to that the fact that the major action, the assignment, is all the way at the top, so if I’ve figured out what it returns I now need to scroll all the way back to remind myself where it’s stored. And _some but not all_ if clauses are assignments. Meanwhile, “if x is small, assign 0 to a” is natural.

I recently implemented a feature in JuliaFormatter, but I ironically had to spend most of the time just figuring out heads or tails of all the if-assignments in there. It’s fixed now though.

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [November 2, 2022, 7:46am UTC](https://discourse.julialang.org/t/function-output-confusion/89589/16 "2022-11-02T07:46:21Z")

</div>

> [@gustaphe](#):
>
> No, I have not, and I’m increasingly convinced I don’t want to.

What a pity 😉

> [@gustaphe](#):
>
> With functions it’s mostly obvious where the return value is, and if it’s not you can mark it with `return`. In an if clause you don’t have that option.

You can wrap the if clause in a `begin` block which then allows you to `return` from there.

> [@gustaphe](#):
>
> Add to that the fact that the major action, the assignment, is all the way at the top, so if I’ve figured out what it returns I now need to scroll all the way back to remind myself where it’s stored. And _some but not all_ if clauses are assignments. Meanwhile, “if x is small, assign 0 to a” is natural.

For short if-clauses an assignment inside can be nice. For larger ones – as in the example above – I find the assignment outside better as it immediately makes clear where the value of `a` comes from. In the other case, it could well be that every branch assigns a different variable!

```julia
if x < 0
    a = 0
elseif x < 1
    b = 1 # oops
else
    a = 2
end

```

In any case, if I need to scroll over the if-clause it is probably time to at least move it into its own function or better yet, break the logic into smaller pieces.
