Skip to content

Commit

Permalink
Adds more context around early return
Browse files Browse the repository at this point in the history
  • Loading branch information
dpshelio committed Aug 19, 2024
1 parent 1c36326 commit fe784be
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions ch01python/016functions.ipynb.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# ---

# %% [markdown]
# # Functions
# # Functions

# %% [markdown]
# ## Definition
Expand Down Expand Up @@ -146,22 +146,21 @@ def double(vec):
# Having multiple `return` statements is a common practice in programming.
# These `return` statements can be placed far from each other, allowing a
# function to return early if a specific condition is met.
#
# For instance, the function below returns early if a number greater than
# 20 is passed as an argument.
#
# The dynamic typing of Python also makes it easy to return different types
# of values based on different conditions, but such code is not considered
# a good practice. It is also a good practice to have a default return value
# in the function if it is returning something in the first place. For instance,
# the function below could use an `elif` or an `else` condition for the second
# `return` statement, but that would not be a good practice.
#
# For example, a function `isbigger` could be written as:
# ```
# def isbigger(x, limit=20):
# return x > limit
# ```
# However, what if you want to print a message on the screen when a smaller
# value has been found? That's what we do below, where the function below
# returns early if a number greater than given limit.

# %%
def isbigger(x, limit=20):
if x > limit:
return True
print("Got here")
print("Value is smaller")
return False


Expand All @@ -171,6 +170,28 @@ def isbigger(x, limit=20):
isbigger(40, 15)


# %% [markdown]
#
# The dynamic typing of Python also makes it easy to return different types
# of values based on different conditions, but such code is not considered
# a good practice. It is also a good practice to have a default return value
# in the function if it is returning something in the first place. For instance,
# the function below could use an `elif` or an `else` condition for the second
# `return` statement, but that would not be a good practice. In those cases,
# Python would be using the implicit `return` statement. For example, what's
# returned in the following example when the argument `x` is equal to the `limit`?

# %%
def isbigger(x, limit=20):
if x > limit:
return True
elif x < limit:
print("Value is smaller")
return False

# %%
# Write your own code to find out

# %% [markdown]
#
# Return without arguments can be used to exit early from a function
Expand Down Expand Up @@ -201,7 +222,7 @@ def extend(to, vec, pad):
# %% [markdown]
#
# If a vector is supplied to a function with a `*`, its elements
# are used to fill each of a function's arguments.
# are used to fill each of a function's arguments.
#
#
#
Expand Down

0 comments on commit fe784be

Please sign in to comment.