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.

sum.py
Replay: real traced execution (multi-file project)
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)

  1. 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]
  2. total ← 0

    pass 1 of 2
    1def sum(arr[10, 20, 30]):2    total→ 0 = 03    for i in range(len(arr)):
  3. total ← 10

    pass 1 of 8
    2total = 03for i0 in range(len(arr[10, 20, 30])):4    total→ 10 = total + arr[i]105#?return_value
    All 8 passes — pass 1 is the card above
    passiarrarr[i]total
    10[10, 20, 30]100 10
    21[10, 20, 30]2010 30
    32[10, 20, 30]3030 60
    40[1, 2, 3, 4, 5]10 1
    51[1, 2, 3, 4, 5]21 3
    62[1, 2, 3, 4, 5]33 6
    73[1, 2, 3, 4, 5]46 10
    84[1, 2, 3, 4, 5]510 15
  4. return total

    5#?return_value6return total60
  5. result1 ← 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]
  6. total ← 0

    pass 2 of 2
    1def sum(arr[1, 2, 3, 4, 5]):2    total→ 0 = 03    for i in range(len(arr)):
  7. return total

    5#?return_value6return total15
  8. result2 ← 15

    14print("nums2=" + str(nums2))15result2→ 15 = sum(nums2[1, 2, 3, 4, 5])

The function takes a list, returns the sum.

def `def name(parameters):` defines a function in Python.
parameter Input values passed to a function when calling it.
return The value a function sends back to the caller.

Temperature conversion

Convert between Fahrenheit and Celsius. Two functions, opposite directions.

example
temperature.py
Replay: real traced execution (multi-file project)
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)
  1. f1 ← 98.6

    7f1→ 98.6 = 98.6  #@f1=32, 2128c1 = fahrenheit_to_celsius(f198.6)
  2. def fahrenheit_to_celsius(f):

    1def fahrenheit_to_celsius(f98.6):2    return (f98.6 - 32) * 5 / 9
  3. c1 ← 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)
  4. def celsius_to_fahrenheit(c):

    4def celsius_to_fahrenheit(c0):5    return c0 * 9 / 5 + 32
  5. f2 ← 32.0

    10c2 = 0  #@c2=100, 3711f2→ 32.0 = celsius_to_fahrenheit(c20)
  1. f1 ← 32

    7f1→ 32 = 328c1 = fahrenheit_to_celsius(f132)
  2. def fahrenheit_to_celsius(f):

    1def fahrenheit_to_celsius(f32):2    return (f32 - 32) * 5 / 9
  3. c1 ← 0.0, c2 ← 0

    7f1 = 328c1→ 0.0 = fahrenheit_to_celsius(f132)910c2→ 0 = 011f2 = celsius_to_fahrenheit(c20)
  4. def celsius_to_fahrenheit(c):

    4def celsius_to_fahrenheit(c0):5    return c0 * 9 / 5 + 32
  5. f2 ← 32.0

    10c2 = 011f2→ 32.0 = celsius_to_fahrenheit(c20)
  1. f1 ← 212

    7f1→ 212 = 2128c1 = fahrenheit_to_celsius(f1212)
  2. def fahrenheit_to_celsius(f):

    1def fahrenheit_to_celsius(f212):2    return (f212 - 32) * 5 / 9
  3. c1 ← 100.0, c2 ← 0

    7f1 = 2128c1→ 100.0 = fahrenheit_to_celsius(f1212)910c2→ 0 = 011f2 = celsius_to_fahrenheit(c20)
  4. def celsius_to_fahrenheit(c):

    4def celsius_to_fahrenheit(c0):5    return c0 * 9 / 5 + 32
  5. f2 ← 32.0

    10c2 = 011f2→ 32.0 = celsius_to_fahrenheit(c20)
  1. f1 ← 98.6

    7f1→ 98.6 = 98.68c1 = fahrenheit_to_celsius(f198.6)
  2. def fahrenheit_to_celsius(f):

    1def fahrenheit_to_celsius(f98.6):2    return (f98.6 - 32) * 5 / 9
  3. c1 ← 37.0, c2 ← 37

    7f1 = 98.68c1→ 37.0 = fahrenheit_to_celsius(f198.6)910c2→ 37 = 3711f2 = celsius_to_fahrenheit(c237)
  4. def celsius_to_fahrenheit(c):

    4def celsius_to_fahrenheit(c37):5    return c37 * 9 / 5 + 32
  5. f2 ← 98.6

    10c2 = 3711f2→ 98.6 = celsius_to_fahrenheit(c237)
  1. f1 ← 98.6

    7f1→ 98.6 = 98.68c1 = fahrenheit_to_celsius(f198.6)
  2. def fahrenheit_to_celsius(f):

    1def fahrenheit_to_celsius(f98.6):2    return (f98.6 - 32) * 5 / 9
  3. c1 ← 37.0, c2 ← 100

    7f1 = 98.68c1→ 37.0 = fahrenheit_to_celsius(f198.6)910c2→ 100 = 10011f2 = celsius_to_fahrenheit(c2100)
  4. def celsius_to_fahrenheit(c):

    4def celsius_to_fahrenheit(c100):5    return c100 * 9 / 5 + 32
  5. f2 ← 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.

