Built-In Procedures and Functions |
6 |
![]() |
The third section, beginning on page 99, lists the nonstandard routines alphabetically and contains detailed descriptions and examples of each routine.
Standard Procedures and Functions
Pascal supplies the standard procedures listed in Table 6-1, and the standard functions listed in Table 6-2.
dispose |
page |
readln |
unpack |
get |
put |
reset |
write |
new |
read |
rewrite |
writeln |
pack |
|
|
|
abs |
eof |
odd |
round |
sqrt |
arctan |
eoln |
ord |
sin |
succ |
chr |
exp |
pred |
sqr |
trunc |
cos |
ln |
|
|
|
Routines Specific to Pascal (Summary)
This section lists the nonstandard Pascal procedures and functions according to the following categories:
Table 6-4 Nonstandard Bit Shift Routines
Table 6-5 Nonstandard Character String Routines
Table 6-6 Nonstandard Input and Output Routines
Table 6-7 Extensions to Standard Input and Output Routines
Table 6-8 Miscellaneous Nonstandard Routines
Routines Specific to Pascal (Details)
Described in this section are the detailed descriptions for each of the Pascal-specific routines: its syntax, arguments, and return value. Comments and an example are also included. addr
The addr function returns the address of a variable, constant, function, or procedure. Syntax
addr(x) Arguments
x is either a variable, a constant string, a function, or a procedure. Return Value
The return value of addr is the address in which the variable or a constant string is stored. For function or procedural arguments, addr returns the starting address of the function or procedure. In each case, addr returns a value of type univ_ptr. Comments
In Pascal, you can apply addr to a variable, function, or procedure with dynamic extent such as local variables and nested functions or procedures. Exercise caution in doing so and then dereferencing the resulting pointer value. In the case of local variables, dereferencing these pointers outside the scope in which the variable is active results in a meaningless value.
Note - If you use the addr ( ) function, do not use the -H option. The -H option makes sure that all pointers used point into the heap.
Example
The commands to compile and execute addr.p
|
hostname% pc addr.p hostname% a.out The address of x is 38764. The contents of x is 2147483647. The address of c is 33060. The contents of c is Gail. |
append
The append function allows a file to be modified, and sets the current position to the end of the file. Syntax
append(file, filename) Arguments
file is a variable with the text or file data type. Return Value
append does not return any values. Comments
For example, this code associates the Pascal file data with the operating system file, existent:append(data, 'existent');
If you do not pass an optional second argument, Pascal creates a new temporary file, which is deleted when the program is terminated.
Example
The example that follows shows how to use append.
argc-
The argc function returns the number of arguments passed to the program. Syntax
argc Arguments
argc does not take any arguments. Return Value
argc returns an integer value. Comments
The return value of argc is always at least 1, the name of the program.
Example
See the example in the argv listing page 105.
argv
The argv procedure assigns the specified program argument to a string variable. Syntax
argv(i, a) Arguments
i is an integer value. Return Value
argv returns a string variable. Comments
argv returns the i'th argument of the current process to the string variable a. i ranges from 0, the program name, to argc-1. Example
arshft-
The arshft function does an arithmetic right shift of an integer value. Syntax
arshft(num, sh) Arguments
num and sh are integer expressions. Return Value
arshft returns a 32-bit integer value. Comments
arshft shifts the bits in num sh places to the right. arshft preserves the sign bit of num. arshft does not wrap bits around from left to right. The sign bit is the most significant (leftmost) bit in the number. Pascal uses two's complement to represent negative integers. For example, -8 as a 16-bit integer is represented as:1111 1111 1111 1000
If you shift this number to the right by 1:(arshft (-8, 1) )
your result is:1111 1111 1111 1100
The result arshft returns is machine-dependent, and is unspecified unless the following is true:0 <= sh <= 32
Example
asl-
The asl function does an arithmetic left shift of an integer value. Syntax
asl(num, sh) Arguments
num and sh are integer expressions. Return Value
asl returns a 32-bit integer value. Comments
asl shifts the bits in num sh places to the left. asl preserves the sign bit of num and does not wrap bits from left to right.0 <= sh <= 32
Example
asr-
The asr function is identical to the arshft function. See the arshft listing. card
The card function returns the number of elements in a set variable. Syntax
card(x) Arguments
x must be a set variable. Return Value
card returns an integer value. Comments
card returns the number of elements in the actual set variable, not the size of the set type. Example
The commands to output and execute card.p
|
hostname% pc card.p hostname% a.out There are 3 primary colors. There are 2 possibilities. There are 26 capital letters. There are 101 digits. |
clock
The clock function returns the user time consumed by the process. Syntax
clock Arguments
clock does not take any arguments. Return Value
clock returns an integer value. Comments
clock returns the user time in milliseconds. Example
clock.p Program (Screen 1 of 2)
clock.p Program (Screen 2 of 2)
close
The close procedure closes a file. Syntax
close(file) Arguments
file is a file having the text or file data type. Return Value
close does not return any values. Comments
close closes the open file named file. close is optional; Pascal closes all files either when the program terminates or when it leaves the procedure in which the file variable is associated with the open file.
In Pascal, you cannot close the predeclared files input and output. If you redirect input or output, the associated streams are automatically closed.
Example
See the example in the open listing in this chapter. concat
The concat function returns the concatenation of two strings. Syntax
concat(str1, str2) Arguments
str1 is a variable-length string, a character array, or a character-string constant. Return Value
concat returns a variable-length string. Comments
concat returns a concatenation of str1 and str2. You can concatenate any combination of varying, array of char, constant strings, and single characters.
The Pascal program, concat.p
|
program concat_example(output); var color: varying [10] of char := ' Black'; begin writeln(concat(color, 'bird' + '.')); end. |
The commands to compile and execute concat.p
|
hostname% pc concat.p hostname% a.out Blackbird. |
date
The date procedure takes the current date (as assigned when the operating system was initialized) and assigns it to a string variable. Syntax
date(a) Arguments
a is a variable that can be either a character array that is 8 elements long for the "C" locale, or a variable-length string. Return Value
date returns a character string in the form traditional for a given locale. For the "C" locale, the form is mm-dd-yy, where dd is the day, mm is the month, and yy is the year. Comments
date puts a zero in front of the day and the year, so that they always consist of two digits.
discard
The discard procedure removes the value of an expression. Syntax
discard(expr) Arguments
expr is any expression including a function call. Return Value
discard does not return any values. Comments
Use discard to call a function or evaluate an expression whose value you do not need to continue program execution. For example, you can use discard to execute a function whose return value you do not need. Example
The commands to compile and execute discard.p
|
hostname% pc discard.p hostname% a.out Enter sale amount: 125.00 The amount of discount on 125.00 is 18.75. |
expo
The expo function calculates the integer-valued exponent of a specified number. Syntax
expo(x) Arguments
x is either a real or integer value. Return Value
expo returns an integer value. Comments
expo returns an integer that represents the integer-valued exponent of a real number. Example
filesize
The filesize function returns the size of a given file. Syntax
filesize(file) Arguments
file is a variable with the text or file data type. Return Value
filesize returns an integer value. Comments
The argument can be either a text file of text type, or a binary file of a certain file of T type. It must be associated with an open file, otherwise an error occurs.
The commands to compile and execute filesize.p
|
hostname% pc filesize.p hostname% a.out size of a text of an integer = 20 bytes size of a file of an integer = 2 elements |
firstof
The firstof function returns the value of the lower bound when its argument is or has an ordinal type. For array types, firstof returns the lower bound for the subrange defining the array index. For set types, it returns the lower bound of the set base type. Syntax
firstof(x) Arguments
x is either a variable, a constant, an expression, or the name of a user-defined or predeclared Pascal data type. x cannot be a record, a file, a pointer type, a conformant array, a procedure or function parameter, or a string literal. Return Value
The return value depends on the type that x is.
Comments
Pascal follows the rules in Table 6-9 when returning the value of x.
Table 6-9 firstof Return Values
Example
See the examples that follow.
flush
The flush procedure writes the output buffer for the specified Pascal file into the associated file. Syntax
flush(file) Arguments
file is a file having the text or file data type. Return Value
flush does not return any values. Comments
The flush procedure causes the compiler to write all characters buffered for output to the specified file.
for i := 1 to 5 do begin write(f,i); Compute a lot with no output end; flush(f); |
flush does not append a newline character after writing the data. See also the output procedures, message, write, and writeln.
Example
The commands to compile and execute flush.p
|
hostname% pc flush.p hostname% a.out Beginning of file. End of file. Beginning of file. 1 2 3 4 5 6 7 8 9 10 End of file. |
getenv
The getenv function returns the value associated with an environment name. Syntax
getenv(string, string_variable) Arguments
string is either a constant string, a variable-length string, or a character array. string_variable is a variable-length string or a character array. Return Value
getenv returns a variable-length string or a character array. Comments
The variable string is an environment name. Pascal returns the value for the environment name through the parameter, string_variable. Example
The commands to compile and execute getenv.p
|
hostname% pc getenv.p hostname% a.out EDITOR = /usr/ucb/vi EDITOR = /usr/ucb/vi |
getfile
The getfile function returns a pointer to the C standard I/O descriptor associated with a Pascal file. Syntax
getfile(file) Arguments
file is a file having the text or file data type. file must be associated with an open file; otherwise, getfile returns nil. Return Value
getfile returns a value of type univ_ptr. Comments
You can use the result of getfile for files opened with either the reset, rewrite, or open procedures, placing the return value as a parameter to a C I/O routine. Use extreme caution when you call getfile; directly calling C I/O routines circumvents bookkeeping data structures in the Pascal I/O library. Example
The commands to compile and execute getfile.p
|
hostname% pc getfile.p hostname% a.out hostname% more output.data Hello, world, in the year 1996 . |
halt
The halt procedure terminates program execution. Syntax
halt Arguments
halt does not take any arguments. Return Values
halt does not return any values. Comments
You can use halt anywhere in a program to terminate execution. When execution of a program encounters a halt, it prints the following message:Call to procedure halt
Pascal returns to command level after it executes halt. Example
in_range
The in_range function checks if a value is in the defined subrange. Syntax
in_range(x) Arguments
x is an integer, boolean, character, enumerated, or subrange data type. Return Value
in_range returns a boolean value. Comments
in_range returns true if x is in the defined range, false if x is outside the range. Example
The commands to compile and execute in_range.p
|
hostname% pc in_range.p hostname% a.out Enter values for triangle base and height: 4 5 Area is 10.00. |
index
The index function returns the position of the first occurrence of a string or character within another string. Syntax
index(target_string, pattern_string) Arguments
target_string is a constant string, variable-length string, or an array of character. Return Value
index returns an integer value that represents the position of the first occurrence of pattern_string within target_string. If the first occurrence is at the starting position of the original string, the returned index value is 1. Comments
The leftmost occurrence of the pattern-string is considered the first occurrence. Example
See the example that follows.
The commands to compile and execute index.p
|
hostname% pc index.p hostname% a.out The string EXAMPLE is at index 7 in the string INDEX_EXAMPLE. The string FOO is not in the string INDEX_EXAMPLE. |
land
The land function returns the bitwise and of two integer values. Syntax
land(int1, int2) Arguments
int1 and int2 are integer expressions. Return Value
land returns an integer value. Comments
land performs a bit-by-bit and operation, as shown in Table 6-10.
Value of Bit in result
0
0
0
0
1
0
1
0
0
1
1
1
Table 6-10 land Truth
Value of Bit in int1
Value of Bit in int2
Example
lastof
The lastof function returns the value of the upper bound when its argument is or has an ordinal type. For array types, lastof returns the upper bound for the subrange defining the array index. For set types, it returns the upper bound of the set base type. Syntax
lastof(x) Arguments
x is either a variable, a constant, an expression, or the name of a user-defined or predeclared Pascal data type. x cannot be a record, a file, a pointer type, a conformant array, a procedure or function parameter, or a string literal. Return Value
When x is an ordinal type, a constant, an expression, or variable, the value lastof returns has the same data type as its argument. Comments
Pascal follows the rules in Table 6-11 when returning the value of x.
Table 6-11 lastof Return Values
Example
See the example under firstof on page 126.
length
The length function returns the length of a string. Syntax
length(str) Arguments
str is a variable-length string, a character array, or a character-string constant. Return Value
length returns an integer value. Comments
length returns a value that specifies the length of str. Example
The commands to compile and execute length.p
|
hostname% pc length.p hostname% a.out The length of string one is 15. The length of string two is 10. The combined length is 25. |
linelimit
The linelimit procedure terminates execution of a program after a specified number of lines has been written into a text file. Syntax
linelimit(file, n) Arguments
file is a file having the text or file data type. Return Value
linelimit does not return any values. Comments
linelimit terminates program execution if more than n lines are written to file f. If n is less than zero, no limit is imposed. Example
lnot
The lnot function returns the bitwise not of an integer value. Syntax
lnot(int) Arguments
int is an integer expression. Return Value
lnot returns an integer value. Comments
lnot performs a bit-by-bit not operation, as shown in Table 6-12.
Value of Bit in int |
Value of Bit in result |
0 |
1 |
1 |
0 |
lnot produces the same results as the bitwise operator ~. Do not confuse lnot with the boolean operator not, which evaluates the logical not of a boolean expression.
Example
See the example under land on page 142.
lor
The lor function returns the inclusive or of two integer values. Syntax
lor(int1, int2) Argument
int1 and int2 are integer expressions. Return Value
lor returns an integer value. Comments
lor performs an inclusive or, as shown in Table 6-13.
Value of Bit in intl1
Value of Bit in int2
Value of Bit in result
0
0
0
0
1
1
1
0
1
1
1
1
Table 6-13 lor Truth
Example
See the example under land on page 142.
lshft
The lshft function does a logical left shift of an integer value. Syntax
lshft(num, sh) Argument
num and sh are integer expressions. Return Value
lshft returns a 32-bit integer value. Comments
lshft shifts all bits in num sh places to the left. lshft does not wrap bits from the left to right. The value lshft returns is machine-dependent and is unspecified unless 0 <= sh <= 32.
lsl
The lsl function is identical to the lshft function. See the lshft listing on page 151.
lsr
The lsr function is identical to the rshft function. See the rshft listing on page 171.
max
The max function evaluates two scalar expressions and returns the larger one. Syntax
max(exp1, exp2) Arguments
exp1 and exp2 are any valid scalar expressions that are assignment-compatible. Return Value
max returns the same or the converted type of exp1 and exp2.
message
The message procedure writes the specified information on stderr (usually the terminal). Syntax
message(x1, ..., xN) Arguments
x is one or more expressions separated by commas. x can be a variable, constant, or expression of a type that write accepts (such as integer, real, character, boolean, enumerated, or string). x cannot be a set variable. Return Value
message does not return any values. Comments
message is an output procedure similar to write and writeln. Whereas write and writeln send the output to standard output or the specified file, message sends the output to standard error. message also appends a carriage return to the message.
writeln(errout, x1, ..., xN); flush(errout); flush(output); . . { Flush all open files. } |
min
The min function evaluates two scalar expressions and returns the smaller one. Syntax
min(exp1, exp2) Arguments
exp1 and exp2 are any valid scalar expressions that are assignment-compatible. Return Value
min returns the same or the converted type of exp1 and exp2. Comments
See also the max listing on page 153.
Example
See the example under the max listing on page 153.
null
The null procedure performs no operation. Syntax
null Arguments
null does not take any arguments. Return Value
null does not return any values. Comments
null does absolutely nothing; it is useful as a placeholder. For example, suppose you are developing a program, and you are uncertain about a particular case statement, you could put null in place of the case statement, then replace it later with an actual function or procedure. open
The open procedure associates an external file with a file variable. Syntax
open(file, pathname, history, error, buffer) Arguments
open takes the following arguments:
pathname must be one of the following:
Pascal returns an integer error code through error, as shown in Table 6-14.
Table 6-14 open Error Codes
The commands to compile and execute random.p
|
hostname% pc random.p hostname% a.out These numbers were generated at random: 97 6 48 91 35 |
read and readln
Pascal supports the standard form of read and readln with three extensions:
readln(file, var1 ..., varN);
var can be any real, integer, character, boolean, subrange, enumerated, or array variable or a fixed- or variable-length string variable. If read or readln is used in a function to define the function result, var can also be an identifier of that function.
With both variable- and fixed-length strings, if the number of characters on a line is more than the maximum length of the string, the next read picks up where the last read left off. With readln, the rest of the line is discarded, so the next read or readln begins at the next line.
If var is an enumerated type, read and readln attempt to read a value that is included in the type definition. If the value is not in the type definition, the compiler terminates program execution and prints the following error message:
Unknown name "value" found on enumerated type readYou can trap this error with the I/O error recovery mechanism, described in "I/O Error Recovery" on page 214. Using read or readln in the block of a function in the form:
Trace/BPT trap (core dumped)
read (..., f, ...)
is treated as if it were an assignment of the form:f:=e
where e is the input value. This feature is an extension of the Pascal Standard, and so cannot be used with the -s option. Example
remove
The remove procedure removes the specified file. Syntax
remove(file) Arguments
file is either a fixed- or variable-length string that indicates the name of the file to be removed. file cannot be a text or file variable. Return Value
remove does not return any values. Comments
Pascal generates an I/O error if the file does not exist. You can trap this error with the I/O error recovery mechanism, described in "I/O Error Recovery" on page 214.
The commands to compile and execute remove.p
|
hostname% pc remove.p hostname% touch rmc hostname% ls rmc rmc hostname% a.out rmc hostname% ls rmc rmc not found |
reset
Pascal supports an optional second argument to the reset procedure. This argument gives an operating system file name. Syntax
reset(file, filename) Arguments
file is a variable having the text or file data type. Return Value
reset does not return any values. Comments
reset gives you permission to read from the file, but not to write to the file.reset(data, 'primes');
reset does an implicit close on the file, hence you can reuse its file variable with a different file. Similarly, if input or output is reset, the current implementation of the product also implicitly closes stdin and stdout.
See also the section on "rewrite," which opens a file for writing.
Example
See the example in the rewrite listing that follows. rewrite
Pascal supports an optional second argument to the rewrite procedure. This argument gives an operating system file name. Syntax
rewrite(file, filename) Arguments
file is a variable having the text or file data type. Return Value
rewrite does not return any values. Comments
rewrite gives you permission to modify a file.
rewrite(data, 'primes');If you do not give an optional second argument, Pascal creates a physical operating system file for you. This file has the same name as the file variable if the file variable is listed in the program header. If the file variable is not listed in the program header, Pascal creates a temporary file with the name #tmp.suffix. The temporary file is deleted when the program terminates.
If the file variable is output, and the second argument is not given, Pascal creates a temporary file, but does not delete it after the program exits.
rewrite does an implicit close on the file, thus you can reuse its file variable with a different file.
See also the section on "reset," which opens a file for reading.
rshft
The rshft function does a logical right shift of an integer value. Syntax
rshft(num, sh) Arguments
num and sh are integer expressions. Return Value
rshft returns a 32-bit integer value. Comments
rshft shifts the bits in num sh spaces to the right. rshft does not preserve the sign bit (leftmost) bit of a number and does not wrap bits from right to left. The value rshft returns is machine-dependent, and is unspecified unless 0 <= sh <= 32. Do not confuse rshft with the arithmetic right shift functions asr and arshft, which preserve the sign bit.
seed
The seed function reseeds the random number generator. Syntax
seed(x) Arguments
x is an integer value. Return Value
seed returns an integer value. Comments
seed sets the random number generator to x and returns the previous seed. If you do not reseed the random number generator, the random function returns the same sequence of random numbers each time you run the program. To produce a different random number (sequence each time the program is run), set the seed with the following statement:x := seed(wallclock);
Example
See the example that follows.
The commands to compile and execute seed.p
|
hostname% pc seed.p hostname% a.out These numbers were generated at random: 75 175 186 260 178 |
seek
The seek procedure changes the current position in the file. Syntax
seek(file, pos) Arguments
file is a variable with the text or file data type. Return Value
seek does not return any values. Comments
The seek procedure is a facility to support random access input/output. It changes the position of a given file that is open for reading or writing.
The commands to compile and execute seek.p
|
hostname% pc seek.p hostname% a.out Initial size of f = 10 elements The 4th element of f = 4 Final size of f =101 elements |
sizeof
sizeof returns the number of bytes the program uses to store a data object. Syntax
sizeof(x, tag1, ... tagN) Arguments
x is any predeclared or user-defined Pascal data type, a variable, a constant, or a string. Return Value
sizeof returns an integer value. Comments
sizeof returns the number of bytes in the data object x. tags correspond to the fields in a variant record. tags are effectively ignored because Pascal allocates records according to the largest variant.sizeof ('') = 0
However, if S is defined as follows:
var S: varying [12] of char; begin S:=''; |
then sizeof (S) = 16. Moreover,
sizeof (''+'') = 4
because the '+' string operator returns a varying string object. Example
sizeof.p Program (Screen 1 of 2)
sizeof.p Program (Screen 2 of 2)
stlimit
The stlimit procedure terminates program execution if a specified number of statements have been executed in the current loop. Syntax
stlimit(x) Arguments
x is an integer value. Return Value
stlimit does not return any values. Comments
To use stlimit, you must include the following code in your source program:{$p+}
When you call stlimit, it tests if x number of statements have been executed in the current loop. If the number of statements executed equals or exceeds x, stlimit stops the program, dumps core, and prints the following message:Statement count limit of x exceeded
If stlimit is used without a loop, it reports the number of statements executed and the CPU time utilized.
Trace/BPT trap (core dumped)
Example
The commands to compile and execute stlimit.p
|
hostname% pc stlimit.p hostname% a.out Hello. Hello. Hello. Hello. Statement count limit of 11 exceeded Trace/BPT trap (core dumped) |
stradd
The stradd procedure adds a string to another string. Syntax
stradd(strdest, strsrc) Arguments
strdest is a variable-length string. Return Value
stradd does not return any values. Comments
stradd adds strsrc to strdest, and is a more efficient operator for the concatenation of strings. Use stradd when a string is constructed by multiple concatenation, with other strings to its end.
The commands to compile and execute stradd.p
|
hostname% pc stradd.p hostname% a.out Hello, world! |
substr
The substr function takes a substring from a string. Syntax
substr(str, p, n) Arguments
str is a variable-length string, a character array, or a character-string constant. Return Value
substr returns a variable-length string. Comments
substr returns a substring beginning at position p and continuing for n characters. If the values of either p or n indicate a character outside the bounds of the string size, Pascal returns a null string. Example
The commands to compile and execute substr.p
|
hostname% pc substr.p hostname% a.out Paris, France |
sysclock
The sysclock function returns the system time consumed by the process. Syntax
sysclock Arguments
sysclock does not take any arguments. Return Value
sysclock returns an integer value. Comments
sysclock returns the system time in milliseconds. See also the clock function, which returns the user time the process consumes. Example
See the example in the clock listing earlier in this chapter. tell
The tell function returns the current position of a given file. Syntax
tell(file) Arguments
file is a variable with the text or file data type. Return Value
tell returns an integer value. Comments
The argument can be either a text file of text type, or a binary file of a certain file of T type. It must be associated with an open file, otherwise an error occurs.
The commands to compile and execute tell.p
|
hostname% pc tell.p hostname% a.out tell(ft) = 0 tell(fi) = 0 tell(ft) = 4 tell(fi) = 1 tell(ft) = 8 tell(fi) = 2 |
time
The time procedure retrieves the current time. Syntax
time(a) Arguments
a is a variable that can be either a character array that is 8 elements long for the "C" locale, or a variable-length string. Return Value
time returns a character string in the form traditional for a given locale. For the "C" locale, the form is hh:mm:ss, where hh is the hour (0 through 23); mm is the minutes (0 through 59); and ss is the seconds (0 through 59). Comments
time uses a 24-hour clock. It puts a zero in front of the hours, minutes, and seconds, so that they always consist of two digits.
trace
The trace routine prints stack traceback without terminating a program. Syntax
trace Arguments
trace does not take any arguments. Return Value
trace does not return any values. Comments
You can use the trace routine for debugging. This routine prints stack traceback information to a file without terminating your program. The name of the traceback file is p.trace, where p is the name of your executable. For example, if the executable is called a.out, then the name of the traceback file is a.out.trace. Example
The Pascal program, trace.p
|
program trace_example; procedure subr(count: integer); begin if (count > 0 ) then subr(count - 1) else trace; end; begin subr(5); end. |
trim
The trim function removes the trailing blanks in a character string. Syntax
trim(input_string) Arguments
input_string is a constant string, a variable-length string, a character array, or a character. Return Value
trim returns a variable-length string equal to the input string without any trailing blanks. If input_string is a null string or contains only blanks, trim returns a null string of length 0. Comments
trim has no effect if its result value is assigned to a fixed-length character string variable. Fixed-length characters are always padded with blanks during assignments. Example
The commands to compile and execute trim.p
|
hostname% pc trim.p hostname% a.out 1234560123456789 16 34560 5 0123401234567891234560123456789 31 |
Type Transfer
The type transfer function changes the data type of a variable, constant, or expression. Syntax
transfer_function(x) Arguments
transfer_function is a predeclared or user-defined Pascal data type. Return Value
A type transfer function returns its argument unchanged in internal value, but with a different apparent type. Comments
Suppose your program contains the following data type declarations:
var x: integer32; y: single; |
To transfer the value of variable x to a floating-point number, you would write:
y := single(x); |
When the argument of a type transfer function is a variable, the size of the argument must be the same as the size of the destination type. However, if the argument to a transfer function is a constant or an expression, Pascal attempts to convert the argument to the destination type because constants and expressions do not have explicit types.
Example
wallclock
The wallclock function returns the elapsed number of seconds since
00:00:00 GMT January 1, 1970. Syntax
wallclock Arguments
wallclock does not take any arguments. Return Value
wallclock returns an integer value. Comments
wallclock can be used with the seed function to generate a random number. It can also be used to time programs or parts of programs. Example
See the example that follows.
The commands to compile and execute wallclock.p
|
hostname% pc wallclock.p hostname% a.out Begin computing fibonacci series. 20 Iterations: Fibonacci(24) = 46368. Elapsed time is 5 seconds. |
write and writeln
Pascal supports the standard form of write and writeln with the following extensions:
writeln(file, exp1:width ..., expN:width)
exp is a variable, constant, or expression of type integer, real, character, boolean, enumerated, or string. exp cannot be a set variable.
width is an integer. width is optional.
To write the internal representation of an expression in octal, use this form:
write(x oct);x is a boolean, character, integer, pointer, or user-defined type. It can also be a constant, expression, or variable.
To write an expression in hexadecimal, use this form:
write(x hex);When you specify a negative field width of a parameter, Pascal truncates all trailing blanks in the array. write and writeln assume the default values in Table 6-15 if you do not specify a minimum field length.
Table 6-15 Default Field Widths
Example
The commands to compile and execute octal.p
|
hostname% pc octal.p hostname% a.out Enter an integer: 10 10 is 12 in octal. 10 is A in hexadecimal. |
xor
The xor function returns the exclusive or of two integer values. Syntax
xor(int1, int2) Arguments
int1 and int2 are integer expressions. Return Value
xor returns an integer value. Comments
Pascal uses Table 6-16 to return the bitwise exclusive or of int1 and int2.
Value of Bit in int1 |
Value of Bit in int2 |
Value of Bit in result |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
If int1 and int2 are different size integers, Pascal converts the smaller integer to the larger integer before it performs the xor operation.
Example
See the example in the land listing on page 142.