6 Functions
Review¶
Wow, we've made a lot of progress here
- Variable types like integer, float, string, boolean, and list
if
statements using a boolean operator like==
,>
,<
for
loops- bringing together
for
loops withif
statements
We also touched on
- The print() statement
- The use of
#
to make a free-form comment in our code (e.g. note to self) - The importance of indentation/spacing in Python
The next concept is a Python function
using def
.¶
We will start with some examples.
In [6]:
Copied!
# first we define a function to add to numbers
def add(a, b):
result = a + b
return result
# first we define a function to add to numbers
def add(a, b):
result = a + b
return result
In [7]:
Copied!
# now we will use our new function
print(add(1, 2))
result = add(3, 4)
print('result:', result)
# now we will use our new function
print(add(1, 2))
result = add(3, 4)
print('result:', result)
3 result: 7
In [8]:
Copied!
# we can also pass our own variables to a function
firstNumber = 12
secondNumber = 20
result = add(firstNumber, secondNumber)
print('my result is:', result)
# we can also pass our own variables to a function
firstNumber = 12
secondNumber = 20
result = add(firstNumber, secondNumber)
print('my result is:', result)
my result is: 32
Important concept¶
Our function defined as
def add(a, b):
return a + b
Does not expect a variable named a
or b
, it just expects a number for a
and a number for b
.
Recap on function¶
A function
is a fundamental concept in mathematics.
It takes some inputs, performs a calculation, and return a result.
We have been using functions all along, print()
is a function. It takes an input and prints it to the notebook.
Bringing it together¶
Lets use a function to test if our sensor is above a given threshold
In [9]:
Copied!
# a function to test if a value is above a threshold
def isAboveThreshold(value, threshold):
"""Return True if value > threshold, otherwise False.
"""
return value > threshold
# these are the values we have read from our sensor
sensorValues = [0, 3, 5, 7, 12, 15, 27, 35, 2, 2, 2, 100]
# we define a threshold where we want to report the value
threshold = 12
for sensorValue in sensorValues:
if isAboveThreshold(sensorValue, threshold):
print('WARNING: sensor value is above threshold', threshold, 'and has value', sensorValue)
# a function to test if a value is above a threshold
def isAboveThreshold(value, threshold):
"""Return True if value > threshold, otherwise False.
"""
return value > threshold
# these are the values we have read from our sensor
sensorValues = [0, 3, 5, 7, 12, 15, 27, 35, 2, 2, 2, 100]
# we define a threshold where we want to report the value
threshold = 12
for sensorValue in sensorValues:
if isAboveThreshold(sensorValue, threshold):
print('WARNING: sensor value is above threshold', threshold, 'and has value', sensorValue)
WARNING: sensor value is above threshold 12 and has value 15 WARNING: sensor value is above threshold 12 and has value 27 WARNING: sensor value is above threshold 12 and has value 35 WARNING: sensor value is above threshold 12 and has value 100
Tutorial¶
- Write a funtion
subtract
that takes two variables. Your function will use-
to subtract the given variables. - Write a few lines of code to call your function with different values to subtract.
In [10]:
Copied!
# enter your code here and click the run button to test it
# enter your code here and click the run button to test it