Tutorial 9
🧮
Functions & Procedures - Built-in
9618 A-Level
Cambridge A-Level 9618 pseudocode comes with many built-in modules (functions or procedures) - for A-Level, the built-in modules are all functions, though custom modules can be created too which we will see in the next tutorial
Many students get confused about the difference between a function a procedure - the difference should be fairly easy to understand with practice (next tutorial :)), but is also outlined briefly below:
- Functions will return a value that can be used to assign, in a calculation, a condition, output statement etc
- Procedures won't return a value - we can output data inside the procedure, but the procedure itself won't evaluate to an answer
The built-in function categories for A-Level are:
Note: functions change slightly over the years. Make sure to check the function insert in the exam
String Functions
1
LENGTH
Returns the number of characters in a string
Parameters:
- [STRING] - the string to get the length from
2
LEFT
Returns a given number of characters starting from the left of the string
Parameters:
- [STRING] - the string to extract the characters from
- [INTEGER] - the number of characters to get
3
MID
Gets a part of a string, starting from a given index position and containing a given number of characters
Parameters:
- [STRING] - the string to extract the substring from
- [INTEGER] - the starting position of the substring
- [INTEGER] - the number of characters to get
4
RIGHT
Returns the rightmost n characters of a string. Note: the characters will still be returned in left to right order
Parameters:
- [STRING] - the string to extract the characters from
- [INTEGER] - the number of characters to get
5
TO_LOWER
Converts a string to lowercase
Parameters:
- [STRING] - the string to convert to lowercase
6
TO_UPPER
Converts a string to uppercase
Parameters:
- [STRING] - the string to convert to uppercase
7
LCASE
Converts a character to lowercase
Parameters:
- [CHAR] - the character to convert to lowercase
8
UCASE
Converts a character to uppercase
Parameters:
- [CHAR] - the character to convert to uppercase
9
NUM_TO_STR
Converts an INTEGER or REAL to a STRING
Parameters:
- [INTEGER | REAL] - the number to convert to a string
10
STR_TO_NUM
Converts a STRING to an INTEGER or REAL
Parameters:
- [STRING] - the string to convert to a numeric value
11
IS_NUM
Returns whether a given string is numeric
Parameters:
- [STRING] - the string to check if numeric
12
ASC
Returns the ASCII code for a given character.
Note: the site extends this to allow full Unicode support for non-Latin characters, emojis, symbols etc
Parameters:
- [CHAR] - the character to get the ASCII code of
13
CHR
Returns the character represented by a given ASCII code
Note: the site extends this to allow full Unicode support for non-Latin characters, emojis, symbols etc
Parameters:
- [INTEGER] - the ASCII code to convert to a character
Numeric Functions
14
INT
Returns the integer part of a number after removing the value after the decimal point
Parameters:
- [REAL] - the real number to extract the integer part from
15
RAND
Returns a random real number in the range 0 to max (not inclusive of max)
Parameters:
- [REAL] - the maximum number to generate (exclusive)
Date Functions
16
DAY
Returns the day value from a date
Parameters:
- [DATE] - the date to extract the day value part from
17
MONTH
Returns the month value from a date
Parameters:
- [DATE] - the date to extract the month value part from
18
YEAR
Returns the year value from a date
Parameters:
- [DATE] - the date to extract the year value part from
19
DAYINDEX
Returns the day index of the week where Sunday = 1, Monday = 2...Saturday = 7
Parameters:
- [DATE] - the date to extract the day index from
20
SETDATE
Returns a date object with values corresponding to the day, month and year parameters
Parameters:
- [INTEGER] - the day value
- [INTEGER] - the month value
- [INTEGER] - the year value
21
TODAY
Returns a date object representing the current day
Note: on older papers, this was called NOW() - check the insert if requiring this function in your exam
File Functions
Note: other file handling operations use keywords, not functions - OPENFILE, WRITEFILE, READFILE. And for A2, SEEK, GETRECORD, PUTRECORD
22
EOF (End of File)
Returns whether there are any more file contents left to read
Parameters:
- [STRING | FILE] - the file to check if the end of file has been reached. Can be specified with or without quotes
Note: we often use EOF within a WHILE loop to read all lines from a file, as shown below:
23
Generate IDs
Create a program can generate 8-character alphanumeric IDs - all characters should be equally likely. An example ID could be "T3nA8x1g"
24
Email Greeting
Create a program that asks for a user to enter their first name then last name - the program will then output the following: "Dear SURNAME, firstname" is uppercase and lowercase respectively
25
AbCdE
Create a program outputs the whole alphabet in alternating case - the output should be on a single line
26
Work Usernames
Work usernames will be in the form of: [1 char for first name].[variable chars for last name].[3 digits] - examples could include: W.Rooney.005, C.Ronaldo.007, P.Scholes.001
You should create a program that can extract and assign each of these 3 parts to a variable. Note: the number should be assigned to a numeric variable, since we might want to do comparisons - e.g. finding who was the first employee
27
Number of Digits
Ask the user to enter an integer, then output how many digits that number has
28
ASCII Quiz
Create a program that generates a random alphanumeric character, asks the user to guess its ASCII code, then tells them if they were right or wrong
29
Random Distribution
Create a program generates 100,000 random integers from 1-10, then outputs a count of how many of each number was generated. You should ensure that an approximately equal distribution of all numbers is achieved
30
Email Greeting
Create a program converts the current date into a full text date - i.e. 26/02/2025 would become "Wednesday 26th, February 2025"
Extension: can you output this information for every day in the year? (you can choose to ignore leap years if you want - or see this leap year challenge
31
CS Quote File
Create a program that can read the following file, then output the data in the form: [quote] - [person]
Note how the lines are in pairs - Alan Turing said the first quote, Grace Hopper said the next and so on. You can assume that there is a valid quote for each person
32
Vowels to Upper
Create a program that asks the user to enter a string, then converts all the vowels to uppercase and everything else to lowercase - i.e. "hello world" would become "hEllO wOrld"