Tutorial 9

🧮

Functions & Procedures - Built-in

0984 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:

  1. [STRING] - the string to get the length from
OUTPUT LENGTH("Pseudocode") OUTPUT LENGTH("hello") = LENGTH("world")

2

LCASE

Converts a character or string to lowercase

Parameters:

  1. [STRING | CHAR] - the string or character to convert to lowercase
OUTPUT LCASE("Pseudocode.Pro") OUTPUT LCASE('A')

3

UCASE

Converts a character or string to uppercase

Parameters:

  1. [STRING | CHAR] - the string or character to convert to uppercase
OUTPUT UCASE("Pseudocode.Pro") OUTPUT UCASE('a')

4

SUBSTRING

Gets a part of a string, starting from a given index position and containing a given number of characters

Parameters:

  1. [STRING] - the string to extract the substring from
  2. [INTEGER] - the starting position of the substring
  3. [INTEGER] - the number of characters to get
DECLARE s : STRING s <-- "pseudocode.pro" OUTPUT SUBSTRING(s, 1, 6) OUTPUT SUBSTRING(s, 7, 4) OUTPUT SUBSTRING(s, 12, 3)

Numeric Functions

5

ROUND

Rounds a real number to a given number of decimal places

Parameters:

  1. [REAL] - the number to round
  2. [INTEGER] - the number of decimal places to round it to
CONSTANT PI <-- 3.141592653589793 OUTPUT ROUND(PI, 0) OUTPUT ROUND(PI, 1) OUTPUT ROUND(PI, 2) OUTPUT ROUND(PI, 3) OUTPUT ROUND(PI, 4) OUTPUT ROUND(PI, 5) OUTPUT ROUND(0.999, 0) OUTPUT ROUND(1.49, 1) OUTPUT ROUND(1.51, 1)

We might want to specifically round down (floor) or round up (ceiling) to a whole number - to do this, we can +/- 0.5 respectively

DECLARE a, b : REAL a <-- 1.99 b <-- 2.01 //Round up: OUTPUT ROUND(a + 0.5, 0) OUTPUT ROUND(b + 0.5, 0) //Round down: OUTPUT ROUND(a - 0.5, 0) OUTPUT ROUND(b - 0.5, 0)

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

OUTPUT RANDOM() //0-1 OUTPUT RANDOM() * 5 //0-5 OUTPUT (RANDOM() * 99) + 1 //1-100 OUTPUT ROUND((RANDOM() * 99) + 1, 0) //1-100

7

Password Length

Create a program that repeatedly asks a user to type in a password, until their password is 8 or more characters

DECLARE password : STRING OUTPUT "Choose a new password:" INPUT password WHILE LENGTH(password) < 8 DO OUTPUT "Error - password should be at least 8 characters. Try again:" INPUT password ENDWHILE OUTPUT "Password set successfully!"

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

DECLARE domain : STRING OUTPUT "Enter website domain:" INPUT domain IF domain = LCASE(domain) THEN OUTPUT "Domain valid" ELSE OUTPUT "Domain invalid - should be lowercase: ", LCASE(domain) ENDIF

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

DECLARE name : STRING OUTPUT "What's your name?" INPUT name IF RANDOM() > 0.2 THEN OUTPUT "hello ", LCASE(name) ELSE OUTPUT "HELLO ", UCASE(name) ENDIF

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

DECLARE name : STRING OUTPUT "What's your name?" INPUT name OUTPUT "First: ", SUBSTRING(name, 1, 1) OUTPUT "Middle: ", SUBSTRING(name, 2, LENGTH(name) - 2) OUTPUT "Last: ", SUBSTRING(name, LENGTH(name), 1)

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

DECLARE numberNames : ARRAY[1:9] OF STRING DECLARE index : INTEGER numberNames <-- ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] FOR index <-- 1 TO 9 IF LENGTH(numberNames[index]) = index THEN OUTPUT "🟢 ", numberNames[index], " has ", index, " characters" ELSE OUTPUT "🔴 ", numberNames[index], " doesn't have ", index, " characters" ENDIF NEXT index

12

PI

Create a program that outputs PI (3.141592653589793) from 0 to 15 decimal places

CONSTANT PI <-- 3.141592653589793 DECLARE decimalPlaces : INTEGER FOR decimalPlaces <-- 0 TO 15 OUTPUT ROUND(PI, decimalPlaces) NEXT decimalPlaces