Library Routines (Cambridge International IGCSE Computer Science)
Spec point —
8.1.1· 7 Understand and use 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(1,6) | import randomnumber = random.randint(1,10)number = random.randint(-1.0,10.0) |
| Random choice | import randomchoice = 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(, )
| Concept | Pseudocode | Python |
|---|---|---|
| Round |
Example
So if the variable Price was 12.6789, the output would be:
Was this revision note helpful?
Build on this topic