return statement
Page content
- With
return
, functions send returns value back to the caller code. - Types:
- Explicit
return
statement: It terminates a function execution and sends the return value back to the caller code. - Implicit
return
statement:** If you don’t write areturn
statement, it returnsNone
.
- Explicit
Short-circuiting loops
- A
return
statement inside a loop performs some kind of short-circuit. - It breaks the loop execution and makes the function return immediately.
Using return
in generator functions
def gen():
yield 1
yield 2
return 3 # indicates that the generator is done and makes the generator raise a `StopIteration`