voidex/20-29 Areas/20 Programming/20.01 Python/return.md

980 B
Raw Permalink Blame History

title tags aliases
return
python
functions
return
return statement
return keyword
none

[!note] Concept
The return keyword ends a function and sends back a value. Without it, a function will return None.

[!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 functions main purpose is side effects.
  • Avoid mixing return types in the same function (e.g., returning both int and None).

[!info] Related Notes
functions
len()
pythonSyntaxOverview