n
fibonacci.py
Replay: real traced execution (multi-file project)
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))
  1. n ← 10

    11n→ 10 = 10  #@n=5, 1512result = fibonacci(n10)13print("result=" + str(result))
  2. 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 = 1
  3. fib[i] ← 1

    pass 1 of 8
    6for i2 in range(2, n10):7    fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0
    All 8 passes — pass 1 is the card above
    passifib[i - 1]fib[i - 2]fib[i]
    12101
    23112
    34213
    45325
    56538
    678513
    7813821
    89211334
  4. return fib

    9return fib[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
  5. 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]
  1. n ← 5

    11n→ 5 = 512result = fibonacci(n5)13print("result=" + str(result))
  2. 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 = 1
  3. fib[i] ← 1

    pass 1 of 3
    6for i2 in range(2, n5):7    fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0
    All 3 passes — pass 1 is the card above
    passifib[i - 1]fib[i - 2]fib[i]
    12101
    23112
    34213
  4. return fib

    9return fib[0, 1, 1, 2, 3]
  5. 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]
  1. n ← 15

    11n→ 15 = 1512result = fibonacci(n15)13print("result=" + str(result))
  2. 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 = 1
  3. fib[i] ← 1

    pass 1 of 13
    6for i2 in range(2, n15):7    fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0
    13 passes — pass 1 is the card above
    passifib[i - 1]fib[i - 2]fib[i]
    12101
    23112
    34213
    45325
    56538
    678513
    7813821
    89211334
    910342155
    ⋯ 2 more passes ⋯
    121314489233
    1314233144377
  4. return fib

    9return fib[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
  5. 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.

max.py
Replay: real traced execution (multi-file project)
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)
  1. 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]
  2. 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)):
  3. for i in range(1, len(arr)):

    pass 1 of 6
    2result = 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
    passi
    11
    22
    33
    44
    55
    66
  4. result ← 45

    pass 1 of 3
    3for i in range(1, len(arr)):4    if arr[i]45 > result23:5        result→ 45 = arr[i]456return result
    All 3 passes — pass 1 is the card above
    passarr[i]result
    14523 45
    26745 67
    38967 89
  5. return result

    5        result = arr[i]6return result89
  6. m ← 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.

combined.py
Replay: real traced execution (multi-file project)
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)
  1. 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]
  2. total ← 0

    pass 1 of 2
    1def sum(arr[85, 92, 78, 95, 88]):2    total→ 0 = 03    for i in range(len(arr)):
  3. total ← 85

    pass 1 of 10
    2total = 03for i0 in range(len(arr[85, 92, 78, 95, 88])):4    total→ 85 = total + arr[i]855return total
    All 10 passes — pass 1 is the card above
    passiarr[i]total
    10850 85
    219285 177
    3278177 255
    4395255 350
    5488350 438
    60850 85
    719285 177
    8278177 255
    9395255 350
    10488350 438
  4. return total

    4    total = total + arr[i]5return total438
  5. total ← 438

    12print("scores=" + str(scores))13total→ 438 = sum(scores[85, 92, 78, 95, 88])14avg = average(scores[85, 92, 78, 95, 88])
  6. def average(arr):

    7def average(arr[85, 92, 78, 95, 88]):8    return sum(arr[85, 92, 78, 95, 88]) / len(arr)
  7. total ← 0

    pass 2 of 2
    1def sum(arr[85, 92, 78, 95, 88]):2    total→ 0 = 03    for i in range(len(arr)):
  8. return total

    4    total = total + arr[i]5return total438
  9. avg ← 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.