return statement
Page content
- With
return, functions send returns value back to the caller code. - Types:
- Explicit
returnstatement: It terminates a function execution and sends the return value back to the caller code. - Implicit
returnstatement:** If you don’t write areturnstatement, it returnsNone.
- Explicit
Short-circuiting loops
- A
returnstatement 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`