# Reminder:
# Re-take of Exam 3 Blackboard is scheduled
# for Friday, November 30
#
# Exam 4 is 8am, Monday, December 10 in LB228
#
# Today: Functions
# What are functions?
# "Programmer-defined functions"
# (iso "library functions")
# Code with an assigned name
# Why functions?
# 1. Clarity
# Replace multiple lines of code with a single
# function call
# 2. Modularity
# Easy to fix, update, etc
# 3. Re-use
# Simply import the function (or paste the
# definition into your program) and the
# function can be used with your program -
# multiple times
# The lingo:
#
# Call:
# The command which causes the function code
# to execute
#
# e.g. y = sin(x)
# Has two parts: collection and call
# Collection: y =
# Call: sin(x)
# Arguments:
# The information being provided to the function
# so that it can accomplish its task
#
# e.g. y = sin(x)
# "x" is the argument to the function
# Return values:
# The meaning of a function call is the value(s)
# returned by the function
#
# y = sin(x) has two parts:
# y = and sin(x)
#
# What is being assigned to y?
# "The values returned (sent back) from the function"
# Function definition:
# The code that gets executed when the function
# is called
# Function header:
# The first line of the function definition. It
# specifies the name of the function and the variables
# which will accept the incoming arguments
# Function body:
# The code of the function definition that is executed
# using the incoming arguments as data
# Parameters:
# The variables in the function header which will
# receive copies of the incoming arguments
# Examples:
# A function which computes the real roots of a quadratic
# equation:
# Pick a name:
# quad_roots
# Identify the needed incoming information
# (What info will be provided to the function by the
# function call?)
#
# Our incoming information are the coefficients
# of the quadratic for which we want the roots
# (there are three of them)
#
# And we will pick three variable names for this
# incoming information: a, b, c
#
# These are the parameters
# Construct the function header:
# (Function definitions MUST be in the code BEFORE
# the use of the function - before the function call)
# def ():
import math
def quad_roots(a, b, c):
# Beneath the header, write the function body. And indent
# it. This is the code which will use the parameters to
# accomplish the task of the function
# roots = (-b +/- sqrt(b^2 - 4ac))/2a)
# Algorithm:
# Let's make sure the roots are real
# Compute the discriminant b^2-4ac
discrim = b**2 - 4*a*c
# If they're real, compute the roots
if discrim > 0:
r1 = (-b + math.sqrt(discrim))/(2*a)
r2 = (-b - math.sqrt(discrim))/(2*a)
roots = [r1, r2]
# Otherwise, print an error message
else:
print('Roots are imaginary!')
# But... what about the return values? How do we
# obtain them? How do we send them back to the call?
# To return information use the ...
# return function
return(roots)
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# EVERY FUNCTION HAS ITS OWN VARIABLES
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Rules of Thumb
#
# Avoid re-defining the parameters (very rare to need this)
# Avoid printing in a function
# Avoid collecting user information in a function