Week 7 Homework: Understanding Functions
Topic: Why we write functions, how to decompose a script into functions, and how functions break
Instructions: This assignment is about understanding functions — reading, tracing, and reasoning about them — not writing new Python code from scratch. Answer in complete sentences where explanations are requested. You may use an AI agent to help you check your reasoning, but you must show your own trace/work for Parts 2 and 3.
Part 1: Why Do We Write Functions? (Conceptual)
Answer the following in 2–4 sentences each.
-
A classmate says, "I don't get why we need functions — I can just write everything in one long script and it works fine." Explain to them, in your own words, at least two concrete problems that show up as a script gets longer, that functions help solve.
-
Suppose you and a partner are building a program together and each of you is responsible for a different function. Explain how functions make it possible for the two of you to work at the same time without constantly stepping on each other's code.
-
One reason we write functions is to make code testable. Explain what this means — why is it easier to check whether a function works correctly than to check whether an entire un-broken-up script works correctly?
-
Give one real-world (non-programming) analogy for what a function does, and explain how the analogy maps onto the ideas of input (parameters), process, and output (return value).
Part 2: Break a Script into Functions
Below is a script that has not been broken into functions. It calculates a student's final grade report from a list of quiz scores.
1 scores: list[int] = [88, 92, 79, 95, 84]
2
3 # Step 1: calculate the total
4 total: int = 0
5 s: int
6 for s in scores:
7 total = total + s
8
9 # Step 2: calculate the average
10 average: float = total / len(scores)
11
12 # Step 3: determine the letter grade
13 if average >= 90:
14 letter: str = "A"
15 elif average >= 80:
16 letter: str = "B"
17 elif average >= 70:
18 letter: str = "C"
19 else:
20 letter: str = "F"
21
22 # Step 4: print a report
23 print("Score total:", total)
24 print("Average:", average)
25 print("Letter grade:", letter)
Your task: Do not rewrite this in Python. Instead, complete the table in the worksheet to describe how you would decompose this script into functions. Think about which lines naturally belong together as a single job.
Follow-up questions:
-
Which step(s), if any, did you decide should not become a separate function? Explain your reasoning.
-
One function in your table probably needs the result of another function as one of its parameters. Identify that dependency: which function's return value feeds into which other function's parameter?
-
Draw (by hand or describe in words) what the final
mainpart of the program would look like — i.e., the sequence of function calls that replaces Steps 1–4. You do not need to write real Python syntax; pseudocode is fine (e.g.,total = calculate_total(scores)).
Part 3: Identify 3 Function Bugs
Below are three separate, broken functions. Each one has exactly one bug, and each bug is a different type (a parameter bug, a return value bug, and a scope/state bug). For each function:
- (a) Trace through the function by hand using the given call, tracking variable values step by step.
- (b) State what the function actually outputs/returns vs. what it should output/return.
- (c) Identify and name the type of bug (e.g., "wrong parameter used," "missing return statement," "used a global variable instead of the parameter," etc.).
- (d) Explain in one sentence how you would fix it — you do not need to rewrite the whole function, just describe the fix.
Bug 1
1 def rectangle_area(length, width):
2 area: int = length * length
3 return area
4
5 print(rectangle_area(4, 7))Bug 2
1 def double_value(number):
2 result: int = number * 2
3 print(result)
4
5 output: int = double_value(5)
6 print("The doubled value is:", output)Bug 3
1 count: int = 0
2
3 def add_to_count(amount):
4 count = count + amount
5 return count
6
7 add_to_count(10)
8 add_to_count(5)
9 print("Total count:", count)
Follow-up question:
- For each of the three bugs above, would running the program crash with an error, or would it run without crashing but produce the wrong answer? Explain why some bugs are "silent" (no error, wrong output) while others would cause a visible error message as well as why silent bugs are often harder to catch.