HaxeDoc2

(やまだぃちぅ) #1

5.9 Function Call


Functions calls consist of an arbitrary subject expression followed by an opening parenthesis(,
a comma,separated list of expressions as arguments and a closing parenthesis).

1 subject(); // call with no arguments
2 subject(e1); // call with one argument
3 subject(e1, e2); // call with twoarguments
4 // call with multiple arguments
5 subject(e1, ..., eN);


5.10 var


Thevarkeyword allows declaring multiple variables, separated by comma,. Each variable
has a valid identifier ( 5 ) and optionally a value assignment following the assignment operator=.
Variables can also have an explicit type-hint.

1 var a; // declare local a
2 var b:Int; // declare variable bof type Int
3 // declare variable c, initializedto value 1
4 var c = 1;
5 // declare variable d and variablee
6 // initialized to value 2
7 var d,e = 2;


The scoping behavior of local variables is described inBlocks(Section 5.1).

5.11 Local functions


Haxe supports first-class functions and allows declaring local functions in expressions. The syn-
tax follows class field methods (4.3):

1 class Main {
2 static public function main() {
3 var value = 1;
4 function myLocalFunction(i){
5 return value + i;
6 }
7 trace(myLocalFunction(2)); // 3
8 }
9 }


We declaremyLocalFunctioninside the block expression (5.1) of themainclass field. It
takes one argumentiand adds it tovalue, which is defined in the outside scope.
The scoping is equivalent to that of variables (5.10) and for the most part writing a named
local function can be considered equal to assigning an unnamed local function to a local variable:

1 var myLocalFunction = function(a){}


However, there are some differences related to type parameters and the position of the func-
tion. We speak of a “lvalue” function if it is not assigned to anything upon its declaration, and
an “rvalue” function otherwise.


  • Lvalue functions require a name and can have type parameters (3.2).

Free download pdf