Unit 3: Programming · Math

Article

Built-in maths procedures and constants

7 min read

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 returnsExampleResult
abs(x)x without its minus signabs(-7)7
round(x)x rounded to the nearest whole numberround(3.67)4
round(x, 2)x rounded to 2 decimal placesround(3.14159, 2)3.14
max(a, b)the larger of two valuesmax(3, 9)9
min(a, b)the smaller of two valuesmin(3, 9)3
math.sqrt(x)the square root of xmath.sqrt(16)4.0
math.floor(x)x rounded downmath.floor(3.9)3
math.ceil(x)x rounded upmath.ceil(3.1)4

Run them and try your own numbers:

Runs in your browser

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:

AP pseudocode
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:

Runs in your browser

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

  1. Arguments go in

    for example 4 and 9

    call
  2. The procedure does its job

    you cannot see how

    return
  3. A value comes back

    for example 9

Work out the call from its description, then put the returned value in its place.

Use these rules to evaluate an expression with calls:

  1. Work out the arguments first.
  2. Replace each call with the value it returns.
  3. If calls are inside other calls, start with the innermost one.
  4. Then do the rest of the expression in the usual order.

For example, with the larger procedure above:

AP pseudocode
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?

AP pseudocode
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:

Runs in your browser

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:

AP pseudocode
TAX_RATE ← 0.25
price ← 50
tax ← price * TAX_RATE
DISPLAY(tax)
Runs in your browser

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_RATE is clearer than price * 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?

AP pseudocode
FENCE_PRICE ← 3
radius ← 4
length ← 2 * PI * radius
cost ← round(length * FENCE_PRICE)
DISPLAY(cost)
Runs in your browser

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:

StatementValue
length ← 2 * PI * radius2 * 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 math in Python. math.sqrt(16) fails with an error unless import math comes first. abs, round, max and min do 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 -7 is 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.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.