Tutorial topicsTutorial home
ELC language reference
Commenting your code
ELC app structure
An empty ELC program
Hello world
Functions
Defining variables
Function parameters
Using variables
Function return values
IF decisions
WHILE loops
FOR loops
Arrays of variables
Library functions
Game Tech - Depth & 3D
Game Tech - Shooting bullets
Dev Tech - How to debug
|
Using variables
The value of variables is set using the LET instruction.
You can use LET to set a simple value as follows: LET variable = value
eg: LET NumVar = 3
eg: LET WordVar$ = "Hello"
Alternatively, for number variables, you can use the '+' (add), '-' (subtract), '*' (multiply), '/' (divide) and '%' (modulus)
operators to change variables.
You can have as many operators as you like on one line and they work, one by one, left to right, just as if you were typing
the sequence into a calculator.
eg: LET NumVar1 = 2 * 3 + 4
This would set NumVar1 to be 10 (2 times three is 6, add four is 10)
You can change word variables only by combining them together using the '+' operator.
eg: LET CombinedWords$ = Words1$ + Words2$
Here's an example with a few operators included to make it more intersting...
// ===================================================
// This ELC app defines some global variables and has
// some functions that define some local variables.
// They are just used to do some silly things to give
// you an idea
GLOBAL GlobalNum1,GlobalNum2
GLOBAL GlobalWords1$,GlobalWords2$
FUNCTION START()
{
LOCAL LocalNum
LOCAL LocalWords$
// Set up global variables with some value to start with
LET GlobalNum1 = 5
LET GlobalNum2 = 10
LET GlobalWords1$ = "The cat sat on the mat."
LET GlobalWords2$ = "The mouse sat in the house."
// Set up local variables with some value to start with too
LET LocalNum = 20
LET LocalWords$ = "The pig sat in some mud."
GFX.WRITELINE("Hello world")
DoSomeMaths(LocalNum)
GFX.WRITELINE(RETVAL)
MixSomeWords(LocalWords$)
GFX.WRITELINE(RETVAL$)
}
// ===================================================
// Simple number function
FUNCTION DoSomeMaths(Num)
{
LOCAL TempNum
LET TempNum = 100 - Num
LET RETVAL = TempNum + GlobalNum1
LET RETVAL = RETVAL * GlobalNum2
// Could have written LET RETVAL = TempNum + GlobalNum1 * GlobalNum2
}
// ===================================================
// Simple word function
FUNCTION MixSomeWords(Words$)
{
LOCAL TempWord$
LET TempWord$ = ". "
LET RETVAL$ = Words$ + TempWord$
LET RETVAL$ = RETVAL$ + GlobalWords1$
LET RETVAL$ = RETVAL$ + GlobalWords2$
}
|
|
You can find this app in the Examples area as an app called
"14 - Using variables".
Next, returning values from functions...

|