HotkeyNet: the next generation
REFERENCE
Functions

A function definition looks like this:

function MyFunction ()
{
    print ( "hello world" );
}


If the function contains only one statement, it can also be written like this:

function MyFunction ()
    Print ( "hello world" );


Return type

The function's type is determined by what it returns. For example, the following function returns a string.

function MyFunction ()
    return "done";


Parameters

You can specify a parameter with just a name. In this example, "saywhat" is a name that you invented:

function MyFunction ( saywhat )
{
    Print ( saywhat );
}


Usually that's all you need to do. But if you want, you can also give default values:

function MyFunction ( saywhat = "hello world" )
{
    Print ( saywhat );
}


A default value can be an expression including function calls and global variables:

function MyFunction ( parm = Func2 ( gx + 37 ) )
{
    return parm * 10;
}


Overloading

You can write more than one function with the same name if they take different numbers of arguments. This is called function overloading.

function f ( parm1 )
   ... ;

function f ( parm1, parm2 )
   ... ;

function f ( parm1, parm2, parm3 )
   ... ;


You can also overload by specifying different argument types.

function f ( int parm )
   ... ;

function f ( bool parm )
   ... ;

function f ( string parm )
   ... ;


If you overload with different types, the program makes several attempts to match your function call to one of the definitions. First it tries to find an exact match. If that's impossible, it attempts to create a match by converting your arguments to different types. If you want to prevent a match based on type conversion, put a plus sign in front of the type names in the function declaration:

function f ( +int parm )
   ... ;


Call by value and call by reference

By default, arguments pass by value. If you want to pass by reference, put an ampersand before the parameter name:

function f ( int & parm )
   ... ;


You can create two functions that differ only by an ampersand.

function f ( int parm )
   ... ;

function f ( int & parm )
   ... ;


Declarations and prototypes

There's no need (and no way) to declare functions before you use them. As long as they are defined somewhere in your script, the program will find them.

Putting it all together

Everything described above can be combined. Here's an example:

function f ( +string &name, +bool &price = x && y, int &discount = 100 )
   ... ;


This page was last revised on June 14, 2009
Copyright © 2009 HotkeyNet Software