Software Design & Development · Implementation

SDD10 — Pre-defined Functions

📅 Thu 13 Aug 2026 · P3+P4 (double)
~120 minutes
Learning intentions
Success criteria
Warm up — recap from SDD9
Answer all three questions, then check your answers.
Question 1
What term describes a variable that only exists and can only be accessed inside the sub-program where it was created?
Question 2
Which keyword must be declared inside a function before an existing global variable can be reassigned there?
Question 3
A function reassigns a global variable without first declaring it global. What happens when the function runs?

Key vocabulary

Pre-defined function
A function built into the programming language, ready to use without having to be written from scratch.
Substring
A shorter string taken from within a larger string, either by position (slicing) or by splitting on a delimiter.
ASCII code
The numeric code that represents one specific character in the ASCII character set.
Modulus
The remainder left over after dividing one integer by another, calculated in Python with the % operator.
Type conversion
Changing a value from one data type to another, such as converting a float to an integer with int().

Four more pre-defined functions

What is a pre-defined function?

A pre-defined function is a function built into the programming language itself, ready to use immediately without ever being written or defined by the programmer — this is exactly what separates it from the sub-programs covered in SDD7, which a programmer designs and defines from scratch. National 5 introduced three: random, round, and length (Python's len()). Higher adds four more, each solving a specific, common problem: creating substrings, converting between a character and its ASCII code, converting a float to an integer, and calculating a modulus.

Creating substrings: slicing and split()

A substring is a shorter piece taken from within a larger string. Python offers two distinct ways to create one. Slicing extracts characters by position, using square brackets with a start and end index, e.g. word[0:4] takes the first four characters. A negative index counts backwards from the end, so word[-3:] takes the last three characters. The split() function instead breaks a whole string into a list of substrings wherever a chosen delimiter character appears, e.g. splitting a sentence on a space produces a list of its individual words. Slicing is used when you know the exact position of the substring you want; split() is used when you want to divide a whole string into pieces wherever a particular character occurs.

Converting between characters and ASCII codes

Every character has a numeric ASCII code (covered in CS4). Python provides ord() to convert a single character to its ASCII code, and chr() to convert an ASCII code back to its character. Both take exactly one argument and return the other representation — they are exact inverses of each other.

Converting a float to an integer

The int() function converts a floating-point number to an integer. Critically, int() does not round — it truncates, simply discarding everything after the decimal point, regardless of how large the discarded fraction is. This makes it entirely different from National 5's round() function, which rounds to the nearest whole number. If a problem genuinely requires rounding before converting to a whole number, round() must be applied first, and only then, if needed, int() — using int() alone where rounding was intended is a common source of quietly wrong answers.

Modulus: finding a remainder

The modulus operator, written % in Python, calculates the remainder left over after dividing one integer by another — there is no separate named function for this, unlike the other three. Modulus is frequently used to test whether a number divides evenly into another (a remainder of 0 means it does), to check whether a number is even or odd (number % 2 == 0 is True for even numbers), or to work out how many items are left over once a quantity has been packed into equally-sized groups.

slice / split()
Creates a substring, by position or by delimiter.word[0:4]
sentence.split(" ")
ord() / chr()
Converts a character to its ASCII code, or an ASCII code back to its character.ord("C") → 67
chr(67) → "C"
int()
Converts a float to an integer by truncating — it does not round.int(68.75) → 68
% (modulus)
Calculates the remainder of a division between two integers.23 % 5 → 3

Worked examples

Example 1 — Substrings with slicing and split()
1
Using the SDD6–9 tuck shop item itemName = "Chocolate":
firstFour = itemName[0:4]
lastThree = itemName[-3:]
2
Separately, splitting a whole sentence on its spaces produces a list of words:
sentence = "Higher Computing Science"
words = sentence.split(" ")
firstFour is "Choc", lastThree is "ate", and words is ["Higher", "Computing", "Science"] — slicing picked out characters by position, while split() broke a whole string apart wherever a space occurred.
Example 2 — Converting between a character and its ASCII code
1
itemName = "Chocolate"
firstLetter = itemName[0]
asciiCode = ord(firstLetter)
backToChar = chr(asciiCode)
2
itemName[0] uses slicing (a single index) to extract the first character, "C". ord("C") then converts that character to its ASCII code.
asciiCode is 67, and chr(67) converts it straight back to "C" — confirming ord() and chr() are exact inverses of each other.
Example 3 — int() truncates; it does not round
1
averagePrice = 68.75
wholePence = int(averagePrice)
roundedPence = round(averagePrice)
2
int() simply discards everything after the decimal point of 68.75, no matter how close it is to the next whole number. round(), by contrast, rounds to the nearest whole number.
wholePence is 68 (truncated), while roundedPence is 69 (rounded) — the same starting value produces two different results depending on which function is used.
Example 4 — Modulus for packing items into boxes
1
The tuck shop wants to pack Chocolate bars into boxes of 6:
stockLevel = 29
boxSize = 6
leftover = stockLevel % boxSize
2
29 divided by 6 is 4 whole boxes (24 bars), with some bars left over that don't fill a complete box.
leftover is 5 — 5 bars remain once 4 full boxes of 6 have been packed (4 × 6 = 24, and 29 − 24 = 5).
Now you try
Using itemCount, write a single Python expression using the modulus operator that evaluates to True if itemCount is an even number, and False if it is odd.
⚠️ Common mistakes — examiner feedback
📝 Exam tip

When asked to write code using a pre-defined function, name the exact function required rather than a general description — "use ord()" earns more credit than "convert the letter to a number". When tracing int(), always show the truncation explicitly (e.g. "68.75 becomes 68, not 69") rather than just writing the final answer, since examiners are checking that truncation — not rounding — was understood.

Task Set A — Core questions

Task Set A — Core questions
Work through all questions, then check your answers.
Question 1
What term describes a function built into a programming language, ready to use without being written from scratch?
Question 2
Using word = "Computing", what is the value of word[0:4]?
Question 3
Which of these correctly describes what split() does?
Question 4
What is the value of ord("A")?
Question 5
What is the value of chr(97)?
Question 6
What does int(45.99) evaluate to?
Question 7
What is the value of int(64.86)?
Question 8
What is the value of 23 % 5?
Question 9
Write a Python function called isEven that takes one formal parameter, number, and returns True if number is even, otherwise False, using the modulus operator.
def isEven(number):
    return number % 2 == 0
Question 10
Explain the difference between int() and round() when applied to 68.75, and why confusing the two is a risk. (3 marks)

Task Set B — Extension

Task Set B — Extension · Beyond the specification
Longer written answers — no auto-check. Discuss your answers with your teacher.
Extension 1
SDD11 will use split() to read data from a CSV file. Suggest how split() might be used twice — once with one delimiter, and again with a different delimiter — to turn a whole CSV file's text into individual pieces of data.
Extension 2
Predict the value of -7 % 3 in Python. This may not be the answer you expect — research how Python's modulus operator behaves with negative numbers, and suggest why this could cause a bug if code were transferred from a different programming language.
Extension 3
Suggest one situation where split() would succeed at creating the substrings needed, but slicing alone could not — because the exact positions of the substrings are not known in advance.
📁 File this in OneNote under:
Higher Computing Science → Software Design & Development → SDD10
📌 Teacher notes — not for pupils

Double period — first lesson back after the summer break, and a genuinely dense single-lesson topic (four distinct functions), so the extra time is needed. Consider a short settling-in/retrieval activity before the warm-up given the break since SDD9.

Suggested timing: 5 min warm-up + vocab · 25 min notes + func-grid summary card (live-run each function in the Python shell as it's introduced, rather than only showing static code) · 30 min examples 1–4 · 10 min "now you try" · 40 min Task Set A · Task Set B as homework/extension.

Key misconception: pupils very reliably assume int() rounds, because round() was the only float-to-whole-number tool they met at National 5 — deliberately contrast int(68.75) vs round(68.75) side by side live, not just in the slides.

SQA command words covered: explain, describe, predict, identify, write code.

Extension suggestion: Extension 2 (negative modulus) is a genuine "gotcha" worth demonstrating live in the Python shell for stronger pupils — -7 % 3 surprises almost everyone the first time.

Cross-reference: reviewed a colleague's equivalent file-handling materials (Lessons 7–8) ahead of building SDD11/SDD12 next — confirmed the manual double-split() approach (no csv module) and dataclass dot-notation access are both consistent with this course; flagged one bug in that material (concatenating a non-string dataclass field directly with + without str(), which raises a TypeError) to avoid repeating when SDD12 covers writing CSV output.