January 3, 2022โ€ข6 min read

The Return Statement in Python

The return statement lets you send a value a function has already processed back to the caller.

Read in Indonesian

This time the subject is the return statement in Python 3, the statement that lets you send a value back from a function.

About the Return Statement

The return statement is a special statement you can only use inside a function. It lets you send a value the function has already processed back to the caller.

As a picture, the return statement allows this:

PYTHON
c = (a*a + b*b)**.5           # compute the hypotenuse without a function
c = hitung_sisi_miring(a, b)  # compute it with a function

So how is it done? First you need to cover the format.

How to Write the Return Statement

This statement consists of the return keyword followed by an optional value you want to return. That return value can hold anything: numeric data, a string, a list, or no value at all.

TEXT
def nama_fungsi([parameter]):
    tubuh_fungsi
    ...
    ...
    return [nilai_kembalian]
  • nilai_kembalian: the value that will be returned to the caller (optional)

That's it. Just one line of code that turns out to be very useful ๐Ÿ˜Œ.

Example Uses of the Return Statement

This time you'll try the return statement in 2 cases. The first returns a value, and the second uses the return statement to force an exit from the execution of a function.

Returning a Value with the Return Statement

As mentioned earlier, the purpose of the return statement is to send a value from the function back to the caller. Now you'll build the code for it.

Say you want to compute the hypotenuse of a triangle. As you know, this calculation can be solved with the Pythagorean theorem. You still remember it, right?

c=a2+b2c=\sqrt{a^2+b^2}

The equation is simple enough. Translated into Python code, it looks like this:

PYTHON
c = (a*a + b*b)**.5

Now say there are 3 triangles whose hypotenuses still need finding, each with different side lengths.

Triangle name Base (a) Height (b)
triangle 1 3 4
triangle 2 5 12
triangle 3 7 24

If you want each hypotenuse (c) with the Python code built earlier, it comes out like this:

PYTHON
a1, a2, a3 = 3, 5, 7
b1, b2, b3 = 4, 12, 24

c1 = (a1*a1 + b1*b1)**.5
c2 = (a2*a2 + b2*b2)**.5
c3 = (a3*a3 + b3*b3)**.5

print("sisi miring segitiga 1:", c1)
print("sisi miring segitiga 2:", c2)
print("sisi miring segitiga 3:", c3)

output:

TEXT
sisi miring segitiga 1: 5
sisi miring segitiga 2: 13
sisi miring segitiga 3: 25

That code is valid, but look again. There are 3 lines of code repeating the same logic with different variables, as you can see in the calculation of c1, c2, and c3. You can actually shorten this by making use of a function.

You'll build a function named hitung_sisi_miring that takes 2 parameters, a (base) and b (height). The function body holds the hypotenuse calculation (c) according to the formula, then you return the value with the return statement.

So the code becomes like this:

PYTHON
def hitung_sisi_miring(a, b):
    c = (a*a + b*b)**.5
    return c

c1 = hitung_sisi_miring(3, 4)
c2 = hitung_sisi_miring(5, 12)
c3 = hitung_sisi_miring(7, 24)

print("sisi miring segitiga 1:", c1)
print("sisi miring segitiga 2:", c2)
print("sisi miring segitiga 3:", c3)

output:

TEXT
sisi miring segitiga 1: 5
sisi miring segitiga 2: 13
sisi miring segitiga 3: 25

Nice. This way you don't have to write the long formula over and over, and you can see the code is easier to read โ€” right?

Instead of writing the calculation formula one by one, you now only call the hitung_sisi_miring function with matching arguments. The reason is that the calculation logic has moved into the function body.

Now let's take apart what happens on the call hitung_sisi_miring(3, 4)

PYTHON
def hitung_sisi_miring(a, b):  # the call hitung_sisi_miring(3, 4), so a = 3 and b = 4
    c = (a*a + b*b)**.5 # run the formula, the value is c = 5
    return c            # return the value of c to the caller

When the function reaches the return keyword, the related value is returned (in this case c). So the value of hitung_sisi_miring(3, 4) is 5. That makes the assignment c1 = hitung_sisi_miring(3, 4) become c1 = 5 automatically, and the same goes for c2 and c3.

Using the Return Statement to Stop a Function's Execution

Earlier you wrote the return statement at the end of the function body. What happens if you write it in the middle? Let's find out.

Look again at the function you built earlier. You can see that hitung_sisi_miring accepts any parameter, including negative numbers. A triangle's side length can't be negative, so the function needs a guard. Say the function should return -1 when either parameter is negative, then the code can be written like this:

PYTHON
def hitung_sisi_miring(a, b):
    if a < 0 or b < 0:
        return -1

    c = (a*a + b*b)**.5
    return c

You can see there are 2 return statements in that function, but how does the execution go? Which return runs? Let's try running it.

PYTHON
def hitung_sisi_miring(a, b):
    if a < 0 or b < 0:
        return -1

    c = (a*a + b*b)**.5
    return c

c1 = hitung_sisi_miring(3, 4)   # a and b are positive
c2 = hitung_sisi_miring(-3, 4)  # a is negative, b is positive
c3 = hitung_sisi_miring(3, -4)  # a is positive, b is negative
c4 = hitung_sisi_miring(-3, -4) # a and b are negative

print("nilai c1:", c1)
print("nilai c2:", c2)
print("nilai c3:", c3)
print("nilai c4:", c4)

Output:

TEXT
nilai c1: 5
nilai c2: -1
nilai c3: -1
nilai c4: -1

In that output you can see that when you pass a negative value to one or both parameters, the value returned is -1, or in this case the value from the first return statement. So what about the next return statement?

In Python, when a function reaches the return keyword, it leaves the function and returns the value right then and there. Because a value has already been returned, the execution of the function is considered finished, so none of the code after it runs.

That is why you get the output above.

The return statement can also act like the break keyword in a loop. Instead of stopping an iteration, though, return lets you force an exit and stop the flow of the function. You do it by calling return without giving it a return value. Take a look at the code below:

PYTHON
def cetak():  # definition of the cetak function
    print("Halo, Inva!")
    print("Ini adalah perintah sebelum return dipanggil")
    print("Setelah return dieksekusi maka fungsi dinyatakan selesai")
    return
    print("Baris ini tidak akan dieksekusi")
    print("Baris ini juga tidak")
    print("Apalagi ini!!")

cetak() # call the cetak function

Output:

TEXT
Halo, Inva!
Ini adalah perintah sebelum return dipanggil
Setelah return dieksekusi maka fungsi dinyatakan selesai

You can see it. Every print command after the return keyword never runs.

What's Next?

That's all for this one, and I hope it's useful ๐Ÿ˜‰

Next time you'll look at one more special keyword in functions: the pass statement in Python 3.

See ya ๐Ÿ‘‹

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all

For developers who learn by struggling

ยฉ 2026 Inva.dev.