Article
Built-in maths procedures and constants
How would you find the square root of 2025? You could invent a method, or you could ask a procedure that someone else already wrote and tested. Programmers do the second thing all the time. This article shows how to use ready-made maths tools, and how the exam describes them.
Procedures that are already there
A procedure is a named group of statements that does one job. To use it, you call it: write its name, then brackets. The values you put inside the brackets are its arguments. Many procedures return a value, which means they hand a result back to the program.
You already know one procedure: DISPLAY(x). It takes one argument and shows it. (It does not hand anything back.) Python comes with many maths procedures. Some are always ready, and the ones with math. in front need import math at the top of the program, which brings in a library, a collection of ready-made procedures.
| Procedure (Python) | What it returns | Example | Result |
|---|---|---|---|
abs(x) | x without its minus sign | abs(-7) | 7 |
round(x) | x rounded to the nearest whole number | round(3.67) | 4 |
round(x, 2) | x rounded to 2 decimal places | round(3.14159, 2) | 3.14 |
max(a, b) | the larger of two values | max(3, 9) | 9 |
min(a, b) | the smaller of two values | min(3, 9) | 3 |
math.sqrt(x) | the square root of x | math.sqrt(16) | 4.0 |
math.floor(x) | x rounded down | math.floor(3.9) | 3 |
math.ceil(x) | x rounded up | math.ceil(3.1) | 4 |
Run them and try your own numbers:
A call that returns a value can be used anywhere a value can be used. The computer works out the call first and puts the answer in its place:
x ← abs(-7) + 2 DISPLAY(x)
The call abs(-7) becomes 7, so x is 9 and the program shows 9.
You must use the returned value. A call on its own does not change anything. This Python program shows the mistake:
The first print shows 3.67, because round(price) returned 4 and nothing stored it. After price = round(price), the variable holds 4. (Python rounds a value exactly halfway, such as 2.5, to the nearest even number, so round(2.5) is 2. The exam always says how to round, so you will not be tricked by this.)
Procedures described in words
On the exam you often meet a procedure whose code you cannot see. The question describes it in a sentence, such as: "The procedure larger(a, b) returns the larger of a and b." That sentence is everything you need. You do not need to know how it works, only what goes in and what comes out.
Using a procedure you cannot see inside
Arguments go in
for example 4 and 9
callThe procedure does its job
you cannot see how
returnA value comes back
for example 9
Use these rules to evaluate an expression with calls:
- Work out the arguments first.
- Replace each call with the value it returns.
- If calls are inside other calls, start with the innermost one.
- Then do the rest of the expression in the usual order.
For example, with the larger procedure above:
x ← larger(4, 9) + larger(2, 1) y ← larger(larger(3, 8), 5)
For x: larger(4, 9) is 9 and larger(2, 1) is 2, so x is 9 + 2 = 11. For y: the inside call larger(3, 8) is 8, then larger(8, 5) is 8, so y is 8.
Check your understanding
Assume that the procedure larger(a, b) returns the larger of a and b. What is the value of x after this statement runs?
x ← larger(4, 9) + larger(2, 1)
Constants
A constant is a value that never changes while the program runs. Maths has famous ones. In Python, math.pi holds the value of pi and math.e holds another famous number:
A programmer can also make their own constant, for a value that should stay the same, such as a tax rate. By habit its name is written in capital letters:
TAX_RATE ← 0.25 price ← 50 tax ← price * TAX_RATE DISPLAY(tax)
The program shows 12.5. Python does not force a constant to stay the same. The capital letters are a signal to the people reading the code: "do not change this". Constants help in two ways:
- A name explains a number.
price * TAX_RATEis clearer thanprice * 0.25. - One place to change. If the rate becomes 0.3, you change one line and every calculation updates.
Check your understanding
A game program uses the number of lives, 3, in ten different places. The programmer stores it as MAX_LIVES ← 3 and uses MAX_LIVES everywhere. The rules change to 5 lives. What is the easiest correct change?
Worked example: the cost of a fence
A circular garden has a radius of 4 metres. Fencing costs 3 per metre. How much does the fence cost, rounded to the nearest whole number?
FENCE_PRICE ← 3 radius ← 4 length ← 2 * PI * radius cost ← round(length * FENCE_PRICE) DISPLAY(cost)
On the exam, a question like this would first say what PI and round are, for example "PI is the constant 3.14159" and "round(x) returns x rounded to the nearest whole number". Then trace it:
| Statement | Value |
|---|---|
length ← 2 * PI * radius | 2 * 3.14159 * 4 is about 25.13 |
cost ← round(length * FENCE_PRICE) | 25.13 * 3 is about 75.40, which rounds to 75 |
The program shows 75.
Common mistakes
- Forgetting
import mathin Python.math.sqrt(16)fails with an error unlessimport mathcomes first.abs,round,maxandmindo not need it. - Calling a procedure without using what it returns.
round(price)alone changes nothing. Store the answer:price ← round(price). - Leaving out the brackets.
abs -7is not a call. A call needs(and).
On the exam
The exam describes unfamiliar procedures in words, such as "returns the larger of the two values" or "returns the square root", and asks you to evaluate expressions that use them. Work inside out and replace every call with its returned value. Only RANDOM(a, b) is on the reference sheet, and it includes both a and b. When a constant is used, the question tells you its value.
Key words
Procedure: a named group of statements that does one job.
Call: use a procedure by writing its name and brackets.
Argument: a value you give to a procedure inside the brackets.
Return value: the result a procedure hands back.
Library: a collection of ready-made procedures.
Constant: a value that does not change while the program runs.