This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 5: Advanced function features

Returning multiple objects

face Josiah Wang

A function can usually return only one object.

def move_right(x):
    return x + 1

new_x = move_right(2)
print(new_x)

In Python, you can also return multiple objects.

To be more precise, you can return a single tuple object (a bit like a vector), which can be made up of more than one basic object! We will cover tuples in a future lesson. For now, just exploit this useful Python feature when needed!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def move_diagonally(x, y):
    return (x+1, y+1)

new_x, new_y = move_diagonally(1, 2)
print(new_x)
print(new_y)
print()

new_coords = move_diagonally(1, 2)
print(new_coords)
print(type(new_coords))

Output:

2
3

(2, 3)
<class 'tuple'>