B2.3.4 · Programming constructs
Functions and modularization
Nobody runs a whole school fete cake stall alone, one person takes money, one bags the cakes, one restocks the tray. Splitting a program into named, reusable jobs is the same idea.
Explainer
Every time the cake stall serves a customer, the same three steps happen: work out the price, take the payment, hand over the cake. Writing those three steps out fresh for every single customer would be exhausting and error-prone. Writing them once, giving that block a name, and reusing it for every customer, is what a function is for.
A function is a named, reusable block of code that can take parameters (inputs) and, once it finishes, returns a value. A procedure is the same idea without a return, it might print something or update a variable, but it hands nothing back.
Parameters are the placeholder names written in the function's own definition. Arguments are the real values actually supplied when the function is called. One function with parameters can be called many times, with different arguments each time.
Explainer
Why splitting a program into functions helps
Modularization means breaking a program into small, focused, named pieces instead of one long block. Compare working out three customers' totals the two ways:
total1 = 4 * 2.50
print("Total:", total1)
total2 = 2 * 2.50
print("Total:", total2)
total3 = 6 * 2.50
print("Total:", total3)
def calc_total(price, quantity): return price * quantity print("Total:", calc_total(2.50, 4)) print("Total:", calc_total(2.50, 2)) print("Total:", calc_total(2.50, 6))
Both versions produce the same three totals. But if the stall ever needs to change how a total is calculated (say, a bulk discount for 5 or more cakes), the function version only needs that logic written once, inside calc_total, instead of hunting down and fixing every repeated copy.
Explainer
A function's parameters and local variables belong only to that call
Global versus local scope is covered in full in B2.1.1, the short version needed here: a function's parameters, and anything the function creates inside its own body, are local to that one call. They exist only while the function is running, and disappear the moment it returns.
Reassigning a parameter inside a function does not change the variable that was passed in. The parameter is a local name that starts out holding a copy of whatever value the caller passed. Giving that local name a new value only changes the local copy, the caller's own variable is untouched.
Explainer
The real benefits, applied to the cake stall
Naming a benefit of modularization in the abstract isn't enough to score well, each one has to be tied to what it actually looks like in a specific program.
| Benefit | What it looks like at the cake stall |
|---|---|
| Reusability | calc_total is written once and called for every customer, instead of retyped for each one. |
| Maintainability | Adding a bulk discount means editing one function, not searching the whole program for every place a total was calculated. |
| Readability | A line reading calc_total(price, quantity) says what it does; the three lines of arithmetic it hides don't have to be re-read every time. |
| Team development | One person can write and test calc_total while somebody else works on the stock-tracking function, without either one touching the other's code. |
Worked example
One function, three different orders
The same calc_total function, called with three different pairs of arguments. This is exactly what "different inputs" means: one reusable block, many outcomes.
Step 1. A customer buys 4 cupcakes at $2.50 each.
Step 2. The next customer only wants 2. Same function, same body, different arguments.
Step 3. A family buys 6, for a school event. The function's own code never changed between any of these three calls.
Try it yourself
Run the cake stall functions
Change the arguments in any of the three calc_total calls, or add a fourth one of your own, and press Run.
Running in offline fallback mode. The full Python engine couldn't load (blocked school network, or no internet), so this console switched automatically to a small hand-written engine. It supports this page's functions and parameters, but it isn't full Python.
Worked example
Why the discount didn't stick
The stall wants a function that applies a 10% discount to a price. Someone writes it so it reassigns the parameter directly, expecting that to update the caller's variable.
Wrong: reassigning the parameter, with no return.
def apply_discount(price): price = price * 0.9 # only changes the LOCAL copy of price ticket_price = 8.00 apply_discount(ticket_price) print("Price after discount:", ticket_price)
This prints Price after discount: 8.0, completely unchanged. Inside apply_discount, price is a local name that started out as a copy of 8.00. Reassigning it to 7.20 only changed that local copy, ticket_price back in the caller was never touched.
Right: return the new value, and let the caller reassign its own variable.
def apply_discount(price): return price * 0.9 ticket_price = 8.00 ticket_price = apply_discount(ticket_price) print("Price after discount:", ticket_price)
Now it prints Price after discount: 7.2. The function still never touches the caller's variable directly, but this time the caller takes the returned value and reassigns ticket_price itself.
Check your understanding
A function is defined as def double(n): n = n * 2, with no return. A program then runs x = 5 followed by double(x), then print(x). What gets printed?
Common mistake
RecurringQuestions asking for an advantage of modularity are frequently answered with a generic, textbook-sounding line that never actually engages with the program in the question. Naming the right idea in the abstract is not enough, the answer has to say what it looks like in that specific scenario.
Vague, generic: "Using functions makes the code more organized and reusable." True, but it could be pasted onto any program on the exam paper, it says nothing about the cake stall specifically.
Applied to the actual scenario: "Because the total is calculated inside calc_total, adding a bulk discount later only requires changing that one function, rather than finding and editing every place a total was calculated across the program." Names the same benefit, maintainability, but ties it to a concrete change in this specific program.
Revision questions
Past-paper style. Write an answer, then reveal the marking points and mark yourself.
[2]Distinguish between a function and a procedure.
Marking points
评分要点
- A function takes inputs and returns a value that can be used in an expression.
- A procedure performs an action (such as printing or updating a display) but does not return a value.
- 函数接受输入并返回一个可用于表达式的值。
- 过程执行一个动作(如打印或更新显示)但不返回值。
[3]Explain why reassigning a function's parameter inside the function body does not change the value of the variable that was passed in as an argument.
Marking points
评分要点
- The function receives a copy of the argument's value in its parameter, not the caller's variable itself.
- Reassigning the parameter changes only the local copy inside the function.
- When the function ends the local copy is discarded, and the caller's original variable still holds its old value.
- 函数在其形参中收到实参值的一个副本,而不是调用者的变量本身。
- 重新给形参赋值只改变函数内部的局部副本。
- 函数结束时局部副本被丢弃,调用者的原始变量仍持有其旧值。
[4]A school fete has three separate stalls, each needing to calculate a total price from a quantity and a unit price. Outline one benefit of writing this as a single shared function rather than three separate blocks of code, applying your answer to this scenario specifically.
Marking points
评分要点
- The calculation is written, tested and debugged in one place instead of three.
- A fix or change is made once and applies everywhere the function is called, so the three stalls cannot drift apart.
- The main code is shorter and clearer, and the function can be reused by any future stall.
- Any one specific benefit clearly stated earns the mark.
- 计算在一处编写、测试和调试,而不是三处。
- 修复或改动只做一次,并应用到所有调用该函数的地方,所以三个摊位不会彼此偏离。
- 主代码更短更清晰,而且该函数可以被任何未来的摊位复用。
- 清楚陈述任何一个具体好处即可得分。
Flashcards
Active recall for this statement’s key terms.
Keep this statement’s practice with you
Free account: unlock every revision question and flashcard for B2.3.4, and every other IB CS 2027 statement.