Code

Potion Pricing

Functions get really useful when you can give them values and get answers back.

def add(a, b):
    return a + b

total = add(2, 3)
print(total)   # 5
  • a and b are parameters: placeholders listed in the def.
  • 2 and 3 are arguments: the real values you pass in.
  • return sends a value back to whoever called the function. The call add(2, 3) is then replaced by that value, so you can store it, print it or use it in more maths.

return also ends the function immediately. Any lines after it in the body are skipped.

Assignment

  1. Complete potion_cost(bottles, price) so it returns the total cost: bottles times price.
  2. Complete change_due(paid, cost) so it returns how many coins the buyer gets back: paid minus cost.