Turbo Pascal MSX – Lesson 6

da | Dec 30, 2023 | Programming, Turbo Pascal | 0 comments

MSX Turbo Pascal Column by Stefano Roperto

(part 6)

Chapter 6

PROCEDURES AND FUNCTIONS

Turbo Pascal, as we saw in chapter 4, provides the programmer with a complete collection of procedures and functions that make the language modern and complete. In this chapter we will see the most important ones. It is not necessary to explain them all as many are common to functions present in other languages, including MSX basic, and others are immediately understandable. Turbo Pascal provides procedures and functions for:

  • String manipulation
  • File management
  • Allocation of variables, pointers and memory management
  • Data input and output

In addition to this we have general functions and procedures available.

Let's start with arithmetic functions:

Abs(num) where num is an integer or a real of which you are asked to return the absolute value which will be of the same type as num

Arctan(num) returns the angle in radians of which the tangent is num. Num can be real or integer, but the result is always a real

Cos(num) returns the cosine of num. Num is expressed in radians and can be real or integer. The returned value is always a real

Exp(num) returns the exponential of num. Num can be real or integer, but the result is always a real

Frac(num) returns the fractional part of num. Num can be real or integer, but the result is always a real

Int(num) returns the integer part of num. Num can be real or integer, but the result is always a real

Ln(num) returns the natural logarithm of num. Num can be real or integer, but the result is always a real

Sin(num) returns the sine of num. Num is expressed in radians and can be real or integer, but the result is always a real

Sqr(num) returns the square of num Num can be real or integer, but the result is always a real

Sqrt(num) returns the square root of num Num can be real or integer, but the result is always a real

For scalar types we have some functions available:

Pred(num) returns the predecessor of num in a scalar variable

Next(num) returns the successor of num in a scalar variable

Odd(num) returns true if num is odd and false if num is even. Num must be an integer

Then there are the so-called conversion functions:

Chr(num) returns the character whose ascii code is indicated by num

Ord(var) returns the ordinal of the value var in a set defined by the type var. var can be any scalar type except real, and the result is of type integer

Round(num) returns the num value rounded to the nearest integer. Num must be a real and an integer is returned

Trunc(num) returns the largest integer (if num >=0) or the smallest integer (if num >0). Num must be a real and an integer is returned

And finally the general general purpose functions:

Hi(num) returns the high byte of the integer value num

KeyPressed(num) returns true if a key is pressed or false if no key is pressed

The(num) returns the low byte of the integer value num

Random returns a random number >= 0 <1

Random(num) returns a random number >= 0

ParamCount returns the integer number of parameters passed to the program on the command line separated by space or tab

ParamStr(N) returns the eNessimo parameter from the command line and cannot be greater than ParamCount. ParamStr(0) returns the name of the program

SizeOf(name) returns an integer indicating the number of bytes occupied in memory by the name variable or type

Swaps(num) swaps the high- and low-order bytes of the integer value num

UpCase(chr) returns the uppercase equivalent of the chr character. If it does not exist, no values are changed

Turbo Pascal provides a complete set of functions and procedures dedicated to string management. The string type and the char type are compatible with each other, in fact strings in Turbo Pascal are a data type derived from the char type, technically the string type is similar to an array of characters as can be seen from its definition in this example, with the difference that the element at index 0 contains the length of the array itself:

type

              name = string[50] ;{the characters go from element 1 to element 50, while element 0 contains the value 50, so the name string will occupy 51 bytes in memory. The length limit of a string variable is 255};

begin

name:='Mario Rossi';

if we use the Write instruction passing the name variable as a parameter

Writeln(name);

the program will print on the screen

Mario Rossi

And so far nothing new. Note that the first character of the string name is name[1] or “M” while element 0 name[0] contains the value of the length of the string, in this case name[0]=11, in fact the string ' Mario Rossi' contains 11 characters, and then name[1]='M'… name[10]='s' name[11]='i'. Of course there is much more, the procedures and functions dedicated to strings facilitate the manipulation of the data contained in them:

Delete (str, pos, num)

Delete from the str variable, starting from the position pos, num characters

