Procedural programming
Defining named values
There are several kinds of name value in Elan, defined by:
- A constant (which might be a user-defined constant or a library constant)
- A let statement (see below)
- A variable statement
- A parameter in a function or a procedure: see Functions and procedures
Variable statement
The variable statement is used to define, and initialise, a new variable. Unlike a named value defined by a let statement, a variable may be re-assigned from its initial value to a new value.
The name given to the variable must follow the rules for an Identifier.
The value to which the new variable is initialised may be a literal value, or a more complex expression. Either way, the resulting value defines the Type for that variable.
Set statement
The set statement is used to assign a new value to an existing variable. The new value must be of the same Type (or a Type compatible with) that of the variable.
A set statement may not assign a new value to a parameter: see Parameter passing.
Parameter passing
The arguments provided to a method (function or procedure) are passed ‘by value’ and not ‘by reference’. If you wish to be able to re-assign the value associated with a parameter, such that that change would be visible to the code that calls the procedure, then you can precede the parameter definition with the keyword out. This is useful when you are passing in, say, an Int that refers to an index, and you want the procedure to update the index number it is pointing to.
Note, however, that mutating an instance of a reference Type held in a variable is not the same thing as re-assigning the variable to a different instance. The first changes the contents of the thing, the second changes the thing for another thing!
Therefore, if
- the method is a procedure AND
- the Type of the argument is a ‘reference Type’ AND
- that Type is mutable such as an List, Dictionary, Stack, Queue, or a user-defined class
then it is possible to mutate the parameter that holds that argument within the procedure, such that any reference to the argument outside the procedure will ‘see’ the changes.
A good example of this is an ‘in-place sort’ procedure. In the following code the arr parameter is mutated in the two call arr.PutAt() statements:
+procedure inPlaceRippleSortname(out arr as List<of Int>parameter definitions)
variable changesname set to trueexpression
variable lastCompname set to arr.length() - 2expression
+repeat
set changesvariableName to falseexpression
+for ivariableName from 0expression to lastCompexpression step 1expression
+if arr[i] > arr[i + 1]condition then
let tempname be arr[i]expression
call arr.putAtprocedureName(i, arr[i + 1]arguments)
call arr.putAtprocedureName(i + 1, temparguments)
set changesvariableName to trueexpression
end if
end for
set lastCompvariableName to lastComp - 1expression
end repeat when not changescondition
end procedure
Note however that:
- In a function you may not mutate any parameter
- In a procedure you may not re-assign any parameter
Generating random numbers
Random numbers may be created by calling one of these two standard methods:
- random() returnss a Float in the range 0 … 1
- randomInt(min, max) returnss an Int in the range min to max inclusive
For example:
+main
let probabilityname be random()expression
print probabilityexpression
+for ivariableName from 1expression to 10expression step 1expression
print randomInt(1, 6)expression
end for
end main
Notes
Comments
Explanatory YouTube video: Elan language constructs 4 - comments
Comments:
- may be added at global level, as well as within constructs.
- always start with a # followed by a space and then free-form text. The text field may be left empty
- are a single line, though if the text is long enough the line may be wrapped within the editor
- are always on their own line. It is it not possible to add a comment after, or within, a line of code.