Functions

From ZDoom Wiki
Jump to navigation Jump to search
Note: This page covers the creation of functions in ACS. For ZScript see ZScript functions.


Functions are — like scripts — an arbitrary number of sequenced commands. Unlike scripts, functions are identified by a name and can be easily called from anywhere in your scripts or other functions. They can also return a value to the calling script or function. You can place functions anywhere in your script file.
Note: you cannot use latent functions (such as Delay) in your functions.


Functions have to be defined like this:

function type function_name([type arg1 [, type arg2 [...]]])
{
	// function body

	return value;
}
  • type — variable type the function will return. Use void to not return anything
  • function_name — the name of the function. It is used to execute the function. Note that function names may not contain blanks
  • type argx — type is the variable type of the argument. argx is the name of the variable as it is used in the function. Like scripts, if you are not using arguments you must specify void.
  • return — optional. The function will return this value


Examples

#include "zcommon.acs"

script 1 OPEN
{
	int ret;
	
	ret = square(3);
	
	Print(s:"the square of 3 is ", d:ret);
}

function int square(int val)
{
	return val * val;
}


Defining a function with no arguments and no return value:
function void my_useful_function(void)
{
     //     Do Stuff:
}


Defining a function with a return value and no arguments:
function int my_useful_function(void)
{
     //     Do Stuff:
     return 1 ;
}


See also