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 Array, 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 highlighted lines:

    procedure inPlaceRippleSort(arr as Array<of Int>)
    variable changes set to true
    variable lastComp set to arr.length() - 2
    repeat
    set changes to false
    for i from 0 to lastComp step 1
    if arr [i] > arr [i + 1] then
    variable temp set to arr [i]
    set arr[i] to arr[i + 1]
    set arr[i + 1] to temp
    set changes to true
    end if
    end for
    set lastComp to lastComp - 1
    end repeat when not changes
    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:


    let probability be random()
    print probability

    for i from 1 to 10 step 1
      print randomInt(1, 6)
    end for

    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: See 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.