functions.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. .. _functions:
  2. =================
  3. Functions
  4. =================
  5. .. index::
  6. single: Functions
  7. Functions are first class values like integer or strings and can be stored in table slots,
  8. local variables, arrays and passed as function parameters.
  9. Functions can be implemented in Squirrel or in a native language with calling conventions
  10. compatible with ANSI C.
  11. --------------------
  12. Function declaration
  13. --------------------
  14. .. index::
  15. single: Function Declaration
  16. Functions are declared through the function expression::
  17. local a = function(a, b, c) { return a + b - c; }
  18. or with the syntactic sugar::
  19. function ciao(a,b,c)
  20. {
  21. return a+b-c;
  22. }
  23. that is equivalent to::
  24. this.ciao <- function(a,b,c)
  25. {
  26. return a+b-c;
  27. }
  28. a local function can be declared with this syntactic sugar::
  29. local function tuna(a,b,c)
  30. {
  31. return a+b-c;
  32. }
  33. that is equivalent to::
  34. local tuna = function(a,b,c)
  35. {
  36. return a+b-c;
  37. }
  38. is also possible to declare something like::
  39. T <- {}
  40. function T::ciao(a,b,c)
  41. {
  42. return a+b-c;
  43. }
  44. //that is equivalent to write
  45. T.ciao <- function(a,b,c)
  46. {
  47. return a+b-c;
  48. }
  49. //or
  50. T <- {
  51. function ciao(a,b,c)
  52. {
  53. return a+b-c;
  54. }
  55. }
  56. ^^^^^^^^^^^^^^^^^^
  57. Default Paramaters
  58. ^^^^^^^^^^^^^^^^^^
  59. .. index::
  60. single: Function Default Paramaters
  61. Squirrel's functions can have default parameters.
  62. A function with default parameters is declared as follows: ::
  63. function test(a,b,c = 10, d = 20)
  64. {
  65. ....
  66. }
  67. when the function *test* is invoked and the parameter c or d are not specified,
  68. the VM autometically assigns the default value to the unspecified parameter. A default parameter can be
  69. any valid squirrel expression. The expression is evaluated at runtime.
  70. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  71. Function with variable number of paramaters
  72. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  73. .. index::
  74. single: Function with variable number of paramaters
  75. Squirrel's functions can have variable number of parameters(varargs functions).
  76. A vararg function is declared by adding three dots (`...`) at the end of its parameter list.
  77. When the function is called all the extra parameters will be accessible through the *array*
  78. called ``vargv``, that is passed as implicit parameter.
  79. ``vargv`` is a regular squirrel array and can be used accordingly.::
  80. function test(a,b,...)
  81. {
  82. for(local i = 0; i< vargv.len(); i++)
  83. {
  84. ::print("varparam "+i+" = "+vargv[i]+"\n");
  85. }
  86. foreach(i,val in vargv)
  87. {
  88. ::print("varparam "+i+" = "+val+"\n");
  89. }
  90. }
  91. test("goes in a","goes in b",0,1,2,3,4,5,6,7,8);
  92. ---------------
  93. Function calls
  94. ---------------
  95. .. index::
  96. single: Function calls
  97. ::
  98. exp:= derefexp '(' explist ')'
  99. The expression is evaluated in this order: derefexp after the explist (arguments) and at
  100. the end the call.
  101. Every function call in Squirrel passes the environment object *this* as hidden parameter
  102. to the called function. The 'this' parameter is the object where the function was indexed
  103. from.
  104. If we call a function with this syntax::
  105. table.foo(a)
  106. the environment object passed to foo will be 'table'::
  107. foo(x,y) // equivalent to this.foo(x,y)
  108. The environment object will be *this* (the same of the caller function).
  109. ---------------------------------------------
  110. Binding an environment to a function
  111. ---------------------------------------------
  112. .. index::
  113. single: Binding an environment to a function
  114. while by default a squirrel function call passes as environment object 'this', the object
  115. where the function was indexed from. However, is also possible to statically bind an evironment to a
  116. closure using the built-in method ``closure.bindenv(env_obj)``.
  117. The method bindenv() returns a new instance of a closure with the environment bound to it.
  118. When an environment object is bound to a function, every time the function is invoked, its
  119. 'this' parameter will always be the previously bound environent.
  120. This mechanism is useful to implement callbacks systems similar to C# delegates.
  121. .. note:: The closure keeps a weak reference to the bound environmet object, because of this if
  122. the object is deleted, the next call to the closure will result in a ``null``
  123. environment object.
  124. ---------------------------------------------
  125. Lambda Expressions
  126. ---------------------------------------------
  127. .. index::
  128. single: Lambda Expressions
  129. ::
  130. exp := '@' '(' paramlist ')' exp
  131. Lambda expressions are a synctactic sugar to quickly define a function that consists of a single expression.
  132. This feature comes handy when functional programming patterns are applied, like map/reduce or passing a compare method to
  133. array.sort().
  134. here a lambda expression::
  135. local myexp = @(a,b) a + b
  136. that is equivalent to::
  137. local myexp = function(a,b) { return a + b; }
  138. a more useful usage could be::
  139. local arr = [2,3,5,8,3,5,1,2,6];
  140. arr.sort(@(a,b) a <=> b);
  141. arr.sort(@(a,b) -(a <=> b));
  142. that could have been written as::
  143. local arr = [2,3,5,8,3,5,1,2,6];
  144. arr.sort(function(a,b) { return a <=> b; } );
  145. arr.sort(function(a,b) { return -(a <=> b); } );
  146. other than being limited to a single expression lambdas support all features of regular functions.
  147. in fact are implemented as a compile time feature.
  148. ---------------------------------------------
  149. Free Variables
  150. ---------------------------------------------
  151. .. index::
  152. single: Free Variables
  153. A free variable is a variable external from the function scope as is not a local variable
  154. or parameter of the function.
  155. Free variables reference a local variable from a outer scope.
  156. In the following example the variables 'testy', 'x' and 'y' are bound to the function 'foo'.::
  157. local x=10,y=20
  158. local testy="I'm testy"
  159. function foo(a,b)
  160. {
  161. ::print(testy);
  162. return a+b+x+y;
  163. }
  164. A program can read or write a free variable.
  165. ---------------------------------------------
  166. Tail Recursion
  167. ---------------------------------------------
  168. .. index::
  169. single: Tail Recursion
  170. Tail recursion is a method for partially transforming a recursion in a program into an
  171. iteration: it applies when the recursive calls in a function are the last executed
  172. statements in that function (just before the return).
  173. If this happenes the squirrel interpreter collapses the caller stack frame before the
  174. recursive call; because of that very deep recursions are possible without risk of a stack
  175. overflow.::
  176. function loopy(n)
  177. {
  178. if(n>0){
  179. ::print("n="+n+"\n");
  180. return loopy(n-1);
  181. }
  182. }
  183. loopy(1000);