Functions

by Heemesh Vara: L6 Age ~16

Introduction

This program, now available as a web version, demonstrates the useful functions DateToStr, Ord, Chr and ReverseString, which the user can choose from a menu. The menu code is a good illustration of a case statement. The program also shows that the text can be highlighted; in this case using a red background.

The Program

program Functions;
{$APPTYPE CONSOLE}

{
    Copyright (c) 2010 Heemesh Vara
    
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
}

uses
  SysUtils,StrUtils,Windows;
var
  Choice : integer; //Global Var

procedure DisplayDate;
begin
  Writeln(DateToStr(Date));                   
end;

procedure DisplayASCII;
var
  MyChar : char;
begin
  Writeln('Please type the character.');
  Readln(MyChar);
  writeln('Its ASCII value is ',Ord(MyChar),'.');
end;

procedure DisplayReverseString;
var
  MyString : String;
begin
  Writeln('Please Type in a Word or Sentence.');
  Read(MyString);
  Writeln('The Word or Sentence reversed is ',ReverseString(MyString),'.');
end;

procedure DisplayCharacter;
var
  MyCode : byte;
begin
  Writeln('Please type in the ASCII code.');
  Read(MyCode);
  Writeln('This represents ',Chr(MyCode),'.');
end;

procedure Quit;
begin
  Writeln('I hope you found this useful.');
  Readln;
end;

procedure ShowMenu;
begin
  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_RED);
  Writeln;
  Writeln('Please enter the number of your chosen option.');
  Writeln('1: The Date');
  Writeln('2: The ASCII code for a letter');
  Writeln('3: Reverse Word');
  Writeln('4: The character for an ASCII code');
  Writeln('5: Quit the program');
end;

procedure UserChoice;
begin
  Readln(Choice);
  case Choice of
  1: DisplayDate;
  2: DisplayASCII;
  3: DisplayReverseString;
  4: DisplayCharacter;
  5: Quit;
  end;
end;

begin
  repeat
    ShowMenu;
    UserChoice;
  until Choice = 5;

end.

Remarks

Could you write a program like this to demonstrate the use of some of the many other inbuilt Pascal functions?

Programming - a skill for life!

Student programs to inspire you!