Pascal Programming/Variables and Constants

From testwiki
Jump to navigation Jump to search

Like all programming languages, Pascal provides some means to modify memory. This concept is known as variables. Variables are named chunks of memory. You can use them to store data you cannot predict.

Constants, on the other hand, are named pieces of data. You cannot alter them during run-time, but they are hard-coded into the compiled executable program. Constants do not necessarily occupy any dedicated unique space in memory, but facilitate writing clean and understandable source code.

Declaration

In Pascal, before you are even allowed to use any variable or constant you have to declare them, like virtually any symbol in Pascal. A declaration makes a certain symbol known to the compiler and possibly instructs it to make the necessary provisions for their effective usage, that means – in the context of variables – earmark some piece of memory.

A declaration is always a two-tuple (identifier,definition), to be more specific, variables are declared like (identifier,data type) and constant declarations are (identifier,literal) tuples. A tuple is an ordered collection. You may not reverse or rearrange its items without the tuple rendering to be different.

Template:XNote

Identifiers

Structure

Template:Quote

Letters refers to the modern Latin alphabet, that is all letters you use in writing English words, and digits are Western Arabic digits.

Usage

As you infer from the quote’s last sentence, the casing of letters does not matter: Foo and fOO are both the same identifier, just different representations.

Identifiers are used simply by writing them out at a suitable position.

Significant characters

In the age Pascal was developed in, computer memory was a precious resource. In order to build a working compiler, however, the notion of significant characters was introduced. A significant character of an identifier is a character that contributes to distinguishing two identifiers from one another.

Some programming languages had a limit of 8 (eight) characters. This led to very cryptic identifiers. Today, however, the limit of significant characters is primarily governed by usability: The programmer eventually has to type them out if no Template:Abbr supports some auto-completion mechanism. The Template:Abbr, for example, has a limit of 127 characters: Template:Quote

Template:XNote

Note, allowing _, too, is an ISO 10206 (“Extended Pascal”) extension, but – unlike the Template:Abbr – it imposes the restriction that an identifier may neither begin or end an identifier, nor may two underscores appear one another.

Variables

Variable section

Variables are declared in a dedicated section, the var-section.

program varDemo(input, output);
var
	number: integer;
begin
	write('Enter a number: ');
	readLn(number);
	writeLn('Great choice! ', number, ' is awesome.');
end.

When the compiler processes the var-section it will set as much memory aside as is required by its associated data type. Here, we instruct the compiler to reserve space for an integer. An integer is a data type that is part of the programming language, thus it is guaranteed to be present regardless of the used compiler. It stores a subset of ℤ, the set of integers, like for example 42, 1337 or -1.

Data type

Data type refers to the combination of a permissible range of values and permissible operations on this range of values. Pascal defines some basic data types as part of the language. Apart from integer there are also:

char
A character, like a Latin letter or Western Arabic digit, but also spaces and other characters.
real
A subset of ℚ, that is – due to computer’s binary nature – the set of rational numbers. Examples are 0.015625 (2−6) or 73728.5 (216 + 213 + 2−1).
Boolean
A Boolean value, that is false or true.

Each data type defines how data are laid out in memory. In a high-level language, such as Pascal, it is not of the programmer’s concern how exactly the data are stored, but the processor (i. e. in most cases a compiler) has to define it.

We will revisit all data types later on.

Reading from the console

As you may have noticed, the example above contains readLn(number) and the program header also lists input. ReadLn will (try to) read data from the (optionally named) source and store the (interpreted) values into the supplied parameters discarding any line-end characters. If the source is not specified, like it is the case here, input is assumed, thus readLn(number) is equivalent to readLn(input, number), but shorter.

When the program is run, it will stop and wait for the user to input a number, that is a literal that can be converted into the argument’s data type. Template:Code:Error You have to indicate in your program’s accompanying documents – the user manual – how and when the user needs to input data. Later we will learn how to treat erroneous input, but this is too complex for now.

More variables

There can be as many var-sections as necessary, but they may not be empty. There is also a shorthand syntax for declaring many variables of the same type:

var
	foo, bar, x: integer;

This will declare three independent variables, all of the integer data type. Nonetheless, different types have to appear in different declarations:

var
	x: integer;
	itIsSunnyInPhiladelphia: Boolean;

Constants

Constant section

program constDemo(output);
const
	answer = 42;
begin
	writeLn('The answer to the Ultimate Question of ',
	        'Life, the Universe, and Everything, is: ',
	        answer);
end.

Usage

As already mentioned in the introduction, a constant may never change its value, but you have to modify the source code. Consequently, the name of a constant cannot appear on the left-hand side of an assignment.

Pre‑defined constants

There are some already predefined constants:

maxInt
This is the maximum integer value an integer variable could assume. There is no minimum integer constant, but it is guaranteed that a integer variable can at least store the value -maxInt.
maxChar
Likewise, this is the maximum char value a char variable could assume, where maximum refers to the ordinal value using the built-in ord function.
maxReal, minReal and epsReal
Are defined by the “Extended Pascal” standard.
false and true
Refer to Boolean values.

Rationale

Pascal was designed, so – among other considerations – it could be compiled in one pass, from top to bottom: The reason being to make compiling fast and simple. Distinguishing between variables and constants allows the processor to simply substitute any occurrence of a constant identifier to be replaced by its value. Thus, a constant does not need any special treatment like a variable, yet allows the programmer to reuse reappearing data.

Tasks

Template:Question-answer Template:- Template:Question-answer Template:- Template:Question-answer Template:NewpageTemplate:Hide in print

References: Template:Smallrefs


Template:Auto navigation