AviSynth Syntax - User defined script functions

Definition and Structure

You can define and call your own functions in AviSynth scripts as shown below. The function can return any clip or variable type. An user defined script function is an independent block of script code that is executed each time a call to the function is made in the script. An example of a simple user defined script function (here a custom filter) immediately follows:

function MuteRange(clip c, int fstart, int fend)
{
    before = c.Trim(0, -fstart)
    current = c.Trim(fstart, fend) 
    after = c.Trim(fend + 1, 0)
    audio = Dissolve(Dissolve(before, current.BlankClip, 3), after, 3)
    return AudioDub(c, audio)
}

User defined script functions start with the keyword function followed by the function name. The name of a script function follows the same naming rules as script variables.

Immediately after the name, the function's argument list follows. The list (which can be empty) consists of (expected argument's type - argument's name) pairs. Each argument's type may be any of those supported by the scripting language.

function MuteRange(clip c, int fstart, int fend)

Then comes the function body, ie the code that is executed each time the function is called. The arguments are accessed within the function body by their names. The function body is contained within an opening and closing brace pair { ... }.

{
    before = c.Trim(0, -fstart)
    current = c.Trim(fstart, fend) 
    after = c.Trim(fend + 1, 0)
    audio = Dissolve(Dissolve(before, current.BlankClip, 3), after, 3)
    return AudioDub(c, audio)
}

At the end of the function body a return statement which returns the final value calculated from the arguments and the function's body code is placed.

    return AudioDub(c, audio)

It should be noted that unlike other languages where multiple return statements are allowed inside the function body, in AviSynth functions contain a single return statement. This is because the language does not support branching (i.e. compound block statements).

Facts about user defined script functions

Related Links

$Date: 2008/12/21 09:23:02 $