# return

[Swift](/ios/swift.md) ⟩ [flow control](/ios/swift/flow.md) ⟩ [control transfer](/ios/swift/flow/transfer.md) ⟩ return

{% tabs %}
{% tab title="🧨 雷區" %}
{% hint style="danger" %} <mark style="color:red;">**cannot**</mark> use <mark style="color:purple;">**return**</mark> in the body of a [for...in](/ios/swift/flow/loop/for...in.md) loop, <mark style="color:yellow;">**except**</mark> that the loop is <mark style="color:yellow;">**inside**</mark> a <mark style="color:yellow;">**function**</mark>.
{% endhint %}

* [replit](https://replit.com/@pegasusroe/forin-vs-forEach#main.swift) ( 👉 see: [for...in vs. forEach](/ios/swift/flow/loop/for...in-vs.-foreach.md) )

{% hint style="danger" %}
⛔ <mark style="color:red;">**error**</mark>: [return](/ios/swift/flow/transfer/return.md) <mark style="color:red;">**invalid**</mark>**&#x20;**<mark style="color:yellow;">**outside**</mark> of a <mark style="color:blue;">**func**</mark>.
{% endhint %}

```javascript
for n in 1...10 {
    if n == 3 { return }    // ⛔ error
    //          ^^^^^^
    // ⛔ error: return invalid outside of a func
}
```

{% endtab %}

{% tab title="👥 相關" %}

* [return value](/ios/swift/type/category/basic/func/return.md)
* [implicit return](/ios/swift/type/implicit-return.md)
  {% endtab %}

{% tab title="📘 手冊" %}

* [ ] [Control Transfer Statements](https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID135)
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/ios/swift/flow/transfer/return.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
