Creating a Function - ๐ฒ#
Functions allow us to do repeated tasks easily by writing the code only once. Functions will have a name, inputs, and outputs and can be called anywhere the task is repeated.
There are functions that are built into python; for example, we have already been using the type() function, which tells us the type of variable we are using. Note that print is also a function!
aVal = 10.0
print( type( aVal ) )
<class 'float'>
Functions have four typical parts:
Name - what you call your function
Input arguments - what you provide
Outputs - what the function gives back
Math/Magic - what the function does
Creating Our Own Function#
In python, we use def to define a function with the function name and inputs followed by a colon. The python function is then separated from the rest of the code by a tab. Some languages use braces rather than indentation.
def functionName( inputs ):
# Operate on the inputs
ouputs = inputs + 5
# Return what we want to back
return outputs;
Letโs look at an example function, which changes degrees Fahrenheit to Celsius.
def changeFromFToC( farVal ):
cVal = (farVal - 32.0) * 5.0 / 9.0
return cVal
Here, our function name is changeFromFToC, the input is farVal, the temperature in Fahrenheit, the output is cVal, and the temperature in Celsius. We can print or store the output from the function. Note that the function has to be defined before we use it - the cell with the function definition has to have run before we can call the function.
print( "Change 14 deg F to Celsius" )
print( changeFromFToC( 14 ) )
print( "Change from 68 deg F to Celsius" )
niceTempC = changeFromFToC( 68 )
print( niceTempC )
Change 14 deg F to Celsius
-10.0
Change from 68 deg F to Celsius
20.0
Your turn! What is the temperature today? Convert it to Celsius.
For those who have the temperature in Celsius and want to convert it to Fahrenheit. Define a new function to do this.
Multiple inputs and outputs#
Here is an example of multiple outputs. We can actually work the output in a couple of different ways.
Multiple Output Function#
def changeFromFToCAndK( farVal ):
# Change the temperature from Fahrenheit to Celsius and Kelvin
cVal = (farVal - 32.0) * 5.0 / 9.0
kVal= cVal + 273.15
return cVal, kVal
Output: List#
def changeFromFToCAndK( farVal ):
# Change the temperature from Fahrenheit to Celsius and Kelvin
cVal = (farVal - 32.0) * 5.0 / 9.0
kVal= cVal + 273.15
return cVal, kVal
print( "Change 14 deg F to Celsius and Kelvin" )
print( changeFromFToCAndK( 14 ) )
print( "Change 32 deg F to Celsius and Kelvin" )
freezing = changeFromFToCAndK( 32 )
print( freezing[0] )
print( freezing[1] )
Change 14 deg F to Celsius and Kelvin
(-10.0, 263.15)
Change 32 deg F to Celsius and Kelvin
0.0
273.15
Output: Multiple Variables#
print( "Change 212 deg F to Celsius and Kelvin" )
boilingC, boilingK = changeFromFToCAndK( 212 )
print( boilingC )
print( boilingK )
Change 212 deg F to Celsius and Kelvin
100.0
373.15
Multiple Input Function#
def changeFromFToCOrK( farVal, tempType ):
if (tempType == "C"):
return (farVal - 32.0) * 5.0 / 9.0
elif (tempType == "K"):
return ((farVal - 32.0) * 5.0 / 9.0) + 273.15
else:
return "invalid temperature type"
print ( changeFromFToCOrK(70,"C") )
21.11111111111111
print ( changeFromFToCOrK(70,"K") )
294.26111111111106
print ( changeFromFToCOrK(70,"W") )
invalid temperature type
Functions calling other functions#
Functions can call other functions. Here we add three using a function to add two and then adding one.
def addTwo( inValAddTwo ):
# Add two to the input
return inValAddTwo + 2
def addThree( inValAddThree ):
# Add three two the input
return addTwo( inValAddThree ) + 1
# Add three to four
print( "Add three to four using addThree (which adds 1) but calls addTwo (which adds 2)" )
print( addThree( 4 ) )
Add three to four using addThree (which adds 1) but calls addTwo (which adds 2)
7
Recursive Functions#
Functions can also call themselves - these are called recursive functions. See how we calculate the factorial by subtracting one and calling the function again.
def factorialRecursive( facIn ):
print ("In factorialRecursive() and the current number is:", facIn)
if facIn == 1:
return facIn
else:
return facIn * factorialRecursive( facIn - 1)
print( "Use a recursive function to calculate the factorial of 5" )
print( factorialRecursive( 5 ) )
Use a recursive function to calculate the factorial of 5
In factorialRecursive() and the current number is: 5
In factorialRecursive() and the current number is: 4
In factorialRecursive() and the current number is: 3
In factorialRecursive() and the current number is: 2
In factorialRecursive() and the current number is: 1
120
Function Gotcha! ๐#
Note
The biggest gotcha on functions is with variable scope:
Variables defined in a function are not accessible from the outside
Functions have access to more than just the variables passed in
def addAnAnimal( animal ):
print ("\t","in the function")
print ("\t","I have access to dog:",dog)
print ("\t","I have access to animal:",animal)
newValue = animal + 1
print ("\t","I have access to newValue:",newValue)
return newValue
print ("outside the function")
dog = 10
print("dog:", dog)
print ("function output:",addAnAnimal( dog ))
outside the function
dog: 10
in the function
I have access to dog: 10
I have access to animal: 10
I have access to newValue: 11
function output: 11
If we would add:
print (newValue)
to the bottom, we would end up with this:
def addAnAnimal( animal ):
print ("\t","in the function")
print ("\t","I have access to dog:",dog)
print ("\t","I have access to animal:",animal)
newValue = animal + 1
print ("\t","I have access to newValue:",newValue)
return newValue
print ("outside the function")
dog = 10
print("dog:", dog)
print ("function output:",addAnAnimal( dog ))
print (newValue)
outside the function
dog: 10
ย ย ย ย ย ย in the function
ย ย ย ย ย ย I have access to dog: 10
ย ย ย ย ย ย I have access to animal: 10
ย ย ย ย ย ย I have access to newValue: 11
function output: 11
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-32-07cce689eb00> in <module>
11 print("dog:", dog)
12 print ("function output:",addAnAnimal( dog ))
---> 13 print (newValue)
NameError: name 'newValue' is not defined
Your Turn#
Try to fill in code to fulfill the request! Here is a variable used in the excercises.
aListOfNumbers = [6, 3, 4, 5, 7, 8, 9 ]
Write a function that returns the length of aListOfNumbers as well as the maximum value. Hint: max() is a built-in function
## try here!