Function

The information on this page is very outdated and often misleading. The content of this page most likely will radically change in the future.

A function is something defined in code which carries out an instruction or set of instructions and may or may not have inputs and outputs. The builtin functions which are provided by the various games themselves are referred to as commands, and are called in a different manor to functions which are defined within scripts. Functions were first introduced in the OFP: Resistance patch.

Functions can also be used to reuse code. You can write some code once in the function and then include it in many different scripts. When the code is updated, it is updated for all scripts. When you only copy and paste the code to the other scripts, you have to update every script on any change.

Types of function

Functions-as-files

Functions as files are functions stored within a file. These are usually used for larger and more complex functions. The code is evaluated in the same way, however, there are additional commands which must include the file before the function itself can be called.

//MyFile.sqf
// Code here..

Inline functions

Inline functions are functions are technically code which is often stored within a variable or declared as a function parameter. Inline functions operate the same way as functions-as-files as both are evaluated in the same way, but the difference is that inline functions are stored within parentheses{}, whereas functions-as-files do not require these.

Anatomy of a function

When scripting, there are two types of functions: functions-as-files and inline functions. Functions-as-files are instances where the a whole file itself is used to house a function, whereas inline functions are either contained within a variable or as a parameter of a function. Some built-in functions require functions-as-files, whereas most will support both.

Parameters

Parameters for functions are available to the function via the magic variable_this. Declaration of parameters can be done using the params command. Common practice for defining parameters is done via the use of private variables and defined variables.

In the first example "_this + 1" is the last executed statement in my_fnc, in the second example it is "_this - 1". Traditionally the returning statement is written without ";" after it. Have it or don't have it, it is up to you, doesn't make a blind bit of difference:

Functions executed using call are run within the executing instance, which waits for the result of the function. Unlike scripts, functions halt all other game engine processes until the function has completed its instructions. This means functions run faster than scripts, and the result of functions is immediate and unambiguous. It can also mean that if a function takes too long to run it will have an adverse effect on game play - large functions or CPU intensive functions can cause the game to seize up until it completes. When creating a functions you want the function to be short and sweet to achieve the best results.

Note: You can still use the special variables and commands of scripts in functions (Armed Assault only)!

Spawn

Functions may also be executed using spawn, but then the function result is not accessible, making it behave more like a procedure. Spawned functions will run asynchronously or alongside the executing instance. This helps prevent large CPU intensive functions from seizing up the game.