What’s the Difference Between return and break in Python?

5/5 - (1 vote)

πŸ’¬ Question: What is the difference between return and break? When to use which?

Let’s first look at a short answer before we dive into a simple example to understand the differences and similarities between return and break.

Comparison

Both return and break are keywords in Python.

  • The keyword return ends a function and passes a value to the caller.
  • The keyword break ends a loop immediately without doing anything else. It can be used within or outside a function.
returnbreak
Used to end a functionUsed to end a for or while loop
Passes an optional value to the caller of the function (e.g., return 'hello')Doesn’t pass anything to the “outside”

While they serve a different purpose, i.e., ending a function vs ending a loop, there are some cases where they can be used interchangeably.

Similar Use Cases

The following use case shows why you may have confused both keywords return and break. In both cases, you can use them to end a loop inside a function and return to the outside.

Here’s the variant using return:

def f():
    for i in range(10):
        print(i)
        if i>3:
            return


f()

And here’s the variant using break:

def f():
    for i in range(10):
        print(i)
        if i>3:
            break


f()

Both code snippets do exactly the same—printing out the first 5 values 0, 1, 2, 3, and 4.

Output:

0
1
2
3
4

However, this is where the similarity between those two keywords ends. Let’s dive into a more common use case where they both perform different tasks in the code.

Different Use Cases

The following example uses both keywords break and return. It uses the keyword break to end the loop as soon as the loop variable i is greater than 3.

So the line print(i) is never executed after variable i reaches the value 4—the loop ends.

But the function doesn’t end because break only ends the loop and not the function. That’s why the statement print('hi') is still executed, and the return value of the function is 42 (which we also print in the final line).

def f():
    for i in range(10):
        if i>3:
            break
        print(i)
    print('hi')
    return 42


print(f())

Output:

0
1
2
3
hi
42

Summary

The keyword return is different and more powerful than the keyword break because it allows you to specify an optional return value. But it can only be used in a function context and not outside a function.

  • You use the keyword return to give back a value to the caller of the function or terminate the whole function.
  • You use the keyword break to immediately stop a for or while loop.

🐍 Rule: Only if you want to exit a loop inside a function and this would also exit the whole function, you can use both keywords. In that case, I’d recommend using the keyword return instead of break because it gives you more degrees of freedom, i.e., specifying the return value. Plus, it is more explicit which improves the readability of the code.


Thanks for reading over the whole tutorial—if you want to keep learning, feel free to join my email academy. It’s fun! πŸ™‚

Recommended Video

Python return Keyword - A Simple Illustrated Guide