Describe a function’s contract
Before writing the body, decide what the function receives and what it gives back. For visit_cost(people, member), the first parameter represents a non-negative integer and the second represents a Boolean. The function returns the total as a number. It does not need to ask the user a question. The caller can read a value, convert it, and decide whether input() == "Y" before it calls the function. This division makes both parts easier to inspect and test.
A function definition names the steps. Python does not execute the body just because it has seen def. A call such as visit_cost(2, True) supplies two arguments. The names people and member are local to that call. Another call can use different values without changing the first result. Give function and parameter names that communicate their purpose, especially when several functions are used in the same file.
Trace the call and return
def booking_total(tickets, price):
if tickets == 0:
return 0
total = tickets * price + 2
return total
price = 5
first = booking_total(3, price)
second = booking_total(0, price)
print(first, second)
| Call | Local tickets | Local price | Path | Returned value |
|---|---|---|---|---|
| First | 3 | 5 | Multiply, then add fee | 17 |
| Second | 0 | 5 | Return immediately | 0 |
For the first call, the argument price is evaluated in the caller before the value is passed to the local parameter. Python returns 17 and assigns it to first. For the second call, the first return ends the function before it calculates a fee. The caller then prints 17 0. A trace column labelled “return” is useful when several functions call each other, because you can bring the value back to the exact expression waiting for it.
Avoid dependencies that make testing hard
A global value is defined outside a function. A function may read it, but a parameter is clearer when the value is an input to the calculation. For example, a delivery function that uses subtotal from outside its body only works as intended when someone has set that global value. Give the function a parameter instead, then test it with a few arguments you choose. To change a local variable, assign to it inside the function. The caller does not see the local name. Avoid using a global statement simply to get a value out. Return it explicitly.
When you find a None result where a number is expected, look at every possible path through the function. A missing return can affect only one branch. print(value) displays something and returns None. return value hands the value back and leaves the choice of display to the caller. This distinction also matters when a function helps build a report file.
Practise a Paper 2 change request
A venue charges £4 per adult and £2 per child. Bookings with at least one ticket have a £3 fee. Modify the program above so it has a function booking_total(adults, children) returning the total. Read the two counts in the caller and print exactly one integer. Use cases with no tickets, adults only, children only and a mixture. Before running, predict the outcome for three adults and two children. In the sandbox, use one count per line and Run with my input to check this separate brief.
When checking a changed function, call it twice with different inputs. This helps catch a fixed answer or an unintended dependence on a previous call.