Return Values

Returning single and multiple values

Interview Relevant: Basic function design

Return Values

Functions can return one or more values.

Code Examples

Return values from functions.

python
1# Single return
2def square(x):
3    return x ** 2
4
5# Multiple returns (tuple)
6def divide(a, b):
7    quotient = a // b
8    remainder = a % b
9    return quotient, remainder
10
11q, r = divide(10, 3)
12
13# Early return
14def is_even(n):
15    if n % 2 == 0:
16        return True
17    return False
18
19# Return None (implicit)
20def no_return():
21    print("No return statement")
22# result = no_return()  # result is None
23
24# Return expression
25def abs_value(x):
26    return x if x >= 0 else -x

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In