> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Return or Yield Outside Function

#### Sourcery refactoring id: `return-or-yield-outside-function`

#### Description

Remove return or yield statements found outside function definitions.

#### Before

```python
for i in range(10):
    print(i)
    return i
```

#### After

```python
for i in range(10):
    print(i)
```

#### Alternative

If you intend to `return` or `yield` some value, make sure that the statement is
correctly placed and indented in your code:

*Before*:

```python
def square(x):
    y = x**2


return y
```

*After*:

```python
def square(x):
    y = x**2
    return y
```

#### Explanation

The
[`return`](https://docs.python.org/3/reference/simple_stmts.html#the-return-statement)
and
[`yield`](https://docs.python.org/3/reference/simple_stmts.html#the-yield-statement)
statements can only be used inside function definitions. Using them outside this
context, or inside nested class definitions, is a
[`SyntaxError`](https://docs.python.org/3/library/exceptions.html#SyntaxError).

This error may be very easy to pass through since sometimes it is caused by a
wrong indentation.
