Tutorial 10
✏️
Functions & Procedures - Custom
2210 O-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
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:
Note: this question requires the string concatenation operator & - while this is technically an AS operator, it is simple enough to learn and allows us to complete this challenge - it simply joins two strings. For example "pseudocode" & "pro" would result in the combined string "pseudocodepro"
5
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"
6
Is Even
Create a function that returns a Boolean value corresponding to whether an integer parameter is even or not
7
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
8
Swap Case
Create a function that takes a string then swaps the case of each character - e.g. "psEudoCOdE" should become "PSeUDOcoDe"
9
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:
10
Absolute Value
Create a function that takes a number then returns its absolute value
11
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
12
Distance from Integer
Create a procedure that generates a random number between 1-10 then outputs the closest integer and how close it is to the nearest integer - e.g. 6.25 has a distance of 0.25 from 6, while 7.9 has a distance of 0.1 from 8