# Code after return statement gets executed even if function does not specify a return type

**URL:** <https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089>\
**Category:** Using Swift\
**Created:** [December 28, 2019, 7:53am UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089 "2019-12-28T07:53:39Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![SubjugaterCephalopod](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/subjugatercephalopod/32/9536_2.png) [@SubjugaterCephalopod](https://forums.swift.org/u/SubjugaterCephalopod)\
**Post date:** [December 28, 2019, 7:53am UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089/1 "2019-12-28T07:53:39Z")

</div>

Just spent a while trying to figure out a bizarre, paranormal bug:

I had a function for testing physics components for a game. One entity was moving faster than the other despite being exactly the same.

Turns out that it was being added to the scene twice because code after a `return` was being executed:

 ![Execution After Return](https://global.discourse-cdn.com/swift/original/2X/9/912718f246140fa526e26545de5fa53fd3a55e9d.png)

You can replicate this in a Playground:

```swift
var wth = 0

func SwiftPlease() {
    wth += 1
    return
    wth += 1 // ⚠️ Expression following 'return' is treated as an argument of the 'return'
}

SwiftPlease()
print(wth) // Can you guess what this will be?

```

I can _almost_ see what's happening here, but why allow an argument for the return at all if the function's signature does not specify a return value?

---

<div class="post-metadata">

**Author:** ![eimantas](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eimantas/32/13145_2.png) [@eimantas](https://forums.swift.org/u/eimantas)\
**Post date:** [December 28, 2019, 8:03am UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089/2 "2019-12-28T08:03:55Z")

</div>

Try adding semicolon after a `return` you should be fine.

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [December 28, 2019, 11:55am UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089/3 "2019-12-28T11:55:41Z")

</div>

I would be very surprised/disappointed if this fixes it.

@SubjugaterCephalopod Have you tried doing a clean build? I suspect your code's just running some old version before the `return` was put in.

---

<div class="post-metadata">

**Author:** ![jawbroken](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jawbroken](https://forums.swift.org/u/jawbroken)\
**Post date:** [December 28, 2019, 12:15pm UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089/4 "2019-12-28T12:15:31Z")

</div>

`+=` returns `Void`, and you're free to explicitly return `Void`.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [December 28, 2019, 9:24pm UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089/5 "2019-12-28T21:24:33Z")

</div>

Perhaps this should be a compiler warning. Since it's technically valid, but likely not something people would be doing.

---

<div class="post-metadata">

**Author:** ![Martin](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Martin](https://forums.swift.org/u/Martin)\
**Post date:** [December 28, 2019, 9:37pm UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089/6 "2019-12-28T21:37:30Z")

</div>

This seems to be a known issue, reported as [SR-2028 "return" in func returning void should not have an argument](https://bugs.swift.org/browse/SR-2028).

---

<div class="post-metadata">

**Author:** ![SubjugaterCephalopod](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/subjugatercephalopod/32/9536_2.png) [@SubjugaterCephalopod](https://forums.swift.org/u/SubjugaterCephalopod)\
**Post date:** [December 29, 2019, 1:12am UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089/7 "2019-12-29T01:12:33Z")

</div>

There is an warning, but it seems to be delayed.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [December 29, 2019, 2:19pm UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089/8 "2019-12-29T14:19:28Z")

</div>

I‘d be against a warning because I frequently use that in the `else` body of a `guard` statement. There is nothing wrong learning that several operators including `=` return `Void` and using that in combination with `return`.

To me this is a programmer mistake, not a bug, since in Swift there is _always_ a return type, but for convenience it could be implicit. Not understanding this fact is a source of bugs.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [December 29, 2019, 4:57pm UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089/9 "2019-12-29T16:57:51Z")

</div>

> [@DevAndArtist](#):
>
> I‘d be against a warning because I frequently use that in the `else` body of a `guard` statement.

I’m quite interested as to why you’d do that instead of just using multi-line statements, or is it just purely stylistic choice?

> [@DevAndArtist](#):
>
> Not understanding this fact is a source of bugs.

I’d say it’s a rarely-used fact, and most programmer won’t ever need to think in terms of `Void`-returning function.

Anyhow, I don’t have a strong opinion on this matter. I know of the technicality of it (for a long time actually). I still feel that it’d be hard to read if I actually use it, but that’s about it.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [December 29, 2019, 5:37pm UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089/10 "2019-12-29T17:37:28Z")

</div>

> [@Lantua](#):
>
> stylistic choice

This ☝.

This should be the `else` body from a `guard` statement.

```swift
// #1
return self.foo()

// #2
self.foo()
return

```

#2 feels redundant to me and therefore I prefer #1.

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [December 30, 2019, 9:04am UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089/11 "2019-12-30T09:04:31Z")

</div>

> [@Lantua](#):
>
> I’m quite interested as to why you’d do that instead of just using multi-line statements, or is it just purely stylistic choice?

In a lot of async code that take a completion handler, the completion handler is semantically like a (delayed) return value. You need it on every early return path.

```swift
guard condition else { return completion(nil) }

```

…neatly mirrors `return nil` in a synchronous function.

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [December 30, 2019, 9:06am UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089/12 "2019-12-30T09:06:27Z")

</div>

I would always have return and its argument on the same line though. Using a newline between the two would probably put me off.

---

<div class="post-metadata">

**Author:** ![AlanK](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alank/32/8674_2.png) [@AlanK](https://forums.swift.org/u/AlanK)\
**Post date:** [January 4, 2020, 4:30pm UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089/13 "2020-01-04T16:30:54Z")

</div>

I am also fond of

```
return voidReturningFunction()

```

and would be dismayed to have it produce a warning, but I would not be sad to see

```
return
voidReturningFunction()

```

produce a warning. Does anyone have a use case for why it shouldn’t be discouraged? Assume we’re being conservative and no warning is produced for

```
return operand
    += otherOperand

```

---

<div class="post-metadata">

**Author:** ![jawbroken](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jawbroken](https://forums.swift.org/u/jawbroken)\
**Post date:** [January 8, 2020, 12:13pm UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089/14 "2020-01-08T12:13:59Z")

</div>

That does produce a warning, and the warning is shown in the original post.

---

<div class="post-metadata">

**Author:** ![idrougge](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/idrougge/32/3885_2.png) [@idrougge](https://forums.swift.org/u/idrougge)\
**Post date:** [January 12, 2020, 3:47am UTC](https://forums.swift.org/t/code-after-return-statement-gets-executed-even-if-function-does-not-specify-a-return-type/32089/15 "2020-01-12T03:47:56Z")

</div>

I'm also an avid user of `return funcThatReturnsVoid()` in callbacks and guard clauses, but if this is a impediment for new Swift users, I would relent.
