functions.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1">
  5. <title>Gravity: Functions</title>
  6. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css" />
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  8. <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,700,800">
  9. <link rel="stylesheet" href="scripts/highlight/styles/github-gist.css">
  10. <link rel="stylesheet" href="stylesheets/styles.css">
  11. </head>
  12. <body>
  13. <!-- BEGIN NAVIGATION BAR -->
  14. <nav class="navbar navbar-default navbar-fixed-top">
  15. <div class="container">
  16. <div class="navbar-header">
  17. <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
  18. <span class="sr-only">Toggle navigation</span>
  19. <span class="icon-bar"></span>
  20. <span class="icon-bar"></span>
  21. <span class="icon-bar"></span>
  22. </button>
  23. <a class="navbar-brand" href="http://gravity-lang.org"><img src="images/logo.png" width="32px" height="36px"></a>
  24. </div>
  25. <div id="navbar" class="navbar-collapse collapse">
  26. <ul class="nav navbar-nav navbar-right">
  27. <li class="active"><a href="index.html">Gravity</a></li>
  28. <li><a href="internals/index.html">Internals</a></li>
  29. <li><a href="https://github.com/marcobambini/gravity">GitHub</a></li>
  30. </ul>
  31. </div><!--/.nav-collapse -->
  32. </div>
  33. </nav>
  34. <!-- END NAVIGATION BAR -->
  35. <div class="container body-container">
  36. <div class="main-content">
  37. <div class="row">
  38. <!-- BEGIN SIDEBAR -->
  39. <div class="col-sm-3 border-right section-left">
  40. <div saveheight="1" class="sidebar-nav">
  41. <h4>INTRODUCTION</h4>
  42. <ul>
  43. <li><a href="index.html"><span>Introduction</span></a></li>
  44. <li><a href="getting-started.html"><span>Getting Started</span></a></li>
  45. </ul>
  46. <h4>LANGUAGE GUIDE</h4>
  47. <ul>
  48. <li><a href="syntax.html"><span>Syntax</span></a></li>
  49. <li><a href="operators.html"><span>Operators</span></a></li>
  50. <li><a href="types.html"><span>Types</span></a></li>
  51. <li><a href="lists.html"><span>Lists</span></a></li>
  52. <li><a href="maps.html"><span>Maps</span></a></li>
  53. <li><a href="enum.html"><span>Enum</span></a></li>
  54. <li><a href="functions.html" class="active"><span>Functions</span></a></li>
  55. <li><a href="closures.html"><span>Closures</span></a></li>
  56. <li><a href="classes.html"><span>Classes</span></a></li>
  57. <li><a href="control-flow.html"><span>Control Flow</span></a></li>
  58. <li><a href="loops.html"><span>Loops</span></a></li>
  59. <li><a href="fibers.html"><span>Fibers</span></a></li>
  60. </ul>
  61. <h4>ADVANCED</h4>
  62. <ul>
  63. <li><a href="api.html"><span>Embedding API</span></a></li>
  64. <li><a href="system.html"><span>System class</span></a></li>
  65. <li><a href="math.html"><span>Math class</span></a></li>
  66. <li><a href="unit-test.html"><span>Unit test</span></a></li>
  67. <li><a href="contributing.html"><span>Contributing</span></a></li>
  68. </ul>
  69. </div>
  70. </div>
  71. <!-- END SIDEBAR -->
  72. <div class="col-sm-9 border-left section-right">
  73. <h1 class="section-header">Function</h1><hr>
  74. <p class="section-content">
  75. Functions are first class objects like <a href="types.html">Int</a> or <a href="types.html">String</a> and can be stored in local variables (even in <a href="list.html">Lists</a> or <a href="map.html">Maps</a>), passed as function parameters or returned by a function. Functions can be implemented in Gravity or in a <a href="api.html">native language</a> with calling conventions compatible with ANSI C.<br><br>Functions are called by value. This means that foo(1) calls the function which is the value of the variable foo. Calling a value that is not a function will raise a runtime error.
  76. </p>
  77. <pre><code class="swift">
  78. func main() {
  79. var a = 10;
  80. var b = 20;
  81. return a + b;
  82. }
  83. </code></pre>
  84. <pre><code class="swift">
  85. func f1() {
  86. return 10;
  87. }
  88. func f2() {
  89. return f1;
  90. }
  91. func main() {
  92. // a is now function f2
  93. var a = f2;
  94. // b is now the return value of f2 which is function f1
  95. var b = a();
  96. // return value is f1() which is 10
  97. return b();
  98. // above code is equivalent to
  99. return f2()();
  100. }
  101. </code></pre>
  102. <h4 class="section-h4">Function parameters</h4>
  103. <p>Functions aren’t very useful if you can’t pass values to them so you can provide a parameter list in the function declaration. Gravity performs no check on the number of parameters so you can call a function providing more or less parameters.</p>
  104. <pre><code class="swift">
  105. func sum(a, b) {
  106. return a + b;
  107. }
  108. // execute the sum function
  109. // and returns 30 as result
  110. sum(10,20);
  111. </code></pre>
  112. <p>If a function is called with missing arguments (less than declared), the missing values are set to <strong>undefined</strong>.</p>
  113. <pre><code class="swift">
  114. // sum modified to take in account missing arguments
  115. func sum(a, b) {
  116. // equivalent to if (a == undefined) a = 30;
  117. if (!a) a = 30;
  118. // equivalent to if (b == undefined) b = 50;
  119. if (!b) b = 50;
  120. return a + b;
  121. }
  122. // execute the sum function without any argument
  123. // a has a 30 default value and b has a 50 default value
  124. // return value is 80
  125. sum();
  126. </code></pre>
  127. <p>If a function is called with more arguments (more than declared), the additional arguments can be accessed using the <strong>_args</strong> array.</p>
  128. <pre><code class="swift">
  129. // sum modified to accept a variable number of arguments
  130. func sum() {
  131. var tot = 0;
  132. for (var i in 0..<_args.count) {
  133. tot += _args[i];
  134. }
  135. return tot;
  136. }
  137. // execute the sum function with a variable number
  138. // of arguments returns 550 as result
  139. sum(10,20,30,40,50,60,70,80,90,100);
  140. </code></pre>
  141. <h4 class="section-h4">Recursion</h4>
  142. <p>Function recursion is fully supported in Gravity (current function can be accessed using the _func reserved keyword):</p>
  143. <pre><code class="swift">
  144. func fibonacci (n) {
  145. if (n<2) return n;
  146. // could be written as return _func(n-2) + _func(n-1)
  147. return fibonacci(n-2) + fibonacci(n-1);
  148. }
  149. func main() {
  150. return fibonacci(20);
  151. }
  152. </code></pre>
  153. <h4 class="section-h4">Returning values</h4>
  154. <p>A function without a return statement returns <strong>null</strong> by default. You can explicitly return a value using a return statement.</p>
  155. </div>
  156. </div> <!-- /row -->
  157. </div> <!-- /main-content -->
  158. </div> <!-- /container -->
  159. <!-- BEGIN FOOTER -->
  160. <footer class="navbar-fixed-bottom">
  161. <div class="container footer">
  162. <p>
  163. </p>
  164. </div>
  165. </footer>
  166. <!-- END FOOTER -->
  167. <!-- Bootstrap JS -->
  168. <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  169. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  170. <!-- Highlights JS -->
  171. <script src="scripts/highlight/highlight.min.js"></script>
  172. <script>hljs.initHighlightingOnLoad();</script>
  173. </body>
  174. </html>