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)
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 =
c1 = fahrenheit_to_celsius(f1)
c2 =
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 =
c1 = fahrenheit_to_celsius(f1)
c2 =
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 =
c1 = fahrenheit_to_celsius(f1)
c2 =
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 =
c1 = fahrenheit_to_celsius(f1)
c2 =
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 =
c1 = fahrenheit_to_celsius(f1)
c2 =
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 =
c1 = fahrenheit_to_celsius(f1)
c2 =
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 =
c1 = fahrenheit_to_celsius(f1)
c2 =
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 =
c1 = fahrenheit_to_celsius(f1)
c2 =
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 =
c1 = fahrenheit_to_celsius(f1)
c2 =
f2 = celsius_to_fahrenheit(c2)
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 =
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 =
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 =
result = fibonacci(n)
print("result=" + str(result))
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)
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)
Functions can call other functions. Build complex logic from simple pieces - this is called composition.