Tutorial 10
✏️
Functions & Procedures - Custom
9618 A-Level
The previous tutorial looked at the built-in Cambridge modules/sub-routines (functions or procedures) - in this tutorial, we'll look at how to create and call (use) our own
A recap, the difference between a function and procedure is outlined 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 benefits of creating functions and procedures include:
- Organisation - keeps code organised into small, logical blocks
- Reusability - don't have to copy and paste same functionality over and over
- Flexibility - if we need to modify behaviour, we only need to change once inside the module
- Testing - easy to test individual modules to ensure they work
- Parameterisation - can use the same module with different parameters to achieve different behaviour
1
FUNCTIONs
Each time we declare a function, we need:
- FUNCTION & ENDFUNCTION keywords
- An identifier (function name)
- [optional] A list of parameters
- A return data type
- A return statement for every code path
A simple example is shown below that takes in an integer parameter, then returns whether it is a lucky number, according to some superstition:
Note: when specifying the return type in the function header, we use the keyword RETURNS, while when actually returning a value, we use RETURN
Note: the following example would be invalid - since not all situations (code paths) result in a value being returned
Note: the order of execution demonstrated via these OUTPUT statements - when we return from a function, we leave it and execution continues from the position the function was called from - this is why the OUTPUT "Leaving Function" statement will never execute
2
PROCEDUREs
Declaring a procedure requires less code than a function, since we don't have to return anything - we simply need:
- PROCEDURE & ENDPROCEDURE keywords
- An identifier (procedure name)
- [optional] A list of parameters
Note: in order to call (use) a procedure, we require the CALL keyword
3
Multiple Parameters
For either functions or procedures, we can either write each parameter and its data type individually, or if we have consecutive parameters of the same type, we can simply separate them with a comma - like the "name" and "email" parameters below:
4
BYVAL vs BYREF
One of the trickier concepts in the syllabus is the way parameters are passed - there are two options:
- BYVAL: this is the default parameter passing method if none is specified. A copy of the parameter is copied when the module is called and this copy is used throughout the module - i.e. any changes we make to this parameter won't affect the original variable
- BYREF: by reference. A reference (memory address) to the original variable is passed - i.e. any changes we make to this parameter will affect the original variable
Note in the example below that, the value inside the procedure will double, but the original x variable won't
In the following example, since we are using BYREF, then a copy of the variable isn't created inside the procedure - x and num are pointing to the same memory address, so modifying num inside the procedure will also modify the value of x
5
Triangle
Create a procedure that can output a right-angled triangle to a given depth - for example, if the depth was 4, the result would be:
6
Hash Length
There are various online tools that can take a hash digest (string), then based on its value, try to predict the hashing algorithm that we used - one simple approach is by the length, since most hashing algorithms produced fixed-length hashes
Create a function that takes a string hash digest and returns the name of the possible hashing algorithm used
The data below shows the hashing algorithm and length (in characters) of the digest it produces:
- md5: 32
- sha256: 64
- sha512: 128
If the length doesn't match one of these value, then [UNKNOWN] should be returned
You can use this site to hash various values to use as arguments for your function - e.g. "hello world" hashes to "5eb63bbbe01eeed093cb22bb8f5acdc3"
7
Is Even
Create a function that returns a Boolean value corresponding to whether an integer parameter is even or not
8
Is Prime
Create a function that returns a Boolean value corresponding to whether an integer parameter is prime or not
Use this function in a loop to output all the primes between 1 and 1000
9
Swap Case
Create a function that takes a string then swaps the case of each character - e.g. "psEudoCOdE" should become "PSeUDOcoDe"
10
Times Table
Create a procedure that outputs the times table of a given number from a start to ending multiplier - for example CALL DisplayTimesTable(6, 10, 20) would output:
11
Absolute Value
Create a function that takes a number then returns its absolute value
12
Factorial
Create a function that can return the factorial of a given number - i.e. factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120
13
Round
Create a function that takes a real number then rounds it to the nearest integer
14
Add Days
Create a function that takes in a date and number of days to add, then returns a new date - e.g. adding 5 days to 30/12/2024 would result in the new date 04/01/2025
Note: for simplicity, you can ignore leap years
15
Double Array
Create a procedure that takes an array then doubles all of its values - i.e. the array [1, 2, 3] should become [2, 4, 6]
16
Random Files
Create a procedure that asks the user how many files they'd like to create, how many random numbers should be in each file, the min & max value of the random numbers and once complete, if they'd like to output any file
For example, if they choose to create 3 files with 5 numbers each, then the files "numbers1.txt" to "numbers3.txt" would be created
17
Character Type
Create a function that takes in a character then returns either "uppercase", "lowercase", "number" or "other" depending on the character's type
18
Is Weekend
Create a function that returns a Boolean corresponding to whether a given date is a weekend (Saturday or Sunday)
19
Credit Card Numbers
Create a procedure that can take a credit card number as a string, then output the following information:
- Card Network: Visa cards start with a 4; Mastercards start with a 5
- Bank Identification Number: digits 2 to 6 identify the bank
- Account: digits 7 to 15 represent your bank account
- Check digit: digit 16
Note: both Visa & Mastercard cards should be 16 digits - an error should be output if the card number isn't this length
Example cards can be found here - any non-numbers (spaces, hyphens etc) should be removed
As an extension, you could also implement the relatively simple Luhn algorithm that credit cards use - to verify the check digit is correct
20
Add Numbers in a String
Create a function to add all the digits in a string - for example, "h3l10 w0r1d" would return 5, since 3 + 1 + 0 + 0 + 1 = 5
21
Friday 13th
Many people consider the date Friday 13th to be unlucky/haunted - create a procedure that will output a message depending on whether today is/isn't Friday 13th
22
Hash Code
Create a function that takes a string, adds up the ASCII values of all of its characters combines, then finds the remainder after dividing by 100 to get the hash code
23
Starts & Ends With Vowel
Create a function that takes a string, then capitalises any word that both starts and ends with a vowel - for example "ace pilot" would return "ACE pilot"
24
Random Characters
Create a procedure that outputs a given number of random characters between a min and max ASCII (well technically Unicode) value - for example, CALL RandomChars(5, 128000, 130000) might output: