CIE AS & A Level Computer Science, section 11.1 (Programming basics)
Input and output
OUTPUT "Enter your name"
INPUT Name
OUTPUT "Hello ", Name
INPUT reads from the keyboard into a variable. OUTPUT writes to the screen. Several items can be output separated by commas.
Built-in functions and library routines
A built-in function is provided by the language. A library routine is a ready-made routine from a library. Using them saves time, because they are already written and tested. The exam provides the functions that are not in the pseudocode guide, and string functions are always given.
Common functions (pseudocode)
| Function | Returns | Example |
|---|---|---|
LENGTH(s) |
the number of characters | LENGTH("Computer") is 8 |
LEFT(s, n) |
the first characters | LEFT("Computer", 3) is "Com" |
RIGHT(s, n) |
the last characters | RIGHT("Computer", 3) is "ter" |
MID(s, start, n) |
characters from start (first position is 1) |
MID("Computer", 3, 3) is "mpu" |
ASC(c) |
the character code | ASC("A") is 65 |
CHR(n) |
the character with that code | CHR(97) is "a" |
INT(x) |
the whole-number part | INT(7.9) is 7 |
NUM_TO_STR(x), STR_TO_NUM(s) |
convert between number and text | STR_TO_NUM("42") is 42 |
RAND(n) |
a random real number |
Python
s = "Computer"
print(len(s), s[:3], s[-3:], s[2:5]) # 8 Com ter mpu
print(ord("A"), chr(97), int(7.9)) # 65 a 7
print(int("42") + 1) # 43
(Python counts from 0, so MID(s, 3, 3) is s[2:5].)
Why use them
They are tested, efficient, and make the program shorter and easier to understand.
Common mistake: forgetting that the first character of a string is position 1 in the pseudocode MID, and position 0 in Python. Also, INPUT stores text unless you convert it with a function such as STR_TO_NUM.
Exam tip: when asked to use a string function, write the function name, then the arguments in order. Check your answer by applying it to a short word.
Check yourself
What does `LEFT("Algorithm", 4)` return?
"Algo".
What does `LENGTH("Hello")` return?
Why use a built-in function?
It is already written and tested, which saves time and reduces errors.