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 17:24, 22 January 2015 by 192.168.4.107 (talk) (Fix list formatting)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Definition of Functions[edit]

Ordinary functions[edit]

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[edit]

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

   function_name ( parameter1, parameter2,... )

There cannot be more than 8 parameters.

Function declaration[edit]

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[edit]

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[edit]

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).