This is the subset of the Free Pascal Run-Time Library (RTL) that we are learning and using in Programming I.
This is a work in progress. I will add more functions as we learn them in this class.
The heading names below are units in the Run-Time Library. To use
the functions from any unit, you need to import it with a uses
clause at the top of your program. As an exception, the functions in
the system
unit are automatically available in any
program.
I’ve simplified some of the types below. For example, the
run-time library documentation often uses the type AnsiString
,
but below I’ve written just string
, which is the same
as long as you’re using ${mode delphi}
as we are in
this course. Similarly, I’ve written integer
in place
of LongInt
and int64
, and real
in place of double
.
I’ve also converted all function names to begin with lowercase (the RTL documentation uses upper and lower case variously). Because Pascal is case-insensitive, you can call these functions using any case you like.
For more information, see the full Run-Time Library Reference at the Free Pascal site.
function isDigit(c: char): boolean
Return true if c is a digit, i.e. a character in the range ‘0’..’9’.
function isLetter(c: char): boolean
Return true if c is an uppercase or lowercase letter.
Black, Blue, Brown, Cyan, DarkGray, Green, LightBlue, LightCyan LightGray, LightGreen, LightMagenta, LightRed, Magenta, Red, White, Yellow
Color constants.
ScreenHeight : integer
The height of the terminal window in characters.
ScreenWidth : integer
The width of the terminal window in characters.
procedure clrScr
Clear the terminal window.
procedure delay(ms: Word)
Pause for the given number of milliseconds.
procedure gotoXY(x: integer; y: integer)
Move to the position (x, y) in the terminal window. The upper-left-most character is (1, 1).
function keyPressed : boolean
Return immediately, returning true if the user has pressed a key
(which you can then retrieve with the readKey
function)
or false otherwise.
function readKey : char
Read a key from the user, without waiting for the user to press Enter. If the key is a non-symbolic key such as an arrow key, returns the character with ASCII code zero. In that case a second call to ReadKey will return a code indicating the non-symbolic key that was pressed. Some of these codes include the following: up arrow = 72, left arrow = 75, right arrow = 77, down arrow = 80.
procedure textColor(c: integer)
Set the foreground text color to c, which can be one of the color constants listed above.
function milliSecondsBetween(start: TDateTime; stop: TDateTime) : Int64
Calculate the number of whole milliseconds between two DateTime values.
Infinity : real
A special floating-point value that is greater than any other.
operator **(base: integer; exponent: integer) : integer
Raise an integer to a power. For example, (3 ** 4)
is
81.
function max(a: integer; b: integer) : integer function max(a: real; b: real) : real
Return the greater of two values.
function min(a: integer; b: integer) : integer function min(a: real; b: real) : real
Return the lesser of two values.
function leftStr(s: string; n: integer) : string
Return the leftmost n characters of a string. For example,
leftStr('around the corner', 6) = 'around'
.
function replaceText(s, old, new: string): string
Return a copy of s
in which all occurrences of old
have been replaced by new
.
function rightStr(s: string; n: integer) : string
Return the rightmost n characters of a string. For example,
rightStr('down the road', 4) = 'road'
.
procedure halt;
Exit the program immediately.
function high(a: array) : int64
Return the upper bound of a static or dynamic array.
function length(a: string) : integer function length(a: DynArrayType) : int64
Return the length of a string or array.
function low(a: array) : int64
Return the lower bound of a static or dynamic array.
procedure setLength(var a: DynArrayType; len: int64)
Set the length of a dynamic array.
Elements in a
dynamic array are numbered from zero, so after a call to setLength(a,
len)
they will have indices from 0 to len
– 1,
inclusive. If this call increases the size of an array, the new array
elements will all be zero.
function eof : boolean
Return true if we have reached the end of standard input. In the typical case where the input comes from a terminal window, this means that the user has pressed Ctrl+D (on Linux or macOS) or Ctrl+Z (on Windows). (“eof” stands for “end of file”).
In most programs you will want to use
the seekEof
function (see below) instead.
procedure read(args: arguments)
Read one or more values of standard input. The exact behavior depends on the type of value being read:
char
: Read a single input character. (This could
be a newline character if the input position is currently at the end
of a line.)
integer
, int64
, real
:
Skip past all whitespace (possibly including one or more newlines)
and then read the first following number.
string
: Consume the rest of the current input
line and return it as a string. Warning: This does not consume
the newline following the string; a subsequent call to read() will
return the empty string. If you want to read a series of strings
on separate lines, you must use readln().
procedure readln(args: arguments)
This procedure is like read
, but after reading all
values it consumes the rest of the input line including the newline
character at the end. (Any extra text on the input line is ignored.)
function seekEof : boolean
Advance past all whitespace (such as spaces or newlines), then return true if we have reached the end of standard input, false otherwise.
procedure write(args: arguments)
Write one or more values to standard output, with no following newline. The values to be written may be of any fundamental type (boolean, integer, int64, real, char, string).
Each value in the argument list may optionally be followed by a field width and decimal width, separated by colons.
The field width is the minimum number of characters to output. If the value to be written is shorter than the field width, the output will be padded at the left with space characters. The default field width is 0.
A decimal width can be specified only for real values, and specifies the number of digits to write after the decimal point. The output value will be rounded to this number of digits.
For example:
write(123 : 5); // writes " 123" write(3.1415926535 : 0 : 4); // writes "3.1416" procedure writeln(args: arguments)
This procedure is like write
, but follows the output
with a newline character.
procedure append(f: text)
Open an existing file for writing. Newly written data will be appended to the end of the file.
procedure assign(f: text; name: string)
Assign a filename to a text file.
procedure close(f: text)
Close a text file.
function eof(f: text): boolean
Return true if we are at the end of the given file.
procedure read(f: text; args: arguments)
Read one or more values from a text file.
procedure readln(f: text; args: arguments)
Read a line containing one or more values from a text file.
procedure reset(f: text)
Open a file for reading.
procedure rewrite(f: text)
Open a file for writing. If the file does not exist, it is created. If the file already exists, it is truncated to length 0: all existing data in the file is lost!
function seekEof(f: text): boolean
Advance past all whitespace (such as spaces or newlines) in a text file, then return true if we are at the end of the file, false otherwise.
procedure write(f: text; args: arguments)
Write one or more values to a text file.
procedure writeln(f: text; args: arguments)
Write a line containing one or more values to a text file.
MaxInt : integer
The maximum value that can be held in an integer. If you have enabled Delphi mode, integers are signed 32-bit values and MaxInt is 2,147,483,647.
Unfortunately the library contains no
corresponding constant MinInt
. The minimum possible
integer value is (- MaxInt - 1)
.
function abs(l: integer) : integer function abs(d: real) : real
Return the absolute number of an integer or floating-point number.
function cos(d: real) : real
Return the cosine of the given angle in radians.
function exp(d: real) : real
Return the exponent of d
, i.e. the number e to the power
d
.
function frac(d: real) : real
Return the fractional part of a floating-point value. For example,
frac(pi)
is 0.14159...
function ln(d: real) : real
Return the natural logarithm (i.e. the logarithm to the base e = 2.7182818…) of the value d, which must be positive.
function pi : real
Return the constant value π ≈ 3.14159...
function random(n: integer) : integer
Return a random integer r in the range 0 ≤ r ≤ (n - 1) .
function random : real
Return a random floating-point number r in the range 0 ≤ r < 1.
procedure randomize
Initialize the random number generator with a seed based on the current time. You must call this once at the beginning of any program if you want random numbers to be different on each program run.
function round(d: real) : integer
Round a floating-point number to the nearest integer. The algorithm uses "banker’s rounding": values of the form i + 0.5 (for any integer i) are always rounded towards an even number.
function sin(d: real) : real
Return the sine of the given angle in radians.
function sqrt(d: real) : real
Return the square root of a floating-point number.
function trunc(d: real) : integer
Return the integer part of d
, which is always smaller
than (or equal to) d
in absolute value.
LineEnding
A newline character. The ASCII value of this character may vary from platform to platform.
function chr(i: integer) : char
Return the character with ASCII value i
. For example,
chr(65)
is 'A'
.
function copy(s: string; index: integer; count: integer): string
Return the substring of s
that begins at index
and has count
characters. For example,
copy('here we go', 6, 2)
is 'we'.
function lowerCase(c: char): char function lowerCase(s: string): string
Convert a character or string to lowercase.
function ord(c: char) : integer
Convert a character to an integer. For example, ord('A')
is 65.
function pos(c: char; s: string): integer
Return the first index at which c occurs in s, or 0 if c is not in s.
function stringOfChar(c: char; n: integer) : string
Return a string consisting of the character c
repeated n
times.
function upCase(c: char): char function upCase(s: string): string
Convert a character or string to uppercase.
function floatToStr(value: real) : string
Convert a floating-point number to a string.
function intToStr(value: integer) : string
Convert an integer to a string.
function now : TDateTime
Return the current date and time.
function strToFloat(s: string) : real
Convert a string to a floating-point number. If the string does not represent a valid floating-point number, the program will fail.
function strToInt(s: string) : integer
Convert a string to an integer. If the string does not represent a valid integer, the program will fail.
function trim(s: string) : string
Trim whitespace from the ends of a string.