1.2 Procedures and Functions

AssignCrt

Declaration:
Procedure AssignCrt (Var F: Text);
Description:
AssignCrt Assigns a file F to the console. Everything written to the file F goes to the console instead. If the console contains a window, everything is written to the window instead.
Errors:
None.
See also:
Window (55)

Listing: crtex/ex1.pp


Program Example1;
uses Crt;

{ Program to demonstrate the AssignCrt function. }

var
  F : Text;
begin
  AssignCrt(F);
  Rewrite(F); { Don't forget to open for output! }
  WriteLn(F,'This is written to the Assigned File');
  Close(F);
end.

CursorBig

Declaration:
Procedure CursorBig ;
Description:
Makes the cursor a big rectangle. Not implemented on LINUX.
Errors:
None.
See also:
CursorOn (45), CursorOff (45)

ClrEol

Declaration:
Procedure ClrEol ;
Description:
ClrEol clears the current line, starting from the cursor position, to the end of the window. The cursor doesn’t move
Errors:
None.
See also:
DelLine (46), InsLine (48), ClrScr (44)

Listing: crtex/ex9.pp


Program Example9;
uses Crt;

{ Program to demonstrate the ClrEol function. }
var
  I,J : integer;

begin
  For I:=1 to 15 do
    For J:=1 to 80 do
      begin
      gotoxy(j,i);
      Write(j mod 10);
      end;
  Window(5,5,75,12);
  Write('This line will be cleared from',
        ' here till the right of the window');
  GotoXY(27,WhereY);
  ReadKey;
  ClrEol;
  WriteLn;
end.

ClrScr

Declaration:
Procedure ClrScr ;
Description:
ClrScr clears the current window (using the current colors), and sets the cursor in the top left corner of the current window.
Errors:
None.
See also:
Window (55)

Listing: crtex/ex8.pp


Program Example8;
uses Crt;

{ Program to demonstrate the ClrScr function. }

begin
  Writeln('Press any key to clear the screen');
  ReadKey;
  ClrScr;
  Writeln('Have fun with the cleared screen');
end.

CursorOff

Declaration:
Procedure CursorOff ;
Description:
Switches the cursor off (i.e. the cursor is no longer visible). Not implemented on LINUX.
Errors:
None.
See also:
CursorOn (45), CursorBig (43)

CursorOn

Declaration:
Procedure CursorOn ;
Description:
Switches the cursor on. Not implemented on LINUX.
Errors:
None.
See also:
CursorBig (43), CursorOff (45)

Delay

Declaration:
Procedure Delay (DTime: Word);
Description:
Delay waits a specified number of milliseconds. The number of specified seconds is an approximation, and may be off a lot, if system load is high.
Errors:
None
See also:
Sound (52), NoSound (50)

Listing: crtex/ex15.pp


Program Example15;
uses Crt;

{ Program to demonstrate the Delay function. }
var
  i : longint;
begin
  WriteLn('Counting Down');
  for i:=10 downto 1 do
   begin
     WriteLn(i);
     Delay(1000); {Wait one second}
   end;
  WriteLn('BOOM!!!');
end.

DelLine

Declaration:
Procedure DelLine ;
Description:
DelLine removes the current line. Lines following the current line are scrolled 1 line up, and an empty line is inserted at the bottom of the current window. The cursor doesn’t move.
Errors:
None.
See also:
ClrEol (44), InsLine (48), ClrScr (44)

Listing: crtex/ex11.pp


Program Example10;
uses Crt;

{ Program to demonstrate the InsLine function. }

begin
  ClrScr;
  WriteLn;
  WriteLn('Line 1');
  WriteLn('Line 2');
  WriteLn('Line 2');
  WriteLn('Line 3');
  WriteLn;
  WriteLn('Oops, Line 2 is listed twice,',
          ' let''s delete the line at the cursor postion');
  GotoXY(1,3);
  ReadKey;
  DelLine;
  GotoXY(1,10);
end.

GotoXY

Declaration:
Procedure GotoXY (X: Byte; Y: Byte);
Description:
Positions the cursor at (X,Y), X in horizontal, Y in vertical direction relative to the origin of the current window. The origin is located at (1,1), the upper-left corner of the window.
Errors:
None.
See also:
WhereX (54), WhereY (54), Window (55)

Listing: crtex/ex6.pp


Program Example6;
uses Crt;

{ Program to demonstrate the GotoXY function. }

begin
  ClrScr;
  GotoXY(10,10);
  Write('10,10');
  GotoXY(70,20);
  Write('70,20');
  GotoXY(1,22);
end.

HighVideo

Declaration:
Procedure HighVideo ;
Description:
HighVideo switches the output to highlighted text. (It sets the high intensity bit of the video attribute)
Errors:
None.
See also:
TextColor (53), TextBackground (52), LowVideo (50), NormVideo (50)

Listing: crtex/ex14.pp


Program Example14;
uses Crt;

{ Program to demonstrate the LowVideo, HighVideo, NormVideo functions. }

begin
  LowVideo;
  WriteLn('This is written with LowVideo');
  HighVideo;
  WriteLn('This is written with HighVideo');
  NormVideo;
  WriteLn('This is written with NormVideo');
end.

InsLine

Declaration:
Procedure InsLine ;
Description:
InsLine inserts an empty line at the current cursor position. Lines following the current line are scrolled 1 line down, causing the last line to disappear from the window. The cursor doesn’t move.
Errors:
None.
See also:
ClrEol (44), DelLine (46), ClrScr (44)

Listing: crtex/ex10.pp


Program Example10;
uses Crt;

{ Program to demonstrate the InsLine function. }

begin
  ClrScr;
  WriteLn;
  WriteLn('Line 1');
  WriteLn('Line 3');
  WriteLn;
  WriteLn('Oops, forgot Line 2, let''s insert at the cursor postion');
  GotoXY(1,3);
  ReadKey;
  InsLine;
  Write('Line 2');
  GotoXY(1,10);
end.

KeyPressed

Declaration:
Function KeyPressed : Boolean;
Description:
The Keypressed function scans the keyboard buffer and sees if a key has been pressed. If this is the case, True is returned. If not, False is returned. The Shift, Alt, Ctrl keys are not reported. The key is not removed from the buffer, and can hence still be read after the KeyPressed function has been called.
Errors:
None.
See also:
ReadKey (51)

Listing: crtex/ex2.pp


Program Example2;
uses Crt;

{ Program to demonstrate the KeyPressed function. }

begin
  WriteLn('Waiting until a key is pressed');
  repeat
  until KeyPressed;
 { The key is not Read,
   so it should also be outputted at the commandline}
end.

LowVideo

Declaration:
Procedure LowVideo ;
Description:
LowVideo switches the output to non-highlighted text. (It clears the high intensity bit of the video attribute)
Errors:
None.
See also:
TextColor (53), TextBackground (52), HighVideo (48), NormVideo (50)

For an example, see HighVideo (48)

NormVideo

Declaration:
Procedure NormVideo ;
Description:
NormVideo switches the output to the defaults, read at startup. (The defaults are read from the cursor position at startup)
Errors:
None.
See also:
TextColor (53), TextBackground (52), LowVideo (50), HighVideo (48)

For an example, see HighVideo (48)

NoSound

Declaration:
Procedure NoSound ;
Description:

Stops the speaker sound. This call is not supported on all operating systems.

Errors:
None.
See also:
Sound (52)

Listing: crtex/ex16.pp


Program Example16;
uses Crt;

{ Program to demonstrate the Sound and NoSound function. }

var
  i : longint;
begin
  WriteLn('You will hear some tones from your speaker');
  while (i<15000) do
   begin
     inc(i,500);
     Sound(i);
     Delay(100);
   end;
  WriteLn('Quiet now!');
  NoSound; {Stop noise}
end.

ReadKey

Declaration:
Function ReadKey : Char;
Description:

The ReadKey function reads 1 key from the keyboard buffer, and returns this. If an extended or function key has been pressed, then the zero ASCII code is returned. You can then read the scan code of the key with a second ReadKey call. Remark. Key mappings under Linux can cause the wrong key to be reported by ReadKey, so caution is needed when using ReadKey.

Errors:
None.
See also:
KeyPressed (49)

Listing: crtex/ex3.pp


Program Example3;
uses Crt;

{ Program to demonstrate the ReadKey function. }

var
  ch : char;
begin
  writeln('Press Left/Right, Esc=Quit');
  repeat
    ch:=ReadKey;
    case ch of
     #0 : begin
            ch:=ReadKey; {Read ScanCode}
            case ch of
             #75 : WriteLn('Left');
             #77 : WriteLn('Right');
            end;
          end;
    #27 : WriteLn('ESC');
    end;
  until ch=#27 {Esc}
end.

Sound

Declaration:
Procedure Sound (hz : word);
Description:
Sounds the speaker at a frequency of hz. Under WINDOWS, a system sound is played and the frequency parameter is ignored. On other operating systems, this routine may not be implemented.
Errors:
None.
See also:
NoSound (50)

TextBackground

Declaration:
Procedure TextBackground (CL: Byte);
Description:

TextBackground sets the background color to CL. CL can be one of the predefined color constants.

Errors:
None.
See also:
TextColor (53), HighVideo (48), LowVideo (50), NormVideo (50)

Listing: crtex/ex13.pp


Program Example13;
uses Crt;

{ Program to demonstrate the TextBackground function. }

begin
  TextColor(White);
  WriteLn('This is written in with the default background color');
  TextBackground(Green);
  WriteLn('This is written in with a Green background');
  TextBackground(Brown);
  WriteLn('This is written in with a Brown background');
  TextBackground(Black);
  WriteLn('Back with a black background');
end.

TextColor

Declaration:
Procedure TextColor (CL: Byte);
Description:

TextColor sets the foreground color to CL. CL can be one of the predefined color constants.

Errors:
None.
See also:
TextBackground (52), HighVideo (48), LowVideo (50), NormVideo (50)

Listing: crtex/ex12.pp


Program Example12;
uses Crt;

{ Program to demonstrate the TextColor function. }

begin
  WriteLn('This is written in the default color');
  TextColor(Red);
  WriteLn('This is written in Red');
  TextColor(White);
  WriteLn('This is written in White');
  TextColor(LightBlue);
  WriteLn('This is written in Light Blue');
end.

TextMode

Declaration:
procedure TextMode(Mode: Integer);
Description:
TextMode sets the textmode of the screen (i.e. the number of lines and columns of the screen). The lower byte is use to set the VGA text mode.

This procedure is only implemented on DOS.

Errors:
None.
See also:
Window (55)

WhereX

Declaration:
Function WhereX : Byte;
Description:

WhereX returns the current X-coordinate of the cursor, relative to the current window. The origin is (1,1), in the upper-left corner of the window.

Errors:
None.
See also:
GotoXY (47), WhereY (54), Window (55)

Listing: crtex/ex7.pp


Program Example7;
uses Crt;

{ Program to demonstrate the WhereX and WhereY functions. }

begin
  Writeln('Cursor postion: X=',WhereX,' Y=',WhereY);
end.

WhereY

Declaration:
Function WhereY : Byte;
Description:

WhereY returns the current Y-coordinate of the cursor, relative to the current window. The origin is (1,1), in the upper-left corner of the window.

Errors:
None.
See also:
GotoXY (47), WhereX (54), Window (55)

Listing: crtex/ex7.pp


Program Example7;
uses Crt;

{ Program to demonstrate the WhereX and WhereY functions. }

begin
  Writeln('Cursor postion: X=',WhereX,' Y=',WhereY);
end.

Window

Declaration:
Procedure Window (X1, Y1, X2, Y2: Byte);
Description:
Window creates a window on the screen, to which output will be sent. (X1,Y1) are the coordinates of the upper left corner of the window, (X2,Y2) are the coordinates of the bottom right corner of the window. These coordinates are relative to the entire screen, with the top left corner equal to (1,1) Further coordinate operations, except for the next Window call, are relative to the window’s top left corner.
Errors:
None.
See also:
GotoXY (47), WhereX (54), WhereY (54), ClrScr (44)

Listing: crtex/ex5.pp


Program Example5;
uses Crt;

{ Program to demonstrate the Window function. }

begin
  ClrScr;
  WriteLn('Creating a window from 30,10 to 50,20');
  Window(30,10,50,20);
  WriteLn('We are now writing in this small window we just created, we '+
          'can''t get outside it when writing long lines like this one');
  Write('Press any key to clear the window');
  ReadKey;
  ClrScr;
  Write('The window is cleared, press any key to restore to fullscreen');
  ReadKey;
{Full Screen is 80x25}
  Window(1,1,80,25);
  Clrscr;
  Writeln('Back in Full Screen');
end.