Variables
When solving a problem you identify the inputs and outputs and then determine the variables. You could think of a variable as a box which can store one value. Thus labelling a variable, with a meaningful name, is the same as writing on the outside of that box what is inside. The variable name is known as the identifier.
Programming languages have a set of rules upon what makes a valid identifier or rather, which characters can be used to name a variable. Unlike other languages, Pascal is not case sensitive so using the name: “phrase” and “Phrase”, would relate to the same variable. When creating a variable, it is conventional to use casing to show the start of a new word; PascalVariable, for example.
Only use single letter identifiers when conventional to do so. Make sure your identifiers have a meaning so they make sense to any programmer (including yourself when looking back at old code). Have a look below, which is easier to understand? Which best tells us what the variable does?
var
Variable1 :integer;
Variable2 :string;
var PhraseLength:integer; MyPhrase :string;
This is a true story that I’m going to tell you, relating to why using meaningful identifiers is important. One of my friends needed help with a program that he was making, so I agreed to have a look at the source code to help him out. It was a language which I didn’t know but I figured I would understand it because of my experience with languages. The following is his code:
//program draws a rectangle of stars
$i = 4; $k = 5; $z = "*";
for( $q = 0; $q <= $i; $q += 1 ){
for( $t = 0; $t <= $k; $t += 1){ echo $z; } echo "<br />"; }
When looking at the code I had no idea what was going on, so in the end I had to tell him that I couldn’t help. This was down to the meaningless identifiers and the lack of indentation which made the code hard to read. Here is another version of the same code, is it easier to understand?
//program draws a rectangle of stars
$rectangleHeight = 4;
$rectangleWidth = 5;
$rectangleSymbol = "*";
for( $LowestHeight = 0;
$LowestHeight <= $rectangleHeight;
$LowestHeight += 1 ){
for( $LowestWidth = 0;
$LowestWidth <= $rectangleWidth;
$LowestWidth += 1){
echo $rectangleSymbol;
}
echo "<br />";
}
Constants
A constant is a value that does not change throughout your program and keeps your program easy to maintain. An example of a constant you might use in program might be one called VAT, as the VAT won’t be changed throughout the program but however changed once at the top of the program. Below is an example of where it would be declared and how it could be used:
program ConstantDeclaration;
{$APPTYPE CONSOLE}
uses SysUtils;
const
VAT = 15;
var
ComputerPrice: real;
begin
{without VAT}
ComputerPrice := 100;
{with VAT}
ComputerPrice := ((ComputerPrice * VAT ) / 100 ) + ComputerPrice;
writeln( 'Computer with VAT costs £', ComputerPrice:2:2 );
end.
Try changing the VAT and the see the new VAT being added to the price. Try giving VAT a new value in the program block, between “begin” and “end.”, does it work?