28.1 What are Built-in Functions?
- Built-in functions are pre-defined functions provided by the pseudocode language.
- They perform common operations without needing to write custom code.
- IGCSE level built-in functions include
ROUND()andRANDOM(). - They RETURN a value — so they can be used in expressions or assigned to a variable.
- Syntax:
Result <- FUNCTION(parameters)
Key idea: A function returns a value — that is what makes it different from a statement like OUTPUT. Always assign the result with <-.
DECLARE Result : REAL
Result <- ROUND(3.14159, 2)
OUTPUT Result // Outputs: 3.14What are Built-in Functions?
28.2 The ROUND Function
ROUND(value, decimalPlaces) rounds a number to a specified number of decimal places and returns a REAL value.
DECLARE Price : REAL
DECLARE RoundedPrice : REAL
Price <- 19.987
RoundedPrice <- ROUND(Price, 2)
OUTPUT RoundedPrice // Outputs: 19.99More ROUND examples
ROUND(3.14159, 2)returns 3.14ROUND(3.14159, 4)returns 3.1416ROUND(7.5, 0)returns 8ROUND(1234.5678, 1)returns 1234.6
Common uses: rounding currency to 2 decimal places, rounding measurements, displaying results without long decimal tails.
The ROUND Function
28.3 The RANDOM Function
RANDOM() generates a random REAL number between 0 and 1 (inclusive). It takes no parameters and returns a REAL value such as 0.347, 0.892 or 0.001.
DECLARE R : REAL
R <- RANDOM()
OUTPUT R // Outputs something like: 0.4231Combine with ROUND + arithmetic for random integers
- Random 0–9:
ROUND(RANDOM() * 9, 0) - Random 1–10:
ROUND(RANDOM() * 9, 0) + 1 - Random 1–100:
ROUND(RANDOM() * 99, 0) + 1 - Random 1–6 (dice):
ROUND(RANDOM() * 5, 0) + 1
Watch out: RANDOM() takes no parameters — writing RANDOM(6) is wrong. To get a 1–6 dice value you must use the arithmetic pattern above.
The RANDOM Function
28.4 Generating Random Integers
RANDOM() gives a value between 0 and 1, but we often need whole numbers in a specific range. Two equivalent formulas:
Using INT
INT(RANDOM() * (max - min + 1)) + minUsing ROUND
ROUND(RANDOM() * (max - min - 1), 0) + minExample: Dice roll (1–6)
DECLARE DiceRoll : INTEGER
DiceRoll <- ROUND(RANDOM() * 5, 0) + 1
OUTPUT "You rolled: ", DiceRollExample: Random lottery number (1–49)
DECLARE LotteryNum : INTEGER
LotteryNum <- ROUND(RANDOM() * 48, 0) + 1
OUTPUT "Lottery number: ", LotteryNumRule of thumb: To get the multiplier for a range [min, max], use (max - min) with ROUND (then + min) or (max - min + 1) with INT (then + min).
Generating Random Integers
28.5 Practical Examples
Five short examples that show ROUND and RANDOM in action.
Example 1 — Round a student's average to 2 decimal places
DECLARE Average : REAL
Average <- 87.4567
OUTPUT "Average: ", ROUND(Average, 2) // Outputs: Average: 87.46Example 2 — Generate a random dice roll (1–6)
DECLARE DiceRoll : INTEGER
DiceRoll <- ROUND(RANDOM() * 5, 0) + 1
OUTPUT "You rolled: ", DiceRollExample 3 — Generate a random percentage (0–100)
DECLARE Percent : INTEGER
Percent <- ROUND(RANDOM() * 100, 0)
OUTPUT "Score: ", Percent, "%"Example 4 — Round currency to 2 decimal places for display
DECLARE Total : REAL
Total <- 19.987
OUTPUT "$", ROUND(Total, 2) // Outputs: $19.99Example 5 — Simulate a coin toss (random 0 or 1)
DECLARE Coin : INTEGER
Coin <- ROUND(RANDOM(), 0)
IF Coin = 0 THEN
OUTPUT "Heads"
ELSE
OUTPUT "Tails"
ENDIFExam tip: When a question asks you to "simulate" a dice, coin or lottery, reach for RANDOM() and combine with ROUND() using the range formula.
Practical Examples
28.6 Key Points Summary
Recap of everything you need to remember about ROUND and RANDOM.