Predefined Functions (SQA National 5 Computing Science): Revision Note
Exam code: X816 75
Predefined functions
What is a predefined function?
A predefined function is code that carries out a process
A user does not need to know how the process is carried out
The function performs the task and returns at least one value, often by storing it in a variable
These functions are typically built in to high-level (textual) programming languages
Random function
The
randomfunction is used to generate a random number between two specified numerical limitsRandom number generation is a programming concept used within a program to add an element of unpredictability
Examples of where this concept could be used include:
A task may require generating a random value between 1 and 10 to calculate a discount
The assignment requires storing a random integer in this range
One program design specified selecting a random song to prompt the user to switch on a foam machine
Implementing this involves storing a random number equal to the total number of songs entered
In a voting app, the function can be used to pick one winning candidate from an array storing the names of candidates who received the same number of votes
To select a mystery fruit from a list of ten options, the program generates a random number (e.g., between 0 and 9) to index the fruit from an array of mystery fruits
Syntax
Function | Pseudocode | Python |
|---|---|---|
Random |
|
|
Will pick a number between 1 and 100 |
Example
A National lottery simulation (randomly picking 6 numbers)
SQA Pseudocode | Python |
|---|---|
| |
Round function
The
roundfunction is used to round a real number to a certain number of decimal placesIt takes two parameters, the number to be rounded (a real number) and thee number of decimal places to round to
Examples of where this concept could be used include:
A calculation process might require rounding an average value, such as the average weight of food, to one decimal place
A flowchart design illustrated rounding a score to 2 decimal places
Students may be tasked with rewriting code to display an average data usage rounded to one decimal place
Syntax
Function | Pseudocode | Python |
|---|---|---|
Round |
| |
Will round the value of |
Example
SQA Pseudocode | Python |
|---|---|
| |
Length function
The
lengthfunction is used to determine and retrieve the size of a data structureIt is used to get the length of a string or an array
Syntax
Function | Pseudocode | Python |
|---|---|---|
Length |
| |
Stores the length of a string in the variable named |
The length function can also be found in the string operations page
Worked Example
A music promoter is organising a lucky draw for concert attendees.
The names of the 50 individuals eligible to win a backstage pass have been identified and stored temporarily in the program.
State the predefined function that should be used to select one of the 50 stored names for the prize
[1]
Answer
random [1 mark]
Language specific functions: e.g.
randint,rnd[1 mark]
Unlock more, it's free!
Did this page help you?