Tutorial 9
🧮
Functions & Procedures - Built-in
0478 IGCSE
Cambridge IGCSE/O-Level pseudocode comes with a few built-in modules (functions or procedures) - for IGCSE/O-Level/O-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 IGCSE/O-Level are:
String Functions
1
LENGTH
Returns the number of characters in a string
Parameters:
- [STRING] - the string to get the length from
2
LCASE
Converts a character or string to lowercase
Parameters:
- [STRING | CHAR] - the string or character to convert to lowercase
3
UCASE
Converts a character or string to uppercase
Parameters:
- [STRING | CHAR] - the string or character to convert to uppercase
4
SUBSTRING
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
Numeric Functions
5
ROUND
Rounds a real number to a given number of decimal places
Parameters:
- [REAL] - the number to round
- [INTEGER] - the number of decimal places to round it to
We might want to specifically round down (floor) or round up (ceiling) to a whole number - to do this, we can +/- 0.5 respectively
6
RANDOM
Returns a real number between 0-1 inclusive
Note: we can multiply and add an offset to generate a random number within a range as shown below
7
Password Length
Create a program that repeatedly asks a user to type in a password, until their password is 8 or more characters
8
Domain Name
A website domain name should be lowercase - ask the user to type in a domain name, output a warning and the correct, lowercase version if the domain they entered isn't lowercase - if the domain was lowercase, then a simple "domain valid" message should be output
9
SHOUTING
Create a program with a mood - it will ask the user their name, then 80% of the time respond "hello [name]" in lowercase, while 20% of the time will SHOUT "HELLO [NAME]" back to them
10
First, Middle & Last Letters
Ask the user to type in their name, then output the first, last and middle (i.e. everything except the first and last) letters of their name
Note: the program above will crash for certain inputs - can you figure out what they are and think how to prevent this?
11
Number Words
Create a program that outputs the length of each number's name from "one" to "nine" along with a message if the number of characters is equal to the value of the number - e.g. the length of "one" is 3, which is not equal to the numeric value 1 that "one" represents
12
PI
Create a program that outputs PI (3.141592653589793) from 0 to 15 decimal places