CH/S4CIT/Feb., 2008.

BUILT-IN FUNCTIONS and PROCEDURES

#Not included in the HKCEE syllabus.

SQR #

A function returns the square of a numeric value.

Syntax: SQR(x) where x is real or integral.

Return type: same as parameter.

e.g.

square := SQR(X-Y)

It means the value (X-Y)2 is assigned to square. If X is 5, Y is 10, square will be 25 after execution.

SQRT

A function returns the positive square root of a numeric value.

Syntax : SQRT(x) where x is non-negative real or integral.

Return type : real

e.g.

SQRT(25) returns 5.

SQRT(-25) causes a run-time error.

TRUNC

A function returns the integer part of the real number by simply truncating the fraction part of it. E.g. TRUNC(3.99) returns 3.

Return type : integer

INT #

A function, similar to TRUNC, returns the integer part of a value. The only difference between them is that the return type of INT is real instead of integer. E.g. INT(3.99) returns 3.00000000E+0000.

ROUND

A function returns an integer nearest to a real number. (It returns an integer with larger magnitude if the fraction part of the real number is equal to .5). E.g. ROUND(3.49) returns 3.

Return type : integer

*** Examples:

ROUND(12.3) returns 12.

ROUND(-12.5) returns -13.

TRUNC(34.23) returns 34.

INT(67.89) returns 6.7000000000E+0001.

ABS

A function returns the absolute value of a numeric value.

Return type: same as the parameter.

E.g.

ABS(14*(-.2)) returns 2.8

ABS(0) returns 0.

SIN # and COS#

Functions return the sine and cosine of an angle in radians.

Return type : real

e.g.

COS(0) returns a value of 1.

If pi = 3.1415927, SIN(pi/6) returns a value approximately equal to 0.5.

ARCTAN #

A function returns an angle in radians whose tangent is X.

Return type : real

e.g.

ARCTAN(1) returns a value of /4.

EXP #

A function returns a value of e raised to the power x.

e is a special value used in calculus. Its value is approximately equal to 2.7182, thus EXP(1) is value approximately equal to 2.7182.

Return type : real

LN #

The natural logarithm of x, i.e. the logarithm to the base e.

Return type : real

e.g. LN(EXP(2)) returns 2.

EXP(3) returns 2.0085536923E+0001

*** To Find the value of xp

Let y = xp

Taking natural logarithm to both sides, we have

ln y = p ln x

Taking anti-logarithm, we have

y = epln x

 If we have two real numbers X and P in Pascal, we can calculate the result of XP by the following expression,

EXP(P*LN(X))

RANDOM

A non-standard function returns a random number.

Without parameter, RANDOM returns a real number r where 0 < r < 1.#

If an integer parameter is specified, RANDOM(X) returns an integer r such that 0  r  X - 1.

Actually, RANDOM(X) is the same as TRUNC(X*RANDOM).

RANDOMIZE. It is a procedure initialising the seed value for generating random numbers hence enables the RANDOM function to produce different sequence of random numbers.

Sample program: P.4 of the notes on iteration. (rem: Add the statement “randomize;” at the beginning of the main program, so as to generate different sequence of die number)

ORD

A function returns the ordinal number of the input value x.

Return type : integer

If x is integer, it returns the integral value itself.

If x is boolean, it returns 1 for true, and 0 for false.

If x is character, it returns the ASCII of it.

