# Explicit "return" in anonymous functions

**URL:** <https://discourse.julialang.org/t/explicit-return-in-anonymous-functions/4857>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 14, 2017, 4:23pm UTC](https://discourse.julialang.org/t/explicit-return-in-anonymous-functions/4857 "2017-07-14T16:23:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alyst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alyst/32/18855_2.png) [@alyst](https://discourse.julialang.org/u/alyst)\
**Post date:** [July 14, 2017, 4:23pm UTC](https://discourse.julialang.org/t/explicit-return-in-anonymous-functions/4857/1 "2017-07-14T16:23:11Z")

</div>

I’m not sure whether it’s a bug or a feature, so posting it here.

The following constructs generate identical code (tested with both v0.5.2 and v0.6.0):

```nohighlight
julia> map(x -> x, [1,2,3])
3-element Array{Int64,1}:
 1
 2
 3

julia> map(x -> begin return x end, [1,2,3])
3-element Array{Int64,1}:
 1
 2
 3

```

I thought that the next one should also be identical, but it’s not:

```nohighlight
julia> map(x -> return x, [1,2,3])
ERROR: MethodError: no method matching (::##9#10)()
Closest candidates are:
  #9(::Any) at REPL[5]:1
Stacktrace:
 [1] map(::##9#10) at ./abstractarray.jl:1930
 [2] macro expansion at ./REPL.jl:97 [inlined]
 [3] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73

```

It seems here “`return`” refers to [some] outer block.  
Is it by design or it’s a bug? Is it discussed somewhere in the manual?

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [July 14, 2017, 4:59pm UTC](https://discourse.julialang.org/t/explicit-return-in-anonymous-functions/4857/2 "2017-07-14T16:59:12Z")

</div>

Looks to me like it’s getting parsed as `map(x -> return (x, [1, 2, 3]) )`

```julia
e = :(map(x -> return x, [1,2,3]))
e.args[2].args[2].args[2].args[1]

```

---

<div class="post-metadata">

**Author:** ![alyst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alyst/32/18855_2.png) [@alyst](https://discourse.julialang.org/u/alyst)\
**Post date:** [July 14, 2017, 5:12pm UTC](https://discourse.julialang.org/t/explicit-return-in-anonymous-functions/4857/3 "2017-07-14T17:12:22Z")

</div>

Ah, indeed! And this one works:

```julia
julia> map(x -> (return x), [1,2,3])
3-element Array{Int64,1}:
 1
 2
 3

```
