User defined script functions

From Avisynth wiki
(Difference between revisions)
Jump to: navigation, search
m (1 revision)
(formatting, links, phrasing ***TODO Related Links***)
Line 1: Line 1:
 +
<div style="max-width:62em" >
 
==== Definition and Structure ====
 
==== 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:
+
You can define and call your own (''user defined'') functions in AviSynth scripts. These are independent blocks of script code that are [[wikipedia:Subroutine|executed each time a call to the function is made]] in the script. They can return any [[Script_variables|clip or variable type]]. An example of a simple user defined script function (here a custom ''filter'', ''ie'' a function returning a clip) immediately follows:
  
 
  function MuteRange(clip c, int fstart, int fend)
 
  function MuteRange(clip c, int fstart, int fend)
Line 12: Line 13:
 
  }
 
  }
  
User defined script functions start with the keyword <tt>function</tt> followed by the function name. The name of a script function follows the same naming rules as [[Script_variables|script variables]].  
+
User defined script functions start with the keyword <tt>function</tt>, followed by the function ''name''. The name of a script function follows the same naming rules as [[Script_variables|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 [[Script_variables|type]] may be any of those supported by the scripting language.
+
Immediately after the name, the function's [[Grammar#Functions.2C_Filters_and_Arguments|''argument list'']] follows. The list (which can be empty) consists of (expected argument's type - argument's name) pairs. Argument [[Script_variables|type]] may be any of those supported by the scripting language.  
  
 
  function MuteRange(clip c, int fstart, int fend)
 
  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 <tt>{ ... }</tt>.
+
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 <tt>{ ... }</tt>.
  
 
  {
 
  {
Line 28: Line 29:
 
  }
 
  }
  
(For simplicity, this code assumes the function will only be called with fstart > 0 and fend < c.Framecount-1. A more complete version would also handle the special cases fstart=0 and fend=c.Framecount-1.)
+
For simplicity, this code assumes the function will only be called with fstart>0 and fend<c.Framecount-1. A more complete version would also handle the special cases fstart=0 and fend=c.Framecount-1.
  
At the end of the function body a <tt>return</tt> statement which returns the final value calculated from the arguments and the function's body code is placed.  
+
At the end of the function body, a <tt>return</tt> statement is placed, which sets the return value.  
  
 
     return [[AudioDub]](c, audio)
 
     return [[AudioDub]](c, audio)
Line 38: Line 39:
 
     [[AudioDub]](c, audio)
 
     [[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 (ie compound block statements).
+
It should be noted that unlike other languages, where multiple return statements are allowed inside the function body, AviSynth functions contain a ''single'' return statement. This is because the language does not support loops or branching in the normal sense, although there are ways around this &ndash; see [[Control_structures|Control structures]] for more.
  
 
==== Facts about user defined script functions ====
 
==== Facts about user defined script functions ====
Line 46: Line 47:
 
* Although not recommended practice, an argument type may be omitted, and will default to <tt>val</tt>, the generic type.
 
* Although not recommended practice, an argument type may be omitted, and will default to <tt>val</tt>, the generic type.
  
* If the function expects a video clip as its first argument, and that argument is not supplied, then the clip in the special <tt>last</tt> variable will be used.
+
* If the function expects a video clip as its first argument, and that argument is not supplied, then the clip in the special <tt>Last</tt> variable will be used.
  
 
* Functions support ''named arguments''. Simply enclose an argument's name inside double quotes to make it a named argument. Note that after doing so the following apply:
 
* Functions support ''named arguments''. Simply enclose an argument's name inside double quotes to make it a named argument. Note that after doing so the following apply:
*# All subsequent arguments in the argument list must be made named also.
+
*# All subsequent arguments in the argument list must be named also.
 
*# '''A named argument is an optional argument''', that is, it need not be supplied by the caller.
 
*# '''A named argument is an optional argument''', that is, it need not be supplied by the caller.
 
*# When a function is called, any optional argument which has ''not'' been provided is set to a value which has the void ('undefined') type. This does not mean that its value is random garbage - simply that its type is neither clip, int, float, bool or string and so it has ''no'' usable value.
 
*# When a function is called, any optional argument which has ''not'' been provided is set to a value which has the void ('undefined') type. This does not mean that its value is random garbage - simply that its type is neither clip, int, float, bool or string and so it has ''no'' usable value.
*# Normally, you should use the [[Internal_functions/Boolean_functions|Defined]] function to test if an optional argument has an explicit value, or the [[Internal_functions/Control_functions|Default]] function, which combines the Defined test with the delivery of a default value if appropriate.
+
*# Normally, you should use the [[Internal_functions/Boolean_functions|Defined]] function to test if an optional argument has an explicit value, or the [[Internal_functions/Control_functions|Default]] function, which combines the Defined test with the setting of a default value if appropriate.
 
*# A void ('undefined') value can be passed on to another function as one of its optional arguments. This is useful when you want to write a wrapper function that calls another function, preserving the same defaults.
 
*# A void ('undefined') value can be passed on to another function as one of its optional arguments. This is useful when you want to write a wrapper function that calls another function, preserving the same defaults.
  
Line 59: Line 60:
 
* Functions always produce a new value and never modify an existing one. What that means is that all arguments to a function are passed "by value" and not "by reference"; in order to alter a variable's value in AviSynth script language you must assign to it a new value.
 
* Functions always produce a new value and never modify an existing one. What that means is that all arguments to a function are passed "by value" and not "by reference"; in order to alter a variable's value in AviSynth script language you must assign to it a new value.
  
* Functions can call other functions, ''including themselves''. The latter is known as recursion and is a very useful technique for creating functions that can accomplish complex tasks.
+
* Functions can call other functions, ''including themselves''. The latter is known as ''recursion'' and is a very useful technique for creating functions that can accomplish complex tasks. See [[Control_structures|Control structures]] for more.
  
* Local function variables mask global ones with the same name inside the function body. For example, if you define in a function a local variable <tt>myvar</tt> by assigning to it a value, then you cannot read the global <tt>myvar</tt> anymore inside this function.
+
* Local function variables mask [[Script_variables|global]] ones with the same name inside the function body. For example, if you define in a function a local variable <tt>myvar</tt> by assigning to it a value, then you cannot read the [[Script_variables|global]] <tt>myvar</tt> anymore inside this function.
  
 
* The above is also true for arguments, since from the perspective of a function arguments are initialized local variables.
 
* The above is also true for arguments, since from the perspective of a function arguments are initialized local variables.
  
* Each function has its own local version of the special variable ''last''. On entry to a function, ''last'' is set to a void ('undefined') value.
+
* Each function has its own local version of the special variable ''Last''. On entry to a function, ''Last'' is set to a void ('undefined') value.
  
 
==== Related Links ====
 
==== Related Links ====
  
* [[Shared_functions|Shared functions]]. An ever growing collection of shared script functions created by the members of the AviSynth community.  
+
* [[Shared_functions|Shared functions]] &ndash; an ever growing collection of shared script functions created by the members of the AviSynth community.
:<nowiki>{TEMP: http://www.avisynth.org/ShareFunctions}</nowiki>
+
* [[:Category:Scripts]] &ndash; even more scripts.
* [[Shared_functions/Conditional|Conditional filters and script functions]]. A collection of highly useful conditional filters implemented as user defined script functions.
+
 
: <nowiki>{TEMP: http://www.avisynth.org/ExportingSingleImagehttp://www.avisynth.org/HowToApplyFilterToManySingleFrames :: Perhaps make decent functions from the last two?}</nowiki>
+
[[TODO]]
 +
:<nowiki>http://www.avisynth.org/ExportingSingleImage</nowiki>
 +
:<nowiki>http://www.avisynth.org/HowToApplyFilterToManySingleFrames</nowiki>
 +
:''Perhaps make decent functions from the last two?''
 +
 
 +
</div>
  
 
----
 
----

Revision as of 16:59, 12 March 2016

Definition and Structure

You can define and call your own (user defined) functions in AviSynth scripts. These are independent blocks of script code that are executed each time a call to the function is made in the script. They can return any clip or variable type. An example of a simple user defined script function (here a custom filter, ie a function returning a clip) 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(before, current.BlankClip, 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. Argument 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(before, current.BlankClip, after, 3)
    return AudioDub(c, audio)
}

For simplicity, this code assumes the function will only be called with fstart>0 and fend<c.Framecount-1. A more complete version would also handle the special cases fstart=0 and fend=c.Framecount-1.

At the end of the function body, a return statement is placed, which sets the return value.

    return AudioDub(c, audio)

As a shorthand, a bare expression as the final statement is treated as if the keyword return was present. Thus, we could also have written simply

    AudioDub(c, audio)

It should be noted that unlike other languages, where multiple return statements are allowed inside the function body, AviSynth functions contain a single return statement. This is because the language does not support loops or branching in the normal sense, although there are ways around this – see Control structures for more.

Facts about user defined script functions

  • Functions can take up to sixty arguments and the return value can be of any type supported by the scripting language (clip, int, float, bool, string).
  • Although not recommended practice, an argument type may be omitted, and will default to val, the generic type.
  • If the function expects a video clip as its first argument, and that argument is not supplied, then the clip in the special Last variable will be used.
  • Functions support named arguments. Simply enclose an argument's name inside double quotes to make it a named argument. Note that after doing so the following apply:
    1. All subsequent arguments in the argument list must be named also.
    2. A named argument is an optional argument, that is, it need not be supplied by the caller.
    3. When a function is called, any optional argument which has not been provided is set to a value which has the void ('undefined') type. This does not mean that its value is random garbage - simply that its type is neither clip, int, float, bool or string and so it has no usable value.
    4. Normally, you should use the Defined function to test if an optional argument has an explicit value, or the Default function, which combines the Defined test with the setting of a default value if appropriate.
    5. A void ('undefined') value can be passed on to another function as one of its optional arguments. This is useful when you want to write a wrapper function that calls another function, preserving the same defaults.
  • If a function body has no (explicit or implicit) return, a void value (ie a value of the 'undefined' type) is returned.
  • Functions always produce a new value and never modify an existing one. What that means is that all arguments to a function are passed "by value" and not "by reference"; in order to alter a variable's value in AviSynth script language you must assign to it a new value.
  • Functions can call other functions, including themselves. The latter is known as recursion and is a very useful technique for creating functions that can accomplish complex tasks. See Control structures for more.
  • Local function variables mask global ones with the same name inside the function body. For example, if you define in a function a local variable myvar by assigning to it a value, then you cannot read the global myvar anymore inside this function.
  • The above is also true for arguments, since from the perspective of a function arguments are initialized local variables.
  • Each function has its own local version of the special variable Last. On entry to a function, Last is set to a void ('undefined') value.

Related Links

  • Shared functions – an ever growing collection of shared script functions created by the members of the AviSynth community.
  • Category:Scripts – even more scripts.

TODO

http://www.avisynth.org/ExportingSingleImage
http://www.avisynth.org/HowToApplyFilterToManySingleFrames
Perhaps make decent functions from the last two?

Back to AviSynth Syntax.

Personal tools