Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] What’s the Difference Between return and break in Python?
1
What’s the Difference Between return and break in Python?

5/5 – (1 vote)

[Image: 1f4ac.png] Question: What is the difference between
Code:
return
and
Code:
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
Code:
return
and
Code:
break
.

Comparison

[Image: image-190-1024x682.png]

Both
Code:
return
and
Code:
break
are keywords in Python.
  • The keyword
    Code:
    return
    ends a function and passes a value to the caller.

  • The keyword
    Code:
    break
    ends a loop immediately without doing anything else. It can be used within or outside a function.

Code:
return
|
Code:
break


Used to end a function |
Used to end a
Code:
for
or
Code:
while
loop

Passes an optional value to the caller of the function (e.g.,
Code:
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
Code:
return
and
Code:
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
Code:
return
:

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

And here’s the variant using
Code:
break
:

Code:
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:

Code:
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
Code:
break
and
Code:
return
. It uses the keyword
Code:
break
to end the loop as soon as the loop variable
Code:
i
is greater than 3.

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

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

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

Output:

Code:
0 1 2 3 hi 42
Summary

The keyword
Code:
return
is different and more powerful than the keyword
Code:
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
    Code:
    return
    to give back a value to the caller of the function or terminate the whole function.

  • You use the keyword
    Code:
    break
    to immediately stop a
    Code:
    for
    or
    Code:
    while
    loop.

[Image: 1f40d.png] 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
Code:
return
instead of
Code:
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! [Image: 1f642.png]

Recommended Video

[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: