# Why doesn't this return statement return?

**URL:** https://discourse.julialang.org/t/why-doesnt-this-return-statement-return/124142
**Category:** General Usage
**Created:** [December 24, 2024, 12:29am UTC](https://discourse.julialang.org/t/why-doesnt-this-return-statement-return/124142 "2024-12-24T00:29:48Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![g2g](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@g2g](https://discourse.julialang.org/u/g2g)
#### Post date: [December 24, 2024, 12:29am UTC](https://discourse.julialang.org/t/why-doesnt-this-return-statement-return/124142/1 "2024-12-24T00:29:48Z")

</div>

When attempting to return out of a function’s loop, the following return statement instead behaves like a break statement: breaking out of the loop but not returning from the function.

```julia
function mweret()
	# write temp file
	fn = "temp.txt"
	write(fn, "1\n2\n3\n2\n1\n")
	
	# read temp file
	nLine = 0
	linePrev = ""
	open(fn, "r") do f
		while !eof(f)
			line = readline(f, keep=true)
			print(line)
			nLine += 1
			if linePrev > line
				println("non-increasing order at line $nLine")
				close(f)
				rm(fn)
				return nothing
			end
			linePrev = line
		end
	end # open
	
	println("POST LOOP!")
end

```

In contrast, when looping through similar elements but in a vector, the return statement behaves as expected: breaking out of the loop AND returning from the function.

```julia
function mweret2()
	iPrev = 0
	nElement = 0
	for i in [1; 2; 3; 2; 1]
		println("$i")
		nElement += 1
		if iPrev > i
			println("non-increasing order at element $nElement")
			return nothing
		end
		iPrev = i
	end
	println("POST LOOP!")
end

```

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [December 24, 2024, 1:08am UTC](https://discourse.julialang.org/t/why-doesnt-this-return-statement-return/124142/2 "2024-12-24T01:08:09Z")

</div>

The `open(...) do` notation defines a new anonymous function in the indented block, which is then passed to `open` as a first parameter. Therefore, the `return` inside the block exits the anonymous function back into `mweret`.

In the second function `mweret2`, the `return` exits `mweret2` function as expected.

TL;DR The `do` notation is a confusing at first (for me too), but then becomes natural and useful.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [December 24, 2024, 9:53am UTC](https://discourse.julialang.org/t/why-doesnt-this-return-statement-return/124142/3 "2024-12-24T09:53:22Z")

</div>

Easy to fix:

```julia
function mweret()
        # write temp file
        fn = "temp.txt"
        write(fn, "1\n2\n3\n2\n1\n")

        # read temp file
        nLine = 0
        linePrev = ""
        open(fn, "r") do f
                while !eof(f)
                        line = readline(f, keep=true)
                        print(line)
                        nLine += 1
                        if linePrev > line
                                println("non-increasing order at line $nLine")
                                break
                        end
                        linePrev = line
                end
        end # open
        
        println("POST LOOP!")
end

```

---

<div class="post-metadata">

### Author: ![g2g](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@g2g](https://discourse.julialang.org/u/g2g)
#### Post date: [December 24, 2024, 1:50pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-statement-return/124142/4 "2024-12-24T13:50:43Z")

</div>

> [@ufechner7](#):
>
> Easy to fix

In my application, I want to break out of the loop AND avoid the post loop processing AND abort the application. So my simple fix was to use the [error](https://docs.julialang.org/en/v1/manual/control-flow/#Exception-Handling) function.

---

<div class="post-metadata">

### Author: ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)
#### Post date: [December 24, 2024, 3:53pm UTC](https://discourse.julialang.org/t/why-doesnt-this-return-statement-return/124142/5 "2024-12-24T15:53:55Z")

</div>

You could also let the anonymous function return something informative like a `Symbol` or `Bool` flag. E.g.

```julia
function mweret(seq)
    # write temp file
    fn = tempname()
    write(fn, join("$x\n" for x in seq))

    # read temp file
    nLine = 0
    linePrev = ""
    open(fn, "r") do f
        while !eof(f)
           line = readline(f, keep=true)
            print(line)
            nLine += 1
            if linePrev > line
                println("non-increasing order at line $nLine")
                return false # Indicates we stopped early
                # Cleanup is handled by open(::Function, ...) itself
            end
            linePrev = line
        end
        return true # We went through the entire file
    end || return # (or exit() instead of return if you want to terminate the application)
    println("POST LOOP!")
end

```

```julia-repl
julia> mweret([1, 2, 3, 4, 5])
1
2
3
4
5
POST LOOP!

julia> mweret([1, 2, 3, 2, 1])
1
2
3
2
non-increasing order at line 4

```
