Run-Time Library Quick Reference

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.

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.

character

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.

crt

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.

dateutils

function milliSecondsBetween(start: TDateTime; stop: TDateTime) : Int64

Calculate the number of whole milliseconds between two DateTime values.

math

Infinity : real

A special floating-point value that is greater than any other.

operator **(base: int64; exponent: int64) : int64

Raise an integer to a power. For example, (3 ** 4) is 81.

function ifThen(val: boolean; ifTrue: integer; ifFalse: integer) : integer
function ifThen(val: boolean; ifTrue: double; ifFalse: double) : double

Return the value ifTrue is val is true, otherwise return ifFalse.

function log2(x: float) : float

Return the base-2 logarithm of x.

function max(a: integer; b: integer) : integer
function max(a: double; b: double) : double

Return the greater of two values.

function min(a: integer; b: integer) : integer
function min(a: double; b: double) : double

Return the lesser of two values.

function sign(a: integer): integer;

Return the sign of a, which is +1, 0, or -1 in the cases where a > 0, a = 0 or a < 0, respectively.

strutils

function ifThen(val: boolean; ifTrue: string; ifFalse: string) : string;

Return the value ifTrue is val is true, otherwise return ifFalse.

function leftStr(s: string; n: int64) : string

Return the leftmost n characters of a string.

function replaceText(s, old, new: string): string

Return a copy of s in which all occurrences of old have been replaced by new.

function reverseString(s: string) : string

Reverse a string. reverseString('abcde') is 'edcba'.

function rightStr(s: string; n: int64) : string

Return the rightmost n characters of a string.

function stuffString(s: string; start: integer; length: integer; t: string) : string

Return a copy of s in which the string t replaces the segment of length characters beginning at index start. For example, stuffString('one more time', 5, 4, 'last') is 'one last time'.

system

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) : int64
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.

function paramCount : integer

Return the number of parameters that were passed on the command line.

function paramStr(l: integer) : string

Return the L-th command-line parameter. Parameters are numbered starting from 1.

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.

system (console I/O)

function eof : boolean

Return true if we have reached the end of standard input, i.e. the user has pressed Ctrl+D (on Linux or macOS) or Ctrl+Z (on Windows). (“eof” stands for “end of file”).

function eoln : boolean

Return true if we are at the end of an input line.

procedure read(args: arguments)

Read one or more values from a single line of standard input, without consuming a following newline.

procedure readLn(args: arguments)

Read one or more values from a single line of standard input. 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.

procedure writeln(args: arguments)

Write one or more values to standard output, followed by a newline.

system (file I/O)

procedure assign(out f: file; name: string)
procedure assign(out t: text; name: string)

Assign a filename to a binary or text file.

procedure close(var f: file)
procedure close(var t: text)

Close a binary or text file.

function eof(var f: file)
function eof(var t: text)

Return true if we are at the end of the given file.

function eoln(var t: text)

Return true if we are at the end of a line.

procedure read(var f: file; args: arguments)
procedure read(var f: text; args: arguments)

Read one or more values from a binary or text file.

procedure readln(var f: text; args: arguments)

Read a line containing one or more values from a text file.

procedure reset(var f: file)
procedure reset(var t: text)

Open a file for reading.

procedure rewrite(var f: file)
procedure rewrite(var t: text)

Open a file for writing. If the file does not exist, it is created. If the file exists, it is truncated to length 0.

procedure seek(var f: file; pos: int64)

Seek to a record offset in a file. This works only for binary files, not text files.

function seekEof(var t: 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(var f: file; args: arguments)
procedure write(var f: text; args: arguments)

Write one or more values to a binary or text file.

procedure writeln(var f: text; args: arguments)

Write a line containing one or more values to a text file.

system (math)

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.

function abs(l: integer) : integer
function abs(d: double) : double

Return the absolute number of an integer or floating-point number.

function cos(d: double) : double

Return the cosine of the given angle in radians.

function exp(d: double) : double

Return the exponent of d, i.e. the number e to the power d.

function frac(d: double) : double

Return the fractional part of a floating-point value. For example, frac(pi) is 0.14159...

function ln(d: double) : double

Return the natural logarithm of the value d, which must be positive.

function pi : double

Return the constant value π ≈ 3.14159...

function random(n: integer) : integer

Return a random integer r from a uniform distribution in the range 0 ≤ r < n .

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: double) : int64

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: double) : double

Return the sine of the given angle in radians.

function sqrt(d: double) : double

Return the square root of a floating-point number.

function trunc(d: double) : int64

Return the integer part of d, which is always smaller than (or equal to) d in absolute value.

system (strings)

LineEnding = #10

A newline character. The ASCII value of this character may vary from platform to platform.

function chr(b: byte) : char

Return the character with ASCII value b. For example, chr(65) is 'A'.

function lowerCase(c: char): char
function lowerCase(s: string): string

Convert a character or string to lowercase.

function ord(x: TOrdinal) : integer

Convert an ordinal value such as a character or enumerated value 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.

sysutils

function fileExists(fileName: string) : boolean

Return true if the given file exists.

function floatToStr(value: double) : 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) : double

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.