Skip to content

[Term Entry] Python Keywords: return #6978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions content/python/concepts/keywords/terms/return/return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
Title: 'return'
Description: 'Ends a function and sends a value back to the caller.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Arguments'
- 'Functions'
- 'Parameters'
- 'Python'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

The **`return`** keyword in Python is used inside a [function](https://www.codecademy.com/resources/docs/python/functions) to terminate its execution and send a value back to the caller.

## Syntax

```pseudo
def function_name():
return value
```

The `return` statement is used inside a function defined using the `def` keyword. It must be written in lowercase (`return`). Using incorrect casing like `Return` or `RETURN` will result in a `SyntaxError`.

## Example 1

In this example, the `sum()` function returns the sum of the two input values:

```py
def sum(a, b):
result = a + b
return result
print (sum(3, 4))
```

The output of this code is:

```shell
7
```

## Example 2

In this example, the `get_user()` function demonstrates returning multiple values by separating them with commas:

```py
def get_user():
firstname = "Jane"
lastname = "Doe"
age = 30
return firstname, lastname, age

user_firstname, user_lastname, user_age = get_user()
print(user_firstname)
print(user_lastname)
print(user_age)
```

The output of this code will be:

```shell
Jane
Doe
30
```

## Codebyte Example

The `return` keyword can be used to exit a function early. When used without a value, it implicitly returns `None` (which means no value is returned). The following code is demonstrating the same:

```codebyte/python
# Change the value of num and click run to see how the function output changes.

num = 10

def check_positive(x):
if x < 0:
print("Number is negative, stopping early.")
return # exit early, returns None implicitly
print("Number is positive or zero.")
return x

print("Returned value:", check_positive(num))
```