constants are also variables that are assigned an initial value that can never change in the program’s life. euphoria allows to define constants using constant keyword as follows −
constant max = 100 constant upper = max - 10, lower = 5 constant name_list = {"fred", "george", "larry"}
the result of any expression can be assigned to a constant, even one involving calls to previously defined functions, but once the assignment is made, the value of the constant variable is "locked in".
constants may not be declared inside a subroutine. the scope of a constant that does not have a scope modifier, starts at the declaration and ends and the end of the file it is declared in.
examples
#!/home/euphoria-4.0b2/bin/eui constant max = 100 constant upper = max - 10, lower = 5 printf(1, "value of max %d\n", max ) printf(1, "value of upper %d\n", upper ) printf(1, "value of lower %d\n", lower ) max = max + 1 printf(1, "value of max %d\n", max )
this produces the following error −
./test.ex:10 <0110>:: may not change the value of a constant max = max + 1 ^ press enter
if you delete last two lines from the example, then it produces the following result −
value of max 100 value of upper 90 value of lower 5
the enums
an enumerated value is a special type of constant where the first value defaults to the number 1 and each item after that is incremented by 1. enums can only take numeric values.
enums may not be declared inside a subroutine. the scope of an enum that does not have a scope modifier, starts at the declaration and ends and the end of the file it is declared in.
examples
#!/home/euphoria-4.0b2/bin/eui enum one, two, three, four printf(1, "value of one %d\n", one ) printf(1, "value of two %d\n", two ) printf(1, "value of three %d\n", three ) printf(1, "value of four %d\n", four )
this will produce following result −
value of one 1 value of two 2 value of three 3 value of four 4
you can change the value of any one item by assigning it a numeric value. subsequent values are always the previous value plus one, unless they too are assigned a default value.
#!/home/euphoria-4.0b2/bin/eui enum one, two, three, abc=10, xyz printf(1, "value of one %d\n", one ) printf(1, "value of two %d\n", two ) printf(1, "value of three %d\n", three ) printf(1, "value of abc %d\n", abc ) printf(1, "value of xyz %d\n", xyz )
this produce the following result −
value of one 1 value of two 2 value of three 3 value of abc 10 value of xyz 11
sequences use integer indices, but with enum you may write code like this −
enum x, y sequence point = { 0,0 } point[x] = 3 point[y] = 4