Hosting and domain costs until October 2024 have been generously sponsored by dumptruck_ds. Thank you!

QuakeC Definition of Functions

From Quake Wiki

Revision as of 18:33, 14 June 2013 by 86.183.25.94 (talk)

Definition of Functions

Ordinary functions

The general structure of a function definition is:

   type (type param1, type param2, ... ) function =
   {
      ... code ...
      return expression;
   };

Notes:

   * Do not forget the ; after the ending brackets.
   * The return statement can be used more than once, anywhere in the function.
   * The expresion in a return statement must of course be of the same type as the return value of the fuction.
   * If the return type is void, then you can omit the return statement. 

Here are some examples:

   void ()	think = {...};
   entity ()	FindTarget = {...};
   void(vector destination, float speed, void() callback)  SUB_CalcMove = {...};


Function calls

Here is how a function is called in Quake-C:

   function_name ( parameter1, parameter2,... )

There cannot be more than 8 parameters.

Function declaration

If you want to use a function before defining it, you must declare it, otherwise the Quake-C compiler will not be able to use it.

The general structure of a function declaration is:

   type (type param1, typeparam2, ... ) function;


Definition of a frame function

Frame functions (also called States) are special functions that are meant to facilitate the definition of models animation frames.

Here is an example:

   void() framename = [$framenum, nextthink] { ...code...};

It is strictly equivalent to:

   void() framename =
   {
      self.frame= $framenum;  // the model frame to displayed
      self.nextthink = time + 0.1;   // next frame happens in 1/10 of second
      self.think = nextthink; // the function to call at the next frame
      ...code...
   };

Extended features

For consistancy with C code, many modern qcc support this alternative syntax for prototypes:

   type function(type param1, type param2);

Or for functions that accept no arguments:

   type function(void);

For bodies, the same ordering is permitted.

The = has become optional regardless of whether the arguments are defined before or after the name of the function.

The semi-colon on the end of the body is also now optional (semi-colon is still mandatory in prototypes).

A function prototype prefixed with the 'var' keyword may be assigned to (any occurences of the function will then refer to the new function instead, identical to .void() usage), and is not required to have a function body defined (but may still do so).