- Joined
- Dec 14, 2005
- Messages
- 10,532
Variables are one of the key concepts of procedural programming (the programming paradigm which JASS belongs to) and are key to solving most problems in it. A variable can be thought of as a container that holds a certain kind of information--what kind of information is up to you. In this lesson we will be covering "local" variables--that is, variables which are part of a function. From now on in this lesson, local variables will be referred to as simply "variables".
Variable Declaration
Every function may contain a number of variables defined at its top--directly after its declaration. A variable is declared by:
local type name
Where type is its data type and name is the name you choose to refer to it by. For example, suppose we want to create a "string" variable and then display it.
However, there is one major problem with this: the string has no value! A value can be assigned to a variable in two ways, which will be covered in the next two sections of the lesson.
Initializing a Variable
Variables may be initialized by putting an = directly after their name and then a value of the correct type. Taking the previous example, suppose we want to display "hello" to the user:
If a variable is not initialized, it will not have any value and attempting to read it (get its value) will cause your code to stop executing.
Setting a Variable
Variables may change their value after assignment via set followed by their name, an equals sign, and a value of the correct type. This value may be self-referential (it may include the variable itself). For example, the following code increases the variable "number" by two, totalling four:
Variable Declaration
Every function may contain a number of variables defined at its top--directly after its declaration. A variable is declared by:
local type name
Where type is its data type and name is the name you choose to refer to it by. For example, suppose we want to create a "string" variable and then display it.
JASS:
function DisplayStringWithVariable takes nothing returns nothing
local string myString
call BJDebugMsg(myString)
endfunction
However, there is one major problem with this: the string has no value! A value can be assigned to a variable in two ways, which will be covered in the next two sections of the lesson.
Initializing a Variable
Variables may be initialized by putting an = directly after their name and then a value of the correct type. Taking the previous example, suppose we want to display "hello" to the user:
JASS:
function DisplayHelloWithVariable takes nothing returns nothing
local string hello = "hello"
call BJDebugMsg(hello)
endfunction
If a variable is not initialized, it will not have any value and attempting to read it (get its value) will cause your code to stop executing.
Setting a Variable
Variables may change their value after assignment via set followed by their name, an equals sign, and a value of the correct type. This value may be self-referential (it may include the variable itself). For example, the following code increases the variable "number" by two, totalling four:
JASS:
function IncreaseNumberByTwo takes nothing returns nothing
local integer number = 2
set number = number + 2
endfunction