functions.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. A function call in Squirrel passes the current environment object *this* as a hidden parameter.
  102. But when the function was immediately indexed from an object, *this* shall be the object
  103. which was indexed, instead.
  104. If we call a function with the syntax::
  105. mytable.foo(x,y)
  106. the environment object passed to 'foo' as *this* will be 'mytable' (since 'foo' was immediately indexed from 'mytable')
  107. Whereas with the syntax::
  108. foo(x,y) // implicitly equivalent to this.foo(x,y)
  109. the environment object will be the current *this* (that is, propagated from the caller's *this*).
  110. It may help to remember the rules in the following way:
  111. foo(x,y) ---> this.foo(x,y)
  112. table.foo(x,y) ---> call foo with (table,x,y)
  113. It may also help to consider why it works this way: it's designed to assist with object-oriented style.
  114. When calling 'foo(x,y)' it's assumed you're calling another member of the object (or of the file) and
  115. so should operate on the same object.
  116. When calling 'mytable.foo(x,y)' it's written plainly that you're calling a member of a different object.
  117. ---------------------------------------------
  118. Binding an environment to a function
  119. ---------------------------------------------
  120. .. index::
  121. single: Binding an environment to a function
  122. while by default a squirrel function call passes as environment object 'this', the object
  123. where the function was indexed from. However, is also possible to statically bind an evironment to a
  124. closure using the built-in method ``closure.bindenv(env_obj)``.
  125. The method bindenv() returns a new instance of a closure with the environment bound to it.
  126. When an environment object is bound to a function, every time the function is invoked, its
  127. 'this' parameter will always be the previously bound environent.
  128. This mechanism is useful to implement callbacks systems similar to C# delegates.
  129. .. note:: The closure keeps a weak reference to the bound environmet object, because of this if
  130. the object is deleted, the next call to the closure will result in a ``null``
  131. environment object.
  132. ---------------------------------------------
  133. Lambda Expressions
  134. ---------------------------------------------
  135. .. index::
  136. single: Lambda Expressions
  137. ::
  138. exp := '@' '(' paramlist ')' exp
  139. Lambda expressions are a synctactic sugar to quickly define a function that consists of a single expression.
  140. This feature comes handy when functional programming patterns are applied, like map/reduce or passing a compare method to
  141. array.sort().
  142. here a lambda expression::
  143. local myexp = @(a,b) a + b
  144. that is equivalent to::
  145. local myexp = function(a,b) { return a + b; }
  146. a more useful usage could be::
  147. local arr = [2,3,5,8,3,5,1,2,6];
  148. arr.sort(@(a,b) a <=> b);
  149. arr.sort(@(a,b) -(a <=> b));
  150. that could have been written as::
  151. local arr = [2,3,5,8,3,5,1,2,6];
  152. arr.sort(function(a,b) { return a <=> b; } );
  153. arr.sort(function(a,b) { return -(a <=> b); } );
  154. other than being limited to a single expression lambdas support all features of regular functions.
  155. in fact are implemented as a compile time feature.
  156. ---------------------------------------------
  157. Free Variables
  158. ---------------------------------------------
  159. .. index::
  160. single: Free Variables
  161. A free variable is a variable external from the function scope as is not a local variable
  162. or parameter of the function.
  163. Free variables reference a local variable from a outer scope.
  164. In the following example the variables 'testy', 'x' and 'y' are bound to the function 'foo'.::
  165. local x=10,y=20
  166. local testy="I'm testy"
  167. function foo(a,b)
  168. {
  169. ::print(testy);
  170. return a+b+x+y;
  171. }
  172. A program can read or write a free variable.
  173. ---------------------------------------------
  174. Tail Recursion
  175. ---------------------------------------------
  176. .. index::
  177. single: Tail Recursion
  178. Tail recursion is a method for partially transforming a recursion in a program into an
  179. iteration: it applies when the recursive calls in a function are the last executed
  180. statements in that function (just before the return).
  181. If this happenes the squirrel interpreter collapses the caller stack frame before the
  182. recursive call; because of that very deep recursions are possible without risk of a stack
  183. overflow.::
  184. function loopy(n)
  185. {
  186. if(n>0){
  187. ::print("n="+n+"\n");
  188. return loopy(n-1);
  189. }
  190. }
  191. loopy(1000);