Function Declarations

Functions can be declared (or defined) which can then be invoked (called) from anywhere within the script you defined it in. This has many important uses:

/* prototype
type functionName([type parameter1,] [...,] [type parameterN])
{
     // code block ...
     return value;
}
*/
string CombineStrings(string str1, string str2) 
{
     return str1 + str2;
}

Furthermore, you can provide default values for some parameters, namely those of types float, int, string, and object (only OBJECT_INVALID and OBJECT_SELF work for object). A default value means that, when the function is called, if no value is provided for that parameter, the function will use the default one. These optional parameters must be listed AFTER the non-optional ones. Also, variables can't be used as default values, only true integers, floats, strings, and constants.


//This works fine:

void MyFunction(float fSomething, int nSomething = 0, object oSomething = OBJECT_SELF)
{

}

//This doesn't work:

int nDefault = 0;

void MyFunction(float fSomething, int nSomething = nDefault, object oSomething = OBJECT_SELF)
{

}

//But this does:

const int nDefault = 0;

void MyFunction(float fSomething, int nSomething = nDefault, object oSomething = OBJECT_SELF)
{

}




 author: Ryan Hunt, editor: Lilac Soul, additional contributor(s): Lilac Soul