YOUSRC logo    YouTube logo  Twitter logo 
Login/Email: 
Password: 
or Register here | Cookie policy
  Rachel's Rabbit for younger codersELC for older coders
Step by step learning guide

Tutorial topics

Tutorial 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

Defining variables

Variables allow a computer to remember things for you while an app is running. Think of them like this shoe box - a place you can put something, store it, and get it back later...



We can give each of these variables a name, a bit like writing a label on the front. The label on the shoe box above shows it is called VarName.

There are two types of variables in ELC: number variables; and word variables. Number variables are used for storing numbers, which can be either negative or positive. Word variables are used to store anything from one letter up to sentances as long as you like.

Variable names can be anything you like provided they don't include one of the characters that has another meaning (such as '+' (add), '-' (subtrcat), '*' (multiply), '/' (divide), '%' (modulus), '(', ')', '{' or '}', '=' (equals), '>' (greater than), '<' (less than), '>=' (greater than or equals), '<=' (less than or equals), '!=' (not equals), ',' or space). They also can't be one of the special ELC keywords (GLOBAL, LOCAL, FUNCTION, IF, ELSE, ELSEIF, WHILE, FOR, TO, STEP). Finally, they can't be the name of a function.

The way we show if a variable is to store numbers or words is whether it has a '$' character on the end. Word variables have this, number variables do not.

Variables can either be accessed anywhere in an app (global variables), or only in one function (local variables) depending on how they are set-up (in programming we call this declaring variables). If you like, to get you started, you can just use global variables but as your programming skill grows you may think "I just need this variable in this function so I'll declare it as a local variable"). Using local variables prevents your code getting clogged up with lots of global variables that are only really used in one or two places.

Global variables are declared using the GLOBAL keyword before the START function.

Local variables are declared using the LOCAL keyword inside the function where they are to be used.

Good practice means if you need to use variables often across your code then define them as global variables so you don't need to keep passing them as parameters. But if you have variables that are only needed within a function then define them as local variables within that function as this makes your code easier to understand.

Here's an example of some global variables and some local variables. The code doesn't actually use them - we'll come onto that later in the tutorial...

// ===================================================
// This ELC app defines some global variables and has
// some functions that define some local variables.
// They are not actually used in this example, that
// will come in the next section

GLOBAL GlobalNum1,GlobalNum2
GLOBAL GlobalWords1$,GlobalWords2$

FUNCTION START()
{
  LOCAL LocalNum
  LOCAL LocalWords$
  
  GFX.WRITELINE("Hello world")
}


// ===================================================
// Simple empty function

FUNCTION DoSomeMaths(Num)
{
  LOCAL TempNum
}


// ===================================================
// Another simple empty function

FUNCTION MixSomeWords(Words$)
{
  LOCAL TempWords$
}



Next, function parameters...





android, apps, toys, clipart, sound clips, games, wii, nintendo, ds, microsoft, apple, music, playstation, xbox, child, children, teen, music, school
Copyright (C) Previca Limited 2012
Note: This site is optimised for 1024x768 screen resolution.
Although many PC's and monitors support higher resolutions, many users of this site are schools who may not have the latest equipment.
If you are lucky enough to have higher resolution equipment then we are sorry you may see spare white screen space - we hope you understand.