** ASCII (American Standard Code for Information Interchange

ASCII assigns numeric values to all the conventionally used characters.

The standard ASCII has 128 different codes. The following table shows the codes from 32 to 127:

Decimal
Code / Binary
Code / Hexa. Code / Character / Decimal
Code / Binary
Code / Hexa. Code / Character
32 / 00100000 / 20 / (space) / 52 / 00110100 / 34 / 4
33 / 00100001 / 21 / ! / 53 / 00110101 / 35 / 5
34 / 00100010 / 22 / " / 54 / 00110110 / 36 / 6
35 / 00100011 / 23 / # / 55 / 00110111 / 37 / 7
36 / 00100100 / 24 / $ / 56 / 00111000 / 38 / 8
37 / 00100101 / 25 / % / 57 / 00111001 / 39 / 9
38 / 00100110 / 26 / 58 / 00111010 / 3A / :
39 / 00100111 / 27 / ' / 59 / 00111011 / 3B / ;
40 / 00101000 / 28 / ( / 60 / 00111100 / 3C
41 / 00101001 / 29 / ) / 61 / 00111101 / 3D / =
42 / 00101010 / 2A / * / 62 / 00111110 / 3E
43 / 00101011 / 2B / + / 63 / 00111111 / 3F / ?
44 / 00101100 / 2C / 64 / 01000000 / 40 / @
45 / 00101101 / 2D / - / 65 / 01000001 / 41 / A
46 / 00101110 / 2E / . / 66 / 01000010 / 42 / B
47 / 00101111 / 2F / / / 67 / 01000011 / 43 / C
48 / 00110000 / 30 / 0 / 68 / 01000100 / 44 / D
49 / 00110001 / 31 / 1 / 69 / 01000101 / 45 / E
50 / 00110010 / 32 / 2 / 70 / 01000110 / 46 / F
51 / 00110011 / 33 / 3 / 71 / 01000111 / 47 / G
72 / 01001000 / 48 / H / 100 / 01100100 / 64 / d
73 / 01001001 / 49 / I / 101 / 01100101 / 65 / e
74 / 01001010 / 4A / J / 102 / 01100110 / 66 / f
75 / 01001011 / 4B / K / 103 / 01100111 / 67 / g
76 / 01001100 / 4C / L / 104 / 01101000 / 68 / h
77 / 01001101 / 4D / M / 105 / 01101001 / 69 / i
78 / 01001110 / 4E / N / 106 / 01101010 / 6A / j
79 / 01001111 / 4F / O / 107 / 01101011 / 6B / k
80 / 01010000 / 50 / P / 108 / 01101100 / 6C / l
81 / 01010001 / 51 / Q / 109 / 01101101 / 6D / m
82 / 01010010 / 52 / R / 110 / 01101110 / 6E / n
83 / 01010011 / 53 / S / 111 / 01101111 / 6F / o
84 / 01010100 / 54 / T / 112 / 01110000 / 70 / p
85 / 01010101 / 55 / U / 113 / 01110001 / 71 / q
86 / 01010110 / 56 / V / 114 / 01110010 / 72 / r
87 / 01010111 / 57 / W / 115 / 01110011 / 73 / s
88 / 01011000 / 58 / X / 116 / 01110100 / 74 / t
89 / 01011001 / 59 / Y / 117 / 01110101 / 75 / u
90 / 01011010 / 5A / Z / 118 / 01110110 / 76 / v
91 / 01011011 / 5B / [ / 119 / 01110111 / 77 / w
92 / 01011100 / 5C / \ / 120 / 01111000 / 78 / x
93 / 01011101 / 5D / ] / 121 / 01111001 / 79 / y
94 / 01011110 / 5E / ^ / 122 / 01111010 / 7A / z
95 / 01011111 / 5F / _ / 123 / 01111011 / 7B / {
96 / 01100000 / 60 / ` / 124 / 01111100 / 7C / |
97 / 01100001 / 61 / a / 125 / 01111101 / 7D / }
98 / 01100010 / 62 / b / 126 / 01111110 / 7E / ~
99 / 01100011 / 63 / c / 127 / 01111111 / 7F / (del)

Most computer use 8-bit words to represent characters, thus 256 different characters are represented. Codes 128  255 are called the extended ASCII. Some extended ASCII codes include symbols used in languages other than English and various mathematics symbols.

CHR

It returns the character whose ordinal number is the integer value of the parameter.

e.g. CHR(65) returns ‘A’.

Here are two sample programs. The first one (Functions) finds out the ASCII value of an input character. The second one (ASCII) prints a table of all printable characters of the standard ASCII character set.

program Functions(input, output);

var

Character : char;

begin

write('Type a letter: ');

readln(Character);

writeln('The ordinal number of ', Character, ' is ', ORD(Character), '.')

end.

program ASCII(input, output);

var I, J, Code : integer;

Character : char;

begin

writeln(' ASCII code');

writeln;

for I := 1 to 6 do write(' CODE CHAR ');

writeln;

writeln;

for I := 0 to 15 do begin

for J := 0 to 5 do begin

Code := 32 + I * 6 + J;

write(Code:5, CHR(Code):6, ' ')

end;

writeln

end

end.

** Note that we can convert a lower-case letter to an upper case one as follows:

CHR(ORD(X)-32)

where X is a character variable holding a lower-case letter.

LENGTH

A function returns the length of the string in parameter.

E.g. LENGTH(‘Merry Christmas’) returns 15.

COPY

A function extracts a number of characters, beginning from a specified position, from the character string.

E.g. COPY(‘Hello’, 2, 3) returns ‘ell’.

STR

A procedurereturns a string from a numeric value.

E.g. STR(266, outstr) where outstr will become string ‘266’ after execution.

VAL

A procedurereturns a numeric value from a string with numeric format in it.

Syntax:Val(S, V, Code);

where:

Sstring-type variable or constant input

Vinteger-type or real-type variable represented by the input string

Code integral variable storing the result that if error found in the numeric form string. If found, it stores the position of the first offending character, otherwise, it stores 0.

e.g.

VAL('135.7XX', Num, Err);(*Num becomes 0 and Err becomes 6

if Num is a real number.*)

VAL('135.7', Num, Err); (*Num becomes 1.3570000000E+0002 and Err becomes 0

if Num is a real number, and

Num becomes 0 and Err becomes 4

if Num is an integer.*)

*** Sample program - Check the validity of the input numeric string and validated output numeric value.

program ValDemo(input, output);

var

string1, string2 : string;

Num, ErPos : integer;

begin

readln(String1);

VAL(String1, Num, ErPos);

writeln('Error position is ', ErPos);

if ErPos >0 then

begin (* get a substring of String1 without any offending characters *)

String2 := COPY(String1, 1, ErPos - 1);

VAL(String2, Num, ErPos)

end;

writeln(Num)

end.

BUILT-IN FUNCTIONS and PROCEDURESpage 1