# 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 inPlaceRippleSort name(out arr as List<of Int>parameter definitions) variable changes name set to true expression variable lastComp name set to arr.length() - 2 expression +repeat set changes variableName to false expression +for i variableName from 0 expression to lastComp expression step 1 expression +if arr[i] > arr[i + 1]condition then let temp name be arr[i]expression call arr.putAt procedureName(i, arr[i + 1]arguments) call arr.putAt procedureName(i + 1, temp arguments) set changes variableName to true expression end if end for set lastComp variableName to lastComp - 1 expression end repeat when not changes condition 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 probability name be random()expression print probability expression +for i variableName from 1 expression to 10 expression step 1 expression print randomInt(1, 6)expression end for end main

Notes

- These two methods are both System methods, so they may be used only within main or a procedure. A resulting Int or Float may then be used as an argument to pass into a function.
- Elan provides a separate mechanism for generating random numbers within a function.

## 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.