The main differences of PascalABC.NET

The main differences of PascalABC.NET

PascalABC.NET has basic compatibility with Turbo Pascal, which should allow porting older programs with less effort. However, due to this, there has been a tendency to use PascalABC.NET only in the style of Turbo Pascal and to ignore modern designs. Here I want to briefly explain the main differences and show the correct use of this dialect.

Standard functions for input and output

Not the most important difference, but it will be useful for further comparison.

In Turbo Pascal, input and output are used procedures read[ln] and write[ln] in accordance.

var 
  a, b: integer;
  
begin
  write('a="); 
  readln(a);
  
  write("b='); 
  readln(b);
  
  writeln('сумма ', a, ' и ', b, ' равна ', a+b);
end.

PascalABC.NET suggests using for input functions with an explicit indication of the data type. They do not just change the value of the specified variable, but return a calculated value.

write('a="); 
a:= ReadlnInteger;

Similar functions exist of other types (real, string, etc.). Moreover, these functions can display input prompts themselves.

a:= ReadReal("a=");

A subroutine is used for output Print[ln], which automatically separates the passed parameters with spaces. Thus, you can remove extra spaces from the output command.

As a result, the program looks like this:

var 
  a, b: integer;
  
begin
  a:= ReadlnInteger("a=");
  b:= ReadlnInteger("b =');
  
  Println('сумма', a, 'и', b, 'равна', a+b);
end.

Local variables

Turbo Pascal variables are declared before the main program or before the subroutine body. For example, solving a quadratic equation may require such a set of variables

var
  a, b, c, d, x1, x2: real; // коэффициенты, дискриминант, корни
  count: integer; // кол-во найденных корней

The result is an accumulation of variables with excessive scope. From the code, it is difficult to understand at what point this or that variable begins to be used. It is difficult to determine whether a variable is used only in the main program or is also accessed by subroutines. It is more difficult to determine the purpose of this variable.

In PascalABC.NET, variables can be declared in code. This should be done as close as possible to the place of the first use of the variable.

Let’s look at the example of a program that adds numbers.

begin
  var a: integer; // объявляем
  a:= ReadlnInteger('a="); // инициализируем
  
  var b: integer;
  b:= ReadlnInteger("b =');
  
  println('сумма', a, 'и', b, 'равна', a+b);
end.

Variable declaration can be combined with initialization.

begin
  var a: integer:= ReadlnInteger('a=");
  var b: integer:= ReadlnInteger("b =');
  
  println('сумма', a, 'и', b, 'равна', a+b);
end.

Moreover, the compiler understands that we are initializing a variable to a type value integer and do not specify the type explicitly. Auto-inference of types works.

begin
  var a:= ReadlnInteger; // a: integer
  var b:= 24.5;          // b: real
  
  println('сумма', a, 'и', b, 'равна', a+b);
end.

All this can be applied to the for loop

for var i:= 0 to 10 do ;

Dynamic arrays

Turbo Pascal uses static arrays, the main problem of which is the need to specify the size of the array at the stage of program creation. As a result, arrays are most often created “with a margin”. PascalABC.NET suggests using dynamic arrays instead.

begin
  var n:= ReadLnInteger('Введите размер массива:');
  
  var arr: array of integer;
  arr:= new integer[n];
  
  var arr2:= new integer[n]; // автовывод типа
end.

Such arrays are indexed from zero.

In addition to the standard filling of the array by indices, there are a number of other methods.

begin
  var x:= 1;
  
  // заполнение известными значениями
  var arr:= |1, 10, 43, x|; 
  
  // чтение 10 значений из консоли
  var arr2:= ReadArrInteger(10);
end.

Properties and Type Extensions

Each standard data type has a fairly large number of properties and extension methods. These are elements available in dot notation.

Example for integer

begin
  var x:= 2;
  var f:= x.IsEven;   // чётное ли число
  var f2:= x.Divs(4); // делится ли на 4
  x.Print;            // вывод в консоль
end.

Example for dynamic arrays:

begin
  var arr:= |1, 31, 40, 4, 1|;
  arr.Sort; // сортировка
  var len:= arr.Length; // получение размера
  var max:= arr.Max; // получение максимального
  var count:= arr.CountOf(1); // кол-во указанного элемента
end.

Many of these array extensions relate to LINQ, but that’s a pretty big topic.

This allows PascalABC.NET code to look like a chain of methods. For example, this program finds the number of twos in the notation of a number

begin
  ReadlnInteger.ToString.CountOf('2').Print;
end.

By the way .Print[ln] all basic types can be used. This is especially useful in long expressions.

Tuples

Tuples are used to store multiple values ​​of a single variable. This can be useful when the values ​​are closely related to each other. For example, the coordinates of a point.

begin
  var point: (integer, integer);
  point:= (1, 2);
  
  print(point.Item1, point.Item2);
end.

The standard library has functions that read multiple values ​​from the console at once and return a tuple.

begin
  var point1:= ReadInteger2;
  print(point1.Item1, point1.Item2);
  
  var point2:= ReadReal2;
  print(point2.Item1, point2.Item2);
end.

A tuple can be unpacked into separate variables

begin
  var point:= (1, 4, 2);
  
  var x, y, z: integer;
  
  (x, y, z):= point;
  
  Print(y); // 4
end.

Finally, variable declarations can be combined with unpacking

begin
  var point:= (1, 4, 2);
    
  var (x, y, z):= point;
  
  Print(y); // 4
end.

Conclusion

The relevance of the article is caused by the lack of processed information on this language rather than the complexity of the material.

Of course, I touched on only a small part of the language differences. However, I tried to convey the main ideological points of PascalABC.NET programming.

Write modern code!

Related posts