For example delete('hello friends',1,4) will delete 4 characters starting from the first, therefore the string will become 'friends')

Insert (text, str, pos)

Inserts text into the str string starting from position pos

For example Insert ('dear', 'hello friends',5) the string obtained will be 'hello dear friends' in fact starting from the fifth character, the space between the two words, will insert the string 'dear'.

Str (num, stri)

Converts the num parameter to a string that is stored in the stri variable.

Val (string, value, errcode)

Converts the number contained in the string into the numerical value corresponding to the type indicated by the value variable and places a code other than 0 in errcode if an error occurs

Copy (str, pos, n)

Returns n characters of the string str starting at position pos. If n exceeds the length of the string, a string containing only the characters included in the string is returned; if pos is beyond the length of the string, a null string is returned

Concat (string1, string2, string3, .. stringN)

Returns a string formed by concatenating the strings passed in the arguments. If the length exceeds 255 characters, an error occurs. Concat is used for compatibility with standard Pascal, but the addition operator can be used instead:

writeln(concat('ABC','DEF','GHI')); returns the string 'ABCDEFGHI' the same way as writeln('ABC'+'DEF'+'GHI') which prints 'ABCDEFGHI'

Length (str)

Returns the length of the str parameter

Pos (Objstring,Targetstring)

Returns the position of the first character of the Objstring within the Targetstring. If no occurrence is found, the value 0 is returned

So, as we have seen, Turbo Pascal derives from standard Pascal, the possibilities of which it expands by offering functions, procedures and types, as Borland did, which can be easily implemented and reused in other programs by the user. And we will see in detail later with some examples a small part of what is possible to obtain from the structured language nature typical of Turbo Pascal. In this chapter we looked at the string type in a little more detail, but in Turbo Pascal it is possible to use complex data types such as ARRAYs, RECORDs and SETS. As we have seen, the string type is similar to an array of characters which has in the zero index element the value indicating the length of the string. Now we will see how an array is defined. First of all, let's say that an array is a collection of objects of the same type: we can define arrays of integers, characters, strings, real numbers, but we cannot define an array that contains data of different types. For example

type

days = (Mon,Tue,Wed,Thu,Fri,Sat,Sun)

var

WorkHours :array[l .. 8] of Integer;

Week: array[l .. 7] of days;

You can also define multidimensional arrays for example

Type

WorkHours =array[l .. 8] of Integer;

Week= array[l .. 7] of Workhours;

Var

workingweek: week;

where we have a matrix of 7 x 8 variables.

Let's now look at a simple example program to clarify:

program Matrices;

{the following is a directive to the compiler. tells the compiler to always check the validity of the indexes. control}

{makes program execution slower, so it is best to activate it during debugging ($R+) and deactivate it when releasing the program ($R-)}

{$R+}

Type

WorkHours =array[1..8] of Integer;

Week=  array[1..7] of Labor Hours;

letters = string[20];

Var

