Introduction to functions
A function allows us to write generic code that can operate on different argument values. The syntax looks like this
Example function -- quadratic formula
We know from mathematics that if we have a mathematical function , that the function in general has 2 roots, which are values of for which . These roots are defined as:
ParseError: KaTeX parse error: {align} can be used only in display mode.
First, let's define the appropriate function signature for a function named quadratic
. For right now, we'll just have it return a value of 0.
Now let's compute the value of the discriminant inside the function. The discriminant is just the value of . We'll still return 0 at this point.
It's important to note that the variable named discriminant
inside the quadratic
function isn't available outside of that function. It's called a local variable.
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/tmp/ipykernel_1022/3314607954.py in <module>
----> 1 print(discriminant)
NameError: name 'discriminant' is not defined
Now let's create local variables within quadratic
named numerator
and denominator
that compute their respective values.
Now, we'll return a list containing the two roots of the equation, rather than the "hard-coded" value of 0.
Test the function by solving , which can be written
If the value of discriminant
is 0, there is only one root, so add some code in to test for this condition, and return a list with a single element if true.
Test this for the quadratic equation . The only solution to this should be .
Finally, when discriminant < 0
there are no real roots to the equation. For this, we should return an empty list.
And test it for
Equations for driving time saved
First, let's define and implement a function named drive_time_in_minutes
that computes the time it takes to drive distance
at a speed of speed_in_mph
.
And test it for the 35 miles to the GR airport from Holland at a rate of 70 mph.
Since the speed limit is often 70 mph, let's make that the default value for the function.
Now we can call it like this:
Now define the function time_saved_by_speeding
which takes arguments distance
, speed_limit
, and amount_over_limit
. The implementation can make use of the drive_time_minutes
function to compute the times at the two different speeds.
Test it out to see what we get.
What happens if we try to define a version of time_saved_by_speeding
that has a default value for the speed_limit
argument?
Why does this happen? Suppose we called time_saved_by_speeding(35, 60)
. Did we intend:
The value of
speed_limit
to be 60, and we forgot a value foramount_over_limit
?The value of
speed_limit
to be its default of 70, andamount_over_limit
to be 60?
There's no way for the Python interpreter to know. The rule is that all arguments that don't have default values must come before any with default arguments.
How fast should we drive?
Finally, the equation to tell us how fast to drive if we want to save a certain amount of time when traveling a specific distance.