The function block is
Because functions take values and return a value, some extra stuff is required in the first part of the function block. The function block layout reads: function (name) takes (something) returns (something)
The (name) portion is the label of the function, which is placed after the function keyword. The label is what is used when calling the function.
JASS:
function Label
endfunction
The
takes
keyword shows what values must be plugged into a function when running it. Takes uses a comma separated list of declared variables (these variables cannot be initialized since their values are plugged into the function):
takes real x, integer y, boolean b
JASS:
function Label takes real x, integer y, boolean b
endfunction
Each variable declared in takes is associated to the value that is plugged into the function when it is run. For example, the first value put into a function would be the first variable declared in takes.
Whenever the Label function is run, it creates whatever variables are declared in takes for itself. When it ends, it destroys those variables. In this case, 3 variables would be declared every time Label is run
JASS:
Label
real x
integer y
boolean b
Those variables would be initialized to whatever values are passed into Label when it is run (order matters). Whenever calling Label, 3 values would have to be passed in, the first one being a real, the second one being an integer, and the third one being a boolean (values**). This means that if 3.3, 5, and true were passed into the Label function, it'd be akin to
JASS:
Label
real x = 3.3
integer y = 5
boolean b = true
Functions are run using the call keyword.
call MyFunction
Values are passed in by grouping them into
()
with a comma separated list. Even if a function doesn't take anything, the () must be used.
call MyFunction()
To run the Label function from before (remember Label tales a real, integer, and boolean):
call Label(3.3, 5, true)
Three values must always be passed in because Label takes 3 arguments. This wouldn't be legal
call Label(3.3, 5)
If Label didn't take anything
call Label()
Calls can only be done within functions.
The
returns
keyword shows the
type of variable a function returns:
returns real
JASS:
function Label takes real x, integer y, boolean b returns real
endfunction
The
nothing
keyword is used whenever a function takes nothing or returns nothing.
JASS:
function Label takes nothing returns nothing
endfunction
When a function is called, that function
will always return a value, even if that value is nothing. No variable type can be set to nothing, so setting a variable to a function that returns nothing isn't possible.
Variables can be set to functions as if the functions were values (because a function always returns a value). Functions can also be plugged into functions as if they were values. Keep in mind that the type a function returns is the type of value a function is treated as.
When a function is used as a value, the call keyword is not used.
JASS:
real x
real y
set x = Label(5, 6, true) //the above example of Label is used
set y = Label(Label(7, 8, true), 16, true) //Label is passed into Label as a parameter.
//this wouldn't be legal as Label returns a real
//set y = Label(6.6, Label(7, 8, true), true)
//Remember the function is: function Label takes real x, integer y, boolean b returns real
//This means that the y in the function is being set to Label(7, 8, true).
//The y var in the function is an integer, and integer variables cannot store real values.
Function blocks cannot go into function blocks or global blocks (global blocks only store global variables) (functions only store lines of code)
Code can be written normally in a function block and the parameters can be used as regular variables within the function. A function can only call a function that is declared above it.
JASS:
function Label takes integer x returns nothing
endfunction
function Label2 takes integer y returns nothing
call Label(y)
endfunction
function Label3 takes nothing returns nothing
call Label2(3)
call Label4() //syntax error, Label4 is declared after Label3
endfunction
function Label4 takes nothing returns nothing
endfunction
The
return
keyword is used to return values. Return will immediately exit out of a function.
Using the Add example from before:
JASS:
//this function can only return values of type integer
function Add takes integer number1, number2 returns integer
return number1+number2
endfunction
//this function can only return values of type integer
function Add5And7 takes nothing returns integer
return Add(5, 7) //returns 5+7, which is 12
endfunction
If a function returns something, it must always return that something at the last line.
JASS:
function Add takes integer x, integer y returns integer
//syntax error because this function has to return an integer value
endfunction
function Add2 takes nothing returns notihng
call Add(5, 7)
endfunction
JASS:
function Add takes integer x, integer y returns integer
return 5.5 //syntax error because this function has to return an integer value. 5.5 is a real.
endfunction
function Add2 takes nothing returns notihng
call Add(5, 7)
endfunction
JASS:
function Bleh takes nothing returns nothing
endfunction
//Bleh is never called because there is a return above it. This will still cause a syntax error even though the function always returns a value. The last line of code for a function must always be a return
function Add takes integer x, integer y returns integer
return x+y
call Bleh() //line is never reached because of the return above
endfunction
function Add2 takes nothing returns notihng
call Add(5, 7)
endfunction
JASS:
function Bleh takes nothing returns nothing
endfunction
//this example does the exact same thing as the above, but it works.
function Add takes integer x, integer y returns integer
return x+y
call Bleh() //line is never reached because of the return above
return 0
endfunction
function Add2 takes nothing returns notihng
call Add(5, 7)
endfunction
A function that returns nothing may still have a return statement in it, but it doesn't need one. Returning nothing is just the return keyword by itself.
JASS:
function Bleh takes nothing returns nothing
return
endfunction
//this works too
function Bleh2 takes nothing returns nothing
return
call Bleh() //line never reached
endfunction
A function that takes only a single argument does not need a comma separated list.
JASS:
function Bleh takes integer x returns nothing
endfunction