# '\_' as an argument in function definition

**URL:** https://discourse.julialang.org/t/as-an-argument-in-function-definition/107708
**Category:** General Usage
**Tags:** question
**Created:** [December 16, 2023, 1:07pm UTC](https://discourse.julialang.org/t/as-an-argument-in-function-definition/107708 "2023-12-16T13:07:47Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)
#### Post date: [December 16, 2023, 1:07pm UTC](https://discourse.julialang.org/t/as-an-argument-in-function-definition/107708/1 "2023-12-16T13:07:47Z")

</div>

```julia
function myfun(x, i, val, _)
x[i]+ = val 
end 

```

What does the underscore ‘\_’ mean here?

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [December 16, 2023, 1:19pm UTC](https://discourse.julialang.org/t/as-an-argument-in-function-definition/107708/2 "2023-12-16T13:19:29Z")

</div>

I haven’t used it in this context but it usually means “I don’t need to give this argument a name because I’m not going to use it”. More common is

```julia
for _ in 1:10
    println("I don't care about the iteration variable here")
end

```

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [December 16, 2023, 5:05pm UTC](https://discourse.julialang.org/t/as-an-argument-in-function-definition/107708/3 "2023-12-16T17:05:28Z")

</div>

This is the right interpretation, but it is a bit more powerful than just saying you won’t use it: you _cannot_ use this variable, so the compiler can take this fact into account and optimize accordingly.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [December 16, 2023, 7:38pm UTC](https://discourse.julialang.org/t/as-an-argument-in-function-definition/107708/4 "2023-12-16T19:38:23Z")

</div>

Do you have a source (or good example) for this? I would think that the compiler can equally optimize if I’m not using a named function argument. Would be surprising to me if that was only the case for `_`.

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [December 16, 2023, 8:31pm UTC](https://discourse.julialang.org/t/as-an-argument-in-function-definition/107708/5 "2023-12-16T20:31:25Z")

</div>

Well no, I was assuming that from the behavior of the compiler on other similar issues. I think you might be able to show that on the right example, but you are right I have no proof.

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [December 16, 2023, 9:05pm UTC](https://discourse.julialang.org/t/as-an-argument-in-function-definition/107708/6 "2023-12-16T21:05:43Z")

</div>

See for example this thread:

> [@Usage rules for bare underscores](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858):
>
> I’ve seen a few examples where one can use a bare underscore in an expression, e.g., julia\> map(\_ -\> "hello", 1:4) 4-element Array{String,1}: "hello" "hello" "hello" "hello" julia\> ["hello" for \_ in 1:4] 4-element Array{String,1}: "hello" "hello" "hello" "hello" julia\> for \_ in 1:4 println("hello") end hello hello hello hello But of course the following doesn’t work: julia\> \_ = 2 2 julia\> \_ ERROR: all-underscore identifier used as rvalue However, I haven’t seen a…

The key terminology is that `_` cannot be used as an rvalue, which roughly means it cannot appear on the right hand-side of an assignment (or any other expression that is not an explicit assignment).

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [December 17, 2023, 8:24am UTC](https://discourse.julialang.org/t/as-an-argument-in-function-definition/107708/7 "2023-12-17T08:24:40Z")

</div>

Let’s be a bit more precise here. The statement I’m questioning is this:

> [@lrnv](#):
>
> it is a bit more powerful than just saying you won’t use it: you _cannot_ use this variable, so the compiler can take this fact into account and optimize accordingly.

This suggests - at least to me - that, compared to a regular (unused) function argument, `_` is somehow special in that it gives extra “you _cannot_ use it” guarantees to the compiler. And if this is what is meant here, I tend to disagree and would like to see an example showing this different optimization potential. To be clear, I would claim that the optimization for `_` (_cannot_ be used) and a regular unused function argument (_won’t_ be used) is just the same.

I don’t see how the thread you’ve linked gives any more insights into this comparison. The only “new” thing it points out is that you can’t use `_` in conjuction with keyword arguments (which is clearly a difference to a regular argument). However, this is a [bug](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/12) and also doesn’t say anything about optimization potential.

But perhaps I’m just misunderstanding the statement in the first place?

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [December 17, 2023, 9:50am UTC](https://discourse.julialang.org/t/as-an-argument-in-function-definition/107708/8 "2023-12-17T09:50:40Z")

</div>

I’m also skeptical that it would make a difference for optimization but it’s correct that it cannot be used and thus gives the reader of the code a guarantee that it won’t have any effect.

> [@abraemer](#):
>
> The key terminology is that `_` cannot be used as an rvalue

For information this terminology is on its way out and will be changed in Julia 1.11: [Call all-underscore identifiers write-only by GunnarFarneback · Pull Request #50830 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/50830)

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [December 17, 2023, 11:06am UTC](https://discourse.julialang.org/t/as-an-argument-in-function-definition/107708/9 "2023-12-17T11:06:48Z")

</div>

> [@carstenbauer](#):
>
> This suggests - at least to me - that, compared to a regular (unused) function argument, `_` is somehow special in that it gives extra “you _cannot_ use it” guarantees to the compiler. And if this is what is meant here, I tend to disagree and would like to see an example showing this different optimization potential. To be clear, I would claim that the optimization for `_` (_cannot_ be used) and a regular unused function argument (_won’t_ be used) is just the same.
> 
> I don’t see how the thread you’ve linked gives any more insights into this comparison. The only “new” thing it points out is that you can’t use `_` in conjuction with keyword arguments (which is clearly a difference to a regular argument). However, this is a [bug](https://discourse.julialang.org/t/usage-rules-for-bare-underscores/28858/12) and also doesn’t say anything about optimization potential.
> 
> But perhaps I’m just misunderstanding the statement in the first place?

1° You understood what I meant correctly.

2° I tried but was not able to make an example. You are right the claim was a bit too bold.

I thought that the compiler would use the information that `_` cannot escape, but i was not able to find a good example. I appologize.
