Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
A function in Python can return a value to the caller using the return
keyword. The returned value can be used by the caller as needed. In this tutorial, we will learn how to define functions that return values and how to use the returned values.
To define a function that returns a value, use the return
keyword followed by the value or expression you want to return.
def add(a, b): return a + b
In this example, the add
function takes two arguments, a
and b
, and returns their sum.
To call a function that returns a value, simply use the function name followed by the required arguments in parentheses. You can then store the returned value in a variable or use it in an expression.
result = add(5, 7) print(result) # Output: 12
In this example, we call the add
function with the arguments 5
and 7
, and the returned value is stored in the result
variable.
You can directly use the returned value from a function in an expression without storing it in a variable.
print(add(3, 4) * 2) # Output: 14
In this example, we call the add
function with the arguments 3
and 4
, and the returned value is directly used in the expression add(3, 4) * 2
.
In Python, you can return multiple values from a function by separating them with commas.
def divide_and_remainder(a, b): quotient = a // b remainder = a % b return quotient, remainder
When you call this function, the returned values will be packed into a tuple.
result = divide_and_remainder(11, 3) print(result) # Output: (3, 2)
You can also use tuple unpacking to directly assign the returned values to separate variables.
quotient, remainder = divide_and_remainder(11, 3) print(quotient) # Output: 3 print(remainder) # Output: 2
In this tutorial, we covered how to define and call functions that return values, how to use returned values in expressions, and how to return multiple values. These concepts are essential for creating reusable and modular code in Python.
How to return values from functions in Python:
return
keyword.def add_numbers(a, b): return a + b result = add_numbers(3, 5) print(result) # Output: 8
Multiple return values in Python functions:
def get_coordinates(): return 3, 7 x, y = get_coordinates() print(x, y) # Output: 3 7
Returning None in Python functions:
return
statement, or it explicitly returns None
, it is assumed to return None
.def do_nothing(): pass # Implicitly returns None result = do_nothing() print(result) # Output: None
Handling different data types in function return values:
def get_result(value): if value > 0: return "Positive" elif value < 0: return "Negative" else: return "Zero"
Type hints for function return values in Python:
def add_numbers(a: int, b: int) -> int: return a + b
Returning tuples, lists, and dictionaries from functions in Python:
def get_data(): return (1, 2, 3), [4, 5, 6], {'a': 7, 'b': 8}
Returning custom objects in Python functions:
class Person: def __init__(self, name, age): self.name = name self.age = age def create_person(): return Person("John", 25)
Returning functions as values in Python:
def get_multiplier(factor): def multiplier(x): return x * factor return multiplier times_two = get_multiplier(2) result = times_two(5) print(result) # Output: 10
Function chaining and return values in Python:
def add(x): return x + 5 def multiply(x): return x * 2 result = multiply(add(3)) print(result) # Output: 16
Returning values from recursive functions in Python:
def factorial(n): if n == 0 or n == 1: return 1 else: return n * factorial(n-1)