Library Routines
Library Routines
Library Routines
What are library routines?
A library routine is reusable code that can be used throughout a program
The code has been made public through reusable modules or functions
Using library routines saves programmers time by using working code that has already been tested
Some examples of pre-existing libraries are:
Random
Round
Random
The random library is a library of code which allows users to make use of 'random' in their programs
Examples of random that are most common are:
Random choices
Random numbers
Random number generation is a programming concept that involves a computer generating a random number to be used within a program to add an element of unpredictability
Examples of where this concept could be used include:
Simulating the roll of a dice
Selecting a random question (from a numbered list)
National lottery
Cryptography
Concept | Pseudocode | Python |
|---|---|---|
Random numbers |
|
|
Random choice |
|
Examples
Random number
Pseudocode | Python |
|---|---|
| |
National lottery
Pseudocode | Python |
|---|---|
| |
Round
The round library is a library of code which allows users to round numerical values to a set amount of decimal places
An example of rounding is using REAL values and rounding to 2 decimal places
The round library can be written as:
ROUND(<identifier>, <places>)
Concept | Pseudocode | Python |
|---|---|---|
Round | | |
Example
// Example using the ROUND function
DECLARE Price : REAL
DECLARE RoundedPrice : REAL
Price ← 12.6789
// Round the value of Price to 2 decimal places
RoundedPrice ← ROUND(Price, 2)
OUTPUT "The rounded price is ", RoundedPriceSo if the variable Price was 12.6789, the output would be:
The rounded price is 12.68