980 B
980 B
title | tags | aliases | ||||||
---|---|---|---|---|---|---|---|---|
return |
|
|
[!note] Concept
Thereturn
keyword ends a function and sends back a value. Without it, a function will returnNone
.[!example] Explicit Return
def sub_two_nums(a, b): return a - b result = sub_two_nums(100, 49) print(result) # 51
[!example] Implicit Return (None)
def no_return_example(): x = 2 + 2 print(no_return_example()) # None
[!important] Best Practices
- Always use
return
when a function is expected to give a result.- Use
None
as a placeholder return value when the function’s main purpose is side effects.- Avoid mixing return types in the same function (e.g., returning both
int
andNone
).[!info] Related Notes
functions
len()
pythonSyntaxOverview