Foundations
Functions
Reusable Code
Your calculator app needs to convert temperatures in three different places.
Instead of copying the formula three times, you create a function to_celsius(f)
and call it wherever needed. Fix a bug once, it's fixed everywhere.
Sum of list function
Wrap the sum logic in a reusable function.
def sum(arr):
total = 0
for i in range(len(arr)):
total = total + arr[i]
return total
nums1 = [10, 20, 30]
nums2 = [1, 2, 3, 4, 5]
print("nums1=" + str(nums1))
result1 = sum(nums1)
print("nums2=" + str(nums2))
result2 = sum(nums2)
nums1 ← [10, 20, 30], nums2 ← [1, 2, 3, 4, 5]
8nums1→ [10, 20, 30] = [10, 20, 30]9nums2→ [1, 2, 3, 4, 5] = [1, 2, 3, 4, 5]1011print("nums1=" + str(nums1[10, 20, 30]))12result1 = sum(nums1[10, 20, 30])outputnums1=[10, 20, 30]total ← 0
pass 1 of 21def sum(arr[10, 20, 30]):2 total→ 0 = 03 for i in range(len(arr)):total ← 10
pass 1 of 82total = 03for i0 in range(len(arr[10, 20, 30])):4 total→ 10 = total + arr[i]105#?return_valueAll 8 passes — pass 1 is the card above pass iarrarr[i]total1 0 [10, 20, 30] 10 0 → 10 2 1 [10, 20, 30] 20 10 → 30 3 2 [10, 20, 30] 30 30 → 60 4 0 [1, 2, 3, 4, 5] 1 0 → 1 5 1 [1, 2, 3, 4, 5] 2 1 → 3 6 2 [1, 2, 3, 4, 5] 3 3 → 6 7 3 [1, 2, 3, 4, 5] 4 6 → 10 8 4 [1, 2, 3, 4, 5] 5 10 → 15 return total
5#?return_value6return total60result1 ← 60
11print("nums1=" + str(nums1))12result1→ 60 = sum(nums1[10, 20, 30])1314print("nums2=" + str(nums2[1, 2, 3, 4, 5]))15result2 = sum(nums2[1, 2, 3, 4, 5])outputnums2=[1, 2, 3, 4, 5]total ← 0
pass 2 of 21def sum(arr[1, 2, 3, 4, 5]):2 total→ 0 = 03 for i in range(len(arr)):return total
5#?return_value6return total15result2 ← 15
14print("nums2=" + str(nums2))15result2→ 15 = sum(nums2[1, 2, 3, 4, 5])
The function takes a list, returns the sum.
Temperature conversion
Convert between Fahrenheit and Celsius. Two functions, opposite directions.
def fahrenheit_to_celsius(f):
return (f - 32) * 5 / 9
def celsius_to_fahrenheit(c):
return c * 9 / 5 + 32
f1 = 98.6
c1 = fahrenheit_to_celsius(f1)
c2 = 0
f2 = celsius_to_fahrenheit(c2)
def fahrenheit_to_celsius(f):
return (f - 32) * 5 / 9
def celsius_to_fahrenheit(c):
return c * 9 / 5 + 32
f1 = 32
c1 = fahrenheit_to_celsius(f1)
c2 = 0
f2 = celsius_to_fahrenheit(c2)
def fahrenheit_to_celsius(f):
return (f - 32) * 5 / 9
def celsius_to_fahrenheit(c):
return c * 9 / 5 + 32
f1 = 212
c1 = fahrenheit_to_celsius(f1)
c2 = 0
f2 = celsius_to_fahrenheit(c2)
def fahrenheit_to_celsius(f):
return (f - 32) * 5 / 9
def celsius_to_fahrenheit(c):
return c * 9 / 5 + 32
f1 = 98.6
c1 = fahrenheit_to_celsius(f1)
c2 = 37
f2 = celsius_to_fahrenheit(c2)
def fahrenheit_to_celsius(f):
return (f - 32) * 5 / 9
def celsius_to_fahrenheit(c):
return c * 9 / 5 + 32
f1 = 98.6
c1 = fahrenheit_to_celsius(f1)
c2 = 100
f2 = celsius_to_fahrenheit(c2)
f1 ← 98.6
7f1→ 98.6 = 98.6 #@f1=32, 2128c1 = fahrenheit_to_celsius(f198.6)def fahrenheit_to_celsius(f):
1def fahrenheit_to_celsius(f98.6):2 return (f98.6 - 32) * 5 / 9c1 ← 37.0, c2 ← 0
7f1 = 98.6 #@f1=32, 2128c1→ 37.0 = fahrenheit_to_celsius(f198.6)910c2→ 0 = 0 #@c2=100, 3711f2 = celsius_to_fahrenheit(c20)def celsius_to_fahrenheit(c):
4def celsius_to_fahrenheit(c0):5 return c0 * 9 / 5 + 32f2 ← 32.0
10c2 = 0 #@c2=100, 3711f2→ 32.0 = celsius_to_fahrenheit(c20)
f1 ← 32
7f1→ 32 = 328c1 = fahrenheit_to_celsius(f132)def fahrenheit_to_celsius(f):
1def fahrenheit_to_celsius(f32):2 return (f32 - 32) * 5 / 9c1 ← 0.0, c2 ← 0
7f1 = 328c1→ 0.0 = fahrenheit_to_celsius(f132)910c2→ 0 = 011f2 = celsius_to_fahrenheit(c20)def celsius_to_fahrenheit(c):
4def celsius_to_fahrenheit(c0):5 return c0 * 9 / 5 + 32f2 ← 32.0
10c2 = 011f2→ 32.0 = celsius_to_fahrenheit(c20)
f1 ← 212
7f1→ 212 = 2128c1 = fahrenheit_to_celsius(f1212)def fahrenheit_to_celsius(f):
1def fahrenheit_to_celsius(f212):2 return (f212 - 32) * 5 / 9c1 ← 100.0, c2 ← 0
7f1 = 2128c1→ 100.0 = fahrenheit_to_celsius(f1212)910c2→ 0 = 011f2 = celsius_to_fahrenheit(c20)def celsius_to_fahrenheit(c):
4def celsius_to_fahrenheit(c0):5 return c0 * 9 / 5 + 32f2 ← 32.0
10c2 = 011f2→ 32.0 = celsius_to_fahrenheit(c20)
f1 ← 98.6
7f1→ 98.6 = 98.68c1 = fahrenheit_to_celsius(f198.6)def fahrenheit_to_celsius(f):
1def fahrenheit_to_celsius(f98.6):2 return (f98.6 - 32) * 5 / 9c1 ← 37.0, c2 ← 37
7f1 = 98.68c1→ 37.0 = fahrenheit_to_celsius(f198.6)910c2→ 37 = 3711f2 = celsius_to_fahrenheit(c237)def celsius_to_fahrenheit(c):
4def celsius_to_fahrenheit(c37):5 return c37 * 9 / 5 + 32f2 ← 98.6
10c2 = 3711f2→ 98.6 = celsius_to_fahrenheit(c237)
f1 ← 98.6
7f1→ 98.6 = 98.68c1 = fahrenheit_to_celsius(f198.6)def fahrenheit_to_celsius(f):
1def fahrenheit_to_celsius(f98.6):2 return (f98.6 - 32) * 5 / 9c1 ← 37.0, c2 ← 100
7f1 = 98.68c1→ 37.0 = fahrenheit_to_celsius(f198.6)910c2→ 100 = 10011f2 = celsius_to_fahrenheit(c2100)def celsius_to_fahrenheit(c):
4def celsius_to_fahrenheit(c100):5 return c100 * 9 / 5 + 32f2 ← 212.0
10c2 = 10011f2→ 212.0 = celsius_to_fahrenheit(c2100)
Functions can take one value and return another. Each function does one specific job.
Fibonacci function
Get the first n Fibonacci numbers.
def fibonacci(n):
fib = [0] * n
fib[0] = 0
fib[1] = 1
for i in range(2, n):
fib[i] = fib[i - 1] + fib[i - 2]
return fib
n = 10
result = fibonacci(n)
print("result=" + str(result))
def fibonacci(n):
fib = [0] * n
fib[0] = 0
fib[1] = 1
for i in range(2, n):
fib[i] = fib[i - 1] + fib[i - 2]
return fib
n = 5
result = fibonacci(n)
print("result=" + str(result))
def fibonacci(n):
fib = [0] * n
fib[0] = 0
fib[1] = 1
for i in range(2, n):
fib[i] = fib[i - 1] + fib[i - 2]
return fib
n = 15
result = fibonacci(n)
print("result=" + str(result))
n ← 10
11n→ 10 = 10 #@n=5, 1512result = fibonacci(n10)13print("result=" + str(result))fib ← [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], fib[0] ← 0, fib[1] ← 1
1def fibonacci(n10):2 fib→ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = [0] * n103 fib[0]→ 0 = 04 fib[1]→ 1 = 1fib[i] ← 1
pass 1 of 86for i2 in range(2, n10):7 fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0All 8 passes — pass 1 is the card above pass ifib[i - 1]fib[i - 2]fib[i]1 2 1 0 1 2 3 1 1 2 3 4 2 1 3 4 5 3 2 5 5 6 5 3 8 6 7 8 5 13 7 8 13 8 21 8 9 21 13 34 return fib
9return fib[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]result ← [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
11n = 10 #@n=5, 1512result→ [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] = fibonacci(n10)13print("result=" + str(result[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]))outputresult=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
n ← 5
11n→ 5 = 512result = fibonacci(n5)13print("result=" + str(result))fib ← [0, 0, 0, 0, 0], fib[0] ← 0, fib[1] ← 1
1def fibonacci(n5):2 fib→ [0, 0, 0, 0, 0] = [0] * n53 fib[0]→ 0 = 04 fib[1]→ 1 = 1fib[i] ← 1
pass 1 of 36for i2 in range(2, n5):7 fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0All 3 passes — pass 1 is the card above pass ifib[i - 1]fib[i - 2]fib[i]1 2 1 0 1 2 3 1 1 2 3 4 2 1 3 return fib
9return fib[0, 1, 1, 2, 3]result ← [0, 1, 1, 2, 3]
11n = 512result→ [0, 1, 1, 2, 3] = fibonacci(n5)13print("result=" + str(result[0, 1, 1, 2, 3]))outputresult=[0, 1, 1, 2, 3]
n ← 15
11n→ 15 = 1512result = fibonacci(n15)13print("result=" + str(result))fib ← [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], fib[0] ← 0
1def fibonacci(n15):2 fib→ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = [0] * n153 fib[0]→ 0 = 04 fib[1]→ 1 = 1fib[i] ← 1
pass 1 of 136for i2 in range(2, n15):7 fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]013 passes — pass 1 is the card above pass ifib[i - 1]fib[i - 2]fib[i]1 2 1 0 1 2 3 1 1 2 3 4 2 1 3 4 5 3 2 5 5 6 5 3 8 6 7 8 5 13 7 8 13 8 21 8 9 21 13 34 9 10 34 21 55 ⋯ 2 more passes ⋯ 12 13 144 89 233 13 14 233 144 377 return fib
9return fib[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]result ← [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
11n = 1512result→ [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377] = fibonacci(n15)13print("result=" + str(result[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]))outputresult=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
The function handles all the list setup and calculation internally. The caller just says "give me 10 Fibonacci numbers."
Find maximum function
Wrap the "find max" logic in a reusable function.
def max(arr):
result = arr[0]
for i in range(1, len(arr)):
if arr[i] > result:
result = arr[i]
return result
nums = [23, 45, 12, 67, 34, 89, 41]
print("nums=" + str(nums))
m = max(nums)
nums ← [23, 45, 12, 67, 34, 89, 41]
8nums→ [23, 45, 12, 67, 34, 89, 41] = [23, 45, 12, 67, 34, 89, 41]910print("nums=" + str(nums[23, 45, 12, 67, 34, 89, 41]))11m = max(nums[23, 45, 12, 67, 34, 89, 41])outputnums=[23, 45, 12, 67, 34, 89, 41]result ← 23
1def max(arr[23, 45, 12, 67, 34, 89, 41]):2 result→ 23 = arr[0]233 for i in range(1, len(arr)):for i in range(1, len(arr)):
pass 1 of 62result = arr[0]3for i1 in range(1, len(arr[23, 45, 12, 67, 34, 89, 41])):4 if arr[i] > result:5 result = arr[i]All 6 passes — pass 1 is the card above pass i1 1 2 2 3 3 4 4 5 5 6 6 result ← 45
pass 1 of 33for i in range(1, len(arr)):4 if arr[i]45 > result23:5 result→ 45 = arr[i]456return resultAll 3 passes — pass 1 is the card above pass arr[i]result1 45 23 → 45 2 67 45 → 67 3 89 67 → 89 return result
5 result = arr[i]6return result89m ← 89
10print("nums=" + str(nums))11m→ 89 = max(nums[23, 45, 12, 67, 34, 89, 41])
Now any code can find the maximum of a list with a single call.
Multiple function calls
Use functions together to solve a problem.
def sum(arr):
total = 0
for i in range(len(arr)):
total = total + arr[i]
return total
def average(arr):
return sum(arr) / len(arr)
scores = [85, 92, 78, 95, 88]
print("scores=" + str(scores))
total = sum(scores)
avg = average(scores)
scores ← [85, 92, 78, 95, 88]
10scores→ [85, 92, 78, 95, 88] = [85, 92, 78, 95, 88]1112print("scores=" + str(scores[85, 92, 78, 95, 88]))13total = sum(scores[85, 92, 78, 95, 88])14avg = average(scores)outputscores=[85, 92, 78, 95, 88]total ← 0
pass 1 of 21def sum(arr[85, 92, 78, 95, 88]):2 total→ 0 = 03 for i in range(len(arr)):total ← 85
pass 1 of 102total = 03for i0 in range(len(arr[85, 92, 78, 95, 88])):4 total→ 85 = total + arr[i]855return totalAll 10 passes — pass 1 is the card above pass iarr[i]total1 0 85 0 → 85 2 1 92 85 → 177 3 2 78 177 → 255 4 3 95 255 → 350 5 4 88 350 → 438 6 0 85 0 → 85 7 1 92 85 → 177 8 2 78 177 → 255 9 3 95 255 → 350 10 4 88 350 → 438 return total
4 total = total + arr[i]5return total438total ← 438
12print("scores=" + str(scores))13total→ 438 = sum(scores[85, 92, 78, 95, 88])14avg = average(scores[85, 92, 78, 95, 88])def average(arr):
7def average(arr[85, 92, 78, 95, 88]):8 return sum(arr[85, 92, 78, 95, 88]) / len(arr)total ← 0
pass 2 of 21def sum(arr[85, 92, 78, 95, 88]):2 total→ 0 = 03 for i in range(len(arr)):return total
4 total = total + arr[i]5return total438avg ← 87.6
13total = sum(scores)14avg→ 87.6 = average(scores[85, 92, 78, 95, 88])
Functions can call other functions. Build complex logic from simple pieces - this is called composition.