DefineFunction
[] → [function]

Notes

The length in the action header ignores the function body.

Adobe documentation

ActionDefineFunction

Note: ActionDefineFunction is rarely used as of SWF 7 and later; it was superseded by ActionDefineFunction2.

ActionDefineFunction defines a function with a given name and body size.

Field Type Comment
ActionDefineFunction ACTIONRECORDHEADER ActionCode = 0x9B
FunctionName STRING Function name, empty if anonymous
NumParams UI16 # of parameters
param 1 STRING Parameter name 1
param 2 STRING Parameter name 2
...
param N STRING Parameter name N
codeSize UI16 # of bytes of code that follow

ActionDefineFunction parses (in order) FunctionName, NumParams, [param1, param2, ..., param N] and then code size.

ActionDefineFunction does the following:

  1. Parses the name of the function (name) from the action tag.
  2. Skips the parameters in the tag.
  3. Parses the code size from the tag. After the DefineFunction tag, the next codeSize bytes of action data are considered to be the body of the function.
  4. Gets the code for the function.

ActionDefineFunction can be used in the following ways:

Usage 1 Pushes an anonymous function on the stack that does not persist. This function is a function literal that is declared in an expression instead of a statement. An anonymous function can be used to define a function, return its value, and assign it to a variable in one expression, as in the following ActionScript:

area = (function () {return Math.PI * radius *radius;})(5);

Usage 2 Sets a variable with a given FunctionName and a given function definition. This is the more conventional function definition. For example, in ActionScript:

function Circle(radius) {
  this.radius = radius;
  this.area = Math.PI * radius * radius;
}