Turbo Pascal MSX – lesson 2

da | Nov 18, 2023 | Programming, Turbo Pascal | 0 comments

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 of a program:
A program in Turbo Pascal always begins with the reserved word “program” followed by the name of the program, and each line (with some exceptions) always ends with a semicolon. Let's see an example:

program name_of_the_program; {This is a comment} {this section declares labels (label), constants (const), user-defined types (type), variables (var), procedures and functions. All objects that will be used in the program must be declared before use} Var {variable declaration} X,y : integer; str : string[30]; pg: real;
Concrete procedures; {procedure declaration} {declarations, as for the main program} begin {procedure start} clrscr; {statements} end; {end of procedure}
function Cls (param_1:type1,var param_2:type2,..param_n:typen):return_type ; {function declaration} {declarations, as for the main program} begin {function start} Cls := value; {function statements} end; {end of function}
begin {start of program program instructions; {code} end. {end of program}

Seen like this it seems complicated, but in reality it isn't. Let's see a real example and then write the same program in MSX Basic.

Program power; var x,y:integer; begin clrscr; writeln('Enter the number to square'); readln(x); y:=x*x; writeln('the square of ',x,' and '' ',y); end.

The first line is the program header and is mandatory. The variables x and y are of type integer and are declared before their use. The program always starts with the begin keyword and ends with the end keyword. followed by the period. The instructions between begin and end form the body of the program (for those who know C, begin and end have the same function as braces { } ). Groups of instructions that are coherent with each other must be enclosed between begin and end; in this case, however, end is followed by a semicolon (;), because the period (.) indicates the end of the program. When defining subroutines, procedures and user-defined functions, we follow exactly the same pattern, the only difference is that the definition of a subroutine always ends with end and a semicolon. But let's now see the same program written in Basic MSX:

10 CLS 20 PRINT “Enter the number to square” 25 INPUT X 30 Y=X*X 50 PRINT “the square of X is ”; Y 60 END

The equivalent in basic is certainly more compact, but Turbo Pascal, for example in the definition of subprograms, offers greater versatility, in fact a procedure or a function has the same structure as a program, therefore it can have access to its own data, define your own types, define other procedures and functions internally, return values to the main program. Let's rewrite the same program using a function. In this case we implement the squaring operation which is carried out by a function, to which we pass as a parameter the number to be squared and which returns the value of the power. Let's see an example:

Program power; var x,y:integer; {here we define the function} function square(number : integer): integer; begin square:= number * number; end; {this is where the main program begins} begin clrscr; writeln('Enter the number to square'); readln(x); y:=square (x); writeln('the square of ',x,' and '' ',y); {or you can directly insert the function call into an expression} writeln('the square of ',x,' is'' ',square(x)); end.

The same program in basic would be as follows

10 CLS 20 DEF FN SQUARE (X)=X^2 30 INPUT “Enter the number to square”, X 40 Y=FN SQUARE(X) 50 PRINT “the square of Y 55 REM OR 60 PRINT “the square of FN SQUARE(X) 70 END

 

As you can see, in Turbo Pascal the definition of a function follows the same pattern as the definition of the program
Function name_of_the_function (parameters to pass to the function): return type
Begin
Function instructions
End;
In this case the definition of the function is as follows:

function square(number : integer): integer; begin square:= number * number; end;

Naturally we can improve the function so that it takes as input two numbers, base and exponent, and returns the base value raised by exponent. For example

function power(base real, exponent:real): real; var i: integr; r: real; begin r:=1; for i := 1 to abs(trunc(exponent)) do r := r * base; power:=r; end;

therefore within a program we can use, for example, instruction

x:=3; y:=4; k:= power (x,y);

and use the value of K (i.e. 3 to the power of 4) where K will have a value of 81.
The function assigns the value of the first parameter (x) to the base variable and the value of the second parameter (Y) to the exponent variable. After the calculations, it assigns the calculated value to the power function and returns it to the calling expression (in this case ak).
Of course, the function can be improved, for example checks for special cases of exponent = 0 or negative exponent are missing.

In any case, we will return to the functions and procedures in a dedicated chapter. I only intended to present the structure of a program in Turbo Pascal and show its potential. Of course there is much more!

 

 

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 6

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...

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...