Variables: Difference between revisions

2,731 bytes added ,  9 years ago
Added Algol W
(Added Algol W)
Line 46:
There are more rules regarding arrays, variables containing pointers, dynamic allocation,
and initialization that are too extensive to cover here.
 
=={{header|ALGOL W}}==
Algol W is block structured, following the rules established by Algol 60.
There are variables of the following basic types:
integer, real, long real, complex, long complex, bits, logical, string.
 
Logical variables (usually called boolean in other languages) hold true/false values.
Bits variables hold bit strings.
Strings are fixed length, between 1 and 256 characters long. The length is part of the type.
case is not significant in variable names.
 
Declarations must appear at the beginning of the block they are contained in,
before the first executable statement.
Declaration is separate from initialisation - there is no separate initialisation syntax (except for fields of a record - see below), normal assignments are used:
<lang algolw>% declare some variables %
integer a1, a2; real b; long real c; complex d; long complex f;
logical g; bits h; string(32) j;
 
% assign "initial values" %
f := d := c := b := a2 := a1 := 0; % multiple assignment %
g := false; h := #a0; j := "Hello, World!";
</lang>
 
Records can be declared, composed of fields of the basic types and references to records. E.g.:
<lang algolw>record R1 ( integer length; string(256) text );
reference(R1) ref1, ref2;</lang>
In the above, R1 is a structure containing an integer and a string. Ref1 and ref2 are variables that will refer to instances of the R1 structure.
References can be declared that can refer to a number of different record structures. The allowable references must be specified in the declaration E.g.:
<lang algolw>record person( string(32) name, integer age );
record date( integer day, month, year );
reference(person, date) ref3;</lang>
In the above, ref3 can hold references to either a person or a date.
Variables that are references to the basic types are not allowed. E.g.:
<lang algolw>reference(integer) refInt; % an illegal declaration %</lang>
The following could be used instead:
<lang algolw>record INT_VALUE ( integer val );
reference(INT_VALUE) refInt;</lang>
 
Fields are refered to via a function-like notation, e.g.:
<lang algolw>% using the person record defined above...%
reference (person) someone;
someone := person % create a new person structure with uninitialised fields %
name(someone) := "Fred"; % initialise the fields %
age(someone) := 27;
% could also initialise the fields when the record is created: %
someone := person( "Harry", 32 );</lang>
 
Arrays of the basic types and references can also be declared, but records cannot contain arrays.
There are no procedure variables though procedures can be passed as parameters to other procedures, as can arrays, references and the basic types.
Procedures can return basic types and references.
 
=={{header|AppleScript}}==
3,060

edits