workingweek: Week; ;{ this is a 2 dimensional array [1..7,1..8}

simplearray: array[0..100] of real;{ this is an array of reals}

{the following is a string array.}

{It is not possible to directly declare a string array}

{with an instruction like “words:array[0..20] of string[50]”}

{but you need to define a string type first like “letters = string[20];”}

words: array[0..20] of letters;

x,y,k : integer;

{we fill the two-dimensional array with numbers from 1 to n}

begin

k:=1;

for x:=1 to 7  do

for y:=1 to 8  do

begin

workingweek[x,y]:=k;

k:=k+1;

end;

for x:=1 to 7 do

begin

for y:=1 to 8 do

write(workweek[x,y],' ');

writeln;

end;

{we assign a value to an array of reals}

simplearray[5]:=34.56;

{we assign a string to a srting array}

words[0]:='good morning'

end.

As you can see, the possibilities are different and it is easy to imagine how it is possible to define arrays starting from user-defined types, in a simple and effective way. We were saying that an array is a finite collection of objects of the same type, so how can we put objects of different types together? Turbo Pascal provides the type Record which represents a structured data type that allows us to keep data of different types together: numbers, characters, strings, Boolean values, user-defined types, even other records. In MSX Basic the field instruction comes close to the concept of record, but compared to the Turbo Pascal record type it is very limited. Let's see a first example of a record

In the Type section you define the record

type

registry = Record

Name: string[40];

Surname: string[40];

Address1: string[40];

City: string[40];

Cap: string[5];

age: integer;

Weight: real;

Height:real;

end;

{and in the var section the variables of the type defined as record are defined}

var

agenda: registry;

begin

agenda.Name:='Mario'

agenda.Surname:=”Rossi'

end.

Turbo Pascal provides the keyword With which allows you to simplify record management by omitting the variable name and referring only to the fields, like this:

with agenda do

begin

Name:='Mario';

Surname:='Rossi';

….

Weight:=77.4

Height:=1.78

End;

similarly you can declare an array of records by adding the keyword to the variable declaration. In this case, whether you use direct notation or with notation, you must indicate the element of the array:

agenda[x].Name:='Name'

or via the with keyword,

with agenda[x]do

begin

Name:='Mario';

Surname:='Rossi';

….

Weight:=77.4

Height:=1.78

End;

Let's now look at the SET type: A set or, as they say in mathematics, a set is a collection of objects related to each other that can be thought of as a whole. Each object in such a set is called a member or element of the set. Examples of sets could be:

all integers from 1 to 100

all the letters of the alphabet

all prime numbers

two sets are equal to each other only if all the elements of the two sets are equal. There are three operations that can be performed on sets:

union, intersection and relative complement:

The union of two sets, for example A=[1,2,3] and B=[2,8,5], returns a set that includes the members of both sets: A+B=[1,2, 3,5,8]

The intersection of two sets, for example A=[1,2,3] and B=[2,8,5], returns a set that includes only the members present in both sets: A*B=[2]

The relative complement of B with respect to A, for example A=[1,2,3] and B=[2,8,5], returns a set whose members belong to A, but not to B: AB=[1, 3]

Although in mathematics there are no restrictions on the types of objects that can be contained in a set, Turbo Pascal offers a restricted form of Set, that is, a SET can only contain objects of the same type, called the base type, and the base type must be a simple data type except the real type. An example of a SET declaration is the following:

type

Days of the week set of (Mon,Tue,Wed,Thu,Fri,Sat,Sun);

Characters Set of char;

the operators that allow you to operate on sets are the following:

+            produces the union of two sets

–             produces the relative complement of two sets

*            produces the intersection of two sets

=            checks the equality between two sets

<>         checks the inequality between two sets

>=         checks whether all members of the second set are included in the first

<=         checks whether all members of the first set are included in the second

In          checks if an element is present in the set indicated in the operation: if a=9 and b=set of integer then the expression

If (a in b) then… is true

Now we have enough information to start writing some programs in Turbo Pascal. In the next chapter we will take a look at files, so that we can also have the ability to save and load data from our programs and then we will conclude with creating and using libraries in our programs in Turbo Pascal

 

0 Comments

Submit a Comment

Your email address will not be published. I campi obbligatori sono contrassegnati *

Turbo Pascal MSX – Lezione 7

Turbo Pascal MSX – Lesson 7

FILES (part 1) Turbo Pascal provides a complete set of instructions for managing files. The FILE type is represented by a sequence of objects of the same type, the size of a file is not determined by the type, but a pointer is provided that moves...

Turbo Pascal MSX – Lesson 5

MSX Turbo Pascal Column by Stefano Roperto (part 5) Lesson 5 Loops and control instructions In turbo Pascal there are the usual iterative structures present in all high-level languages, the FOR DO loop and the REPEAT UNTIL and WHILE DO structures. ...

Turbo Pascal MSX – lesson 3

MSX Turbo Pascal Column by Stefano Roperto (part 3) Chapter 3 The data type defines the set of values that a variable can take on. The more complex data types that can be defined by the programmer are all derived from the standard types. The guys...

Turbo Pascal MSX – lesson 2

MSX Turbo Pascal Column by Stefano Roperto (part 2) Chapter 2 Introduction to the Turbo Pascal language We will now see how a program in Turbo Pascal is structured. We don't need to understand everything right now, we're just seeing the typical structure...