You will have seen in the previous examples that the code is surrounded by two block statements block and _block. Blocks allow the code to be split up in chunks. You could write all the code in one block if required but this will produce code that is difficult to maintain and debug, splitting the code into blocks also means that you can reuse blocks of code.
There must always be one block called Go this is the entry point into the program so all evolve programs need to have this block.
A block starts with the keyword block followed by the block name and ends with the keyword _block.
//---------------------------------------
// File: SimpleBlock.e
//---------------------------------------
//---------------------------------------
// Block: Printhellobye
//---------------------------------------
block Printhellobye
outl "Hello"
printgoodbye
_block
//---------------------------------------
// Block: Printgoodbye
//---------------------------------------
block Printgoodbye
outl "Good Bye"
_block
//---------------------------------------
// Block: go
//---------------------------------------
block Go
printhellobye
_block
Variables can be passed into a block by adding variable aliases into the block header.
These alias variables can then used within the block. If you change a value of an aliased variable then the variables value in the calling block will be changed.
Since the variables passed to a block are aliases of the passed variable they will take on the variables type, this means that generic blocks can be written that will work with any variable types. This is similar to using templates in c++.
The following example will display
0
Test
true
on the screen showing that the variables are passed as an alias.
//---------------------------------------
// File: BlockVariables.e
//---------------------------------------
//---------------------------------------
// Block: Test1
//---------------------------------------
// Variables:
// Variable
//---------------------------------------
block Test1 Variable
outl Variable
_block
//---------------------------------------
// Block: Go
//---------------------------------------
block Go
BoolVar = true
IntVar = 0
StrVar = "Test"
test1 IntVar
test1 StrVar
test1 BoolVar
_block
Since passing variables works on creating an alias to the original variable you can't pass an element of a list. If you want to pass an element then you need to make a copy and pass the copy.
//---------------------------------------
// Block: PassElement
//---------------------------------------
// Variables:
// Element
//---------------------------------------
block PassElement Element
outl Element
_block
//---------------------------------------
// Block: go
//---------------------------------------
block Go
DataList = { 1 2 3 4 5 }
Data = Data[1]
PassElement Data
_block
There is no concept of a block returning a value as in other languages but since all variables are passed as aliases this is not a problem.
//---------------------------------------
// File: BlockReturnVariables.e
//---------------------------------------
//---------------------------------------
// Block: Test
//---------------------------------------
// Variables:
// Variable1
// Variable2
//---------------------------------------
block test Variable1 Variable2
Variable1 = 10
Variable2 = "New"
_block
//---------------------------------------
// Block: go
//---------------------------------------
block Go
IntVar = 0
StrVar = "Test"
test IntVar StrVar
outl IntVar
outl StrVar
_block