Functions are central in haXe ( or any other language for that matter). A function is basically a number code lines, grouped together so that it can be executed multiple times. All code that needs to be executed by the computer resides in functions.

An Example of a Function


Let us look at how a function is defined -

class Test
{
  function Hello()
  {
    trace("Hello");
  }
}

We first define the class Test ( remember classes ?). All code, including our function definition, has to be present in a certain class.
Then we create a function called Hello which prints Hello on the screen. That's about it. We will be explaining each component a little later.
Remember that this program does not do anything, as the function will not execute. This is explained in the next section.

When Functions are Executed


When we define a function, it does not execute immediately. A function is executed only when we call it.
Calling a function is the act of executing a function which has already been defined earlier. This is the syntax (anything in square brackets is optional):
function function-name([argument-list]);
To call a function, all we have to do is specify the name with a set of brackets. Don't worry about that argument-list right now, I will discuss that later. So, let us rewrite the above code so that something is displayed on the screen.

class Test
{
 
  static function Hello()
  {
    trace("Hello");
  }
 
  static function main()
  {
    Hello();
  }
 
}
First, we define the Hello function which prints Hello on the screen. Then we define the main function which calls Hello. Observe how we call the Hello function. We call it using its name followed by an open and a close bracket.
One point to keep in mind is that, the main function which we defined earlier, is not called called inside the code. It is called by the computer. Thus, it is the first piece of code that is executed in your program. The main function has to be preceded by the word static.
Notice, that we have also preceded the Hello function with the word static. This is because, any function called in main ( which is a static function ) has to usually be static itself. This will be explained later in OOP.

The Components of a Function

To execute a function we need to call it. But, before we can call it we need to define the function.To define a function it needs to have some components. Let us look at these components.
[options] function Name([arg1,arg2,arg3....])[:return-type]
{
 
  /*body or
    code lines
    should be here
   */
  [return value;]
 
}

This is the syntax for function definition. Looks complex, doesn't it. Well, I assure you it is not. Allow me to explain it to you.
  • The function definition begins with the word function. This word tells the compiler that you are now defining a function. ( The word function can be preceded by certain options such as static, which we will see later).
  • Then comes the function name. This is the identifier of the function. You need to use this name when calling the function.
  • Then, after the open brackets we define something called arguments. Arguments are values that you pass ( or give ) to the function during the function call. The function can then use these values to do certain operations on them.
  • Apart from accepting values through arguments during a function call, a function can also send a value back to the line that called it. This is achieved using the return keyword. The data type of the returned value is specified by a colon and the return-type.
  • We then write the code to be executed by the function with two square brackets, i.e, within a block ( remember blocks ? ).
  • Lastly we specify the return value of the function. This value should of type return-type, which we specified earlier.

Phew, that was a little too much work for just one small, measly function.

Remember that the things in square brackets are optional.

Function Arguments


Lets get back to that Hello function. It displays Hello on the screen. That's fine, but what if you wanted it to display anything you wanted to, not only Hello ? Do you need to define a new function? NO. We use something called arguments.
Arguments are values we pass to the function so that it can give an output based on the arguments passed. Interesting, lets check this out. I have redefined the Hello function and added an argument.
class Test
{
 
  static function Hello(str:String)
  {
    trace(str);
  }
 
}
Now, we define the function Hello with an argument str. This str which gets its value when the function is called. So, we can give str any String value we desire.
str is defined inside the brackets of the function. It has a data type like any other variable. str has a data type of String.
Now, using this string value, we print to the screen. So, the value of str is displayed on the screen.

Let us now see how we call this function in main().

class Test
{
 
  static function Hello(str:String)
  {
    trace(str);
  }
 
  static function main()
  {
    Hello("World");
  }
 
}

Observe how we call the function in main. We no longer give empty brackets when calling the function. Since, the function requires arguments, we need to specify these arguments during a function call. We pass the string "World" to the function This means that the variable str gets the value "World". So, the result would be that the statement trace(str) in the function Hello would be executed as trace("World'). So, the string World is displayed on the screen.

Note that we also call arguments as parameters.

Functions Returning a Value


A function can return some value, based on the input give to it ( arguments ) and the code in its body.
Suppose we make a function which accepts two integers, and then adds them up. How do we get the result of this addition ? The answer is that we pass the result as a return value. Lets see how
class AddTest
{
 
  static function add(n1:Int,n2:Int):Int
  {
    var sum = n1+n2;
    return sum;
  }
 
  static function main()
  {
    var s:Int = add(3,4);
    trace(s);
  }
 
}
We define a function add with two parameters n1 and n2, both of type integer. Then we add them up and store the value in sum. Now, we return this value. Notice that we have specified the return type of the function as Int. Thus, the value returned also has to be Int.
Now, lets go to the call statement. Look at how we call this function. We pass the values 3 and 4 to n1 and n2. This function adds these two numbers and returns a value of type Int. This value can now be given to another variable of the same type. So, here we assign the variable s with the returned value sum. Thus, s gets the value (3+4=) 7. This value is then printed on the screen.

Remember, that we do not have to specify a return type for the function. The haXe compiler can infer the type from the value that is returned ( it is the same as type inference for variables ). So, the function add can also be -
static function add(n1:Int,n2:Int)
{
  var sum = n1+n2;
  return sum;
}
Here we do not explicitly say that the return type should be Int. But, the haXe compiler can figure it out by looking at the data type of the variable returned, i.e., the sum variable.

The Essence of a Function


The best way to remember how a function works is to compare it with a machine. At the most basic level, a machine takes some input and gives us an output based on the input given ( eg, take a calculator, it accepts numbers as input and gives an output). This is exactly how a function acts.
When we call a function we may pass arguments to it. This acts as the input which it receives and processes. Then it gives us an output based on the input by returning a value.