| 1 | <!doctype html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | <title>Procedures and several results - Raz, Set, Go</title> |
| 7 | <meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux."> |
| 8 | <meta name="theme-color" content="#E07A5F"> |
| 9 | <meta property="og:title" content="Procedures and several results - Raz, Set, Go"> |
| 10 | <meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report."> |
| 11 | <meta property="og:image" content="https://raz.lucela.org/social.png"> |
| 12 | <meta name="twitter:card" content="summary_large_image"> |
| 13 | <link rel="icon" href="../favicon.svg" type="image/svg+xml"> |
| 14 | <link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32"> |
| 15 | <link rel="apple-touch-icon" href="../apple-touch-icon.png"> |
| 16 | <link rel="stylesheet" href="../style.css"> |
| 17 | </head> |
| 18 | <body> |
| 19 | <header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header> |
| 20 | <main> |
| 21 | <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'> |
| 22 | <li><a href="hello.html">Hello, world</a></li> |
| 23 | <li><a href="modules.html">Modules</a></li> |
| 24 | <li><a href="integers.html">Integers</a></li> |
| 25 | <li><a href="values.html">Values and conversions</a></li> |
| 26 | <li><a href="procedures.html" aria-current="page">Procedures and several results</a></li> |
| 27 | </ol> |
| 28 | <h3>Set</h3><ol start='6'> |
| 29 | <li><a href="slices.html">Arrays, slices and strings</a></li> |
| 30 | <li><a href="records.html">Records and pointers</a></li> |
| 31 | <li><a href="variants.html">Enumerations, sets, variants and match</a></li> |
| 32 | <li><a href="memory.html">Memory and allocators</a></li> |
| 33 | </ol> |
| 34 | <h3>Go</h3><ol start='10'> |
| 35 | <li><a href="errors.html">Errors</a></li> |
| 36 | <li><a href="defer.html">Defer</a></li> |
| 37 | <li><a href="checks.html">When it goes wrong</a></li> |
| 38 | <li><a href="generics.html">Generic modules</a></li> |
| 39 | <li><a href="c.html">Talking to C</a></li> |
| 40 | <li><a href="threads.html">Threads</a></li> |
| 41 | </ol></aside><article><p class="kicker">Raz · lesson 5 of 15</p><h1>Procedures and several results</h1> |
| 42 | <div class="file"><span>Procs.raz</span></div><pre class="code"><code><span class="kw">module</span> Procs; |
| 43 | <span class="kw">import</span> Out; |
| 44 | |
| 45 | <span class="kw">proc</span> divmod(a, b: <span class="ty">INT</span>): (<span class="ty">INT</span>, <span class="ty">INT</span>); |
| 46 | <span class="kw">begin</span> |
| 47 | <span class="kw">return</span> a <span class="kw">div</span> b, a <span class="kw">mod</span> b; |
| 48 | <span class="kw">end</span>; |
| 49 | |
| 50 | <span class="kw">proc</span> swap(<span class="kw">var</span> a, b: <span class="ty">INT</span>); |
| 51 | <span class="kw">var</span> t: <span class="ty">INT</span>; |
| 52 | <span class="kw">begin</span> |
| 53 | t := a; |
| 54 | a := b; |
| 55 | b := t; |
| 56 | <span class="kw">end</span>; |
| 57 | |
| 58 | <span class="kw">proc</span> square(n: <span class="ty">INT</span>): <span class="ty">INT</span>; |
| 59 | <span class="kw">begin</span> |
| 60 | <span class="kw">return</span> n * n; |
| 61 | <span class="kw">end</span>; |
| 62 | |
| 63 | <span class="kw">proc</span> twice(f: <span class="kw">proc</span>(n: <span class="ty">INT</span>): <span class="ty">INT</span>; n: <span class="ty">INT</span>): <span class="ty">INT</span>; |
| 64 | <span class="kw">begin</span> |
| 65 | <span class="kw">return</span> f(f(n)); |
| 66 | <span class="kw">end</span>; |
| 67 | |
| 68 | <span class="kw">proc</span> main*; |
| 69 | <span class="kw">var</span> q, r, x, y: <span class="ty">INT</span>; |
| 70 | <span class="kw">begin</span> |
| 71 | q, r := divmod(<span class="number">17</span>, <span class="number">5</span>); |
| 72 | x := <span class="number">1</span>; |
| 73 | y := <span class="number">2</span>; |
| 74 | swap(x, y); |
| 75 | Out.int(q); Out.char(<span class="string">' '</span>); Out.int(r); Out.ln; |
| 76 | Out.int(x); Out.char(<span class="string">' '</span>); Out.int(y); Out.ln; |
| 77 | Out.int(twice(square, <span class="number">3</span>)); Out.ln; |
| 78 | _, r := divmod(-<span class="number">7</span>, <span class="number">2</span>); |
| 79 | Out.int(r); Out.ln; |
| 80 | <span class="kw">end</span>; |
| 81 | |
| 82 | <span class="kw">end</span> Procs.</code></pre> |
| 83 | <pre class="output"><code>3 2 |
| 84 | 2 1 |
| 85 | 81 |
| 86 | -1 |
| 87 | </code></pre> |
| 88 | <p>A parameter is a copy, unless it is <code>var</code>, when it is the caller's variable itself. A procedure may return several results, which a multiple assignment takes apart; the blank, <code>_</code>, drops one.</p> |
| 89 | <h2>Value and <code>var</code> parameters</h2> |
| 90 | <p>A value parameter is the procedure's own: it may change it, and the caller's variable stays as it was. A <code>var</code> parameter is the caller's variable, and its argument must be a variable of exactly that type.</p> |
| 91 | <div class="file"><span>Params.raz</span></div><pre class="code"><code><span class="kw">module</span> Params; |
| 92 | <span class="kw">import</span> Out; |
| 93 | |
| 94 | <span class="kw">proc</span> bump(n: <span class="ty">INT</span>); <span class="comment">(* changes its own copy *)</span> |
| 95 | <span class="kw">begin</span> |
| 96 | n := n + <span class="number">1</span>; |
| 97 | <span class="kw">end</span>; |
| 98 | |
| 99 | <span class="kw">proc</span> bumpIn(<span class="kw">var</span> n: <span class="ty">INT</span>); <span class="comment">(* changes the caller's *)</span> |
| 100 | <span class="kw">begin</span> |
| 101 | n := n + <span class="number">1</span>; |
| 102 | <span class="kw">end</span>; |
| 103 | |
| 104 | <span class="kw">proc</span> fill(<span class="kw">var</span> a: [<span class="number">4</span>]<span class="ty">INT</span>; v: <span class="ty">INT</span>); <span class="comment">(* an array is copied unless var *)</span> |
| 105 | <span class="kw">var</span> i: <span class="ty">INT</span>; |
| 106 | <span class="kw">begin</span> |
| 107 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">3</span> <span class="kw">do</span> a[i] := v; <span class="kw">end</span>; |
| 108 | <span class="kw">end</span>; |
| 109 | |
| 110 | <span class="kw">proc</span> main*; |
| 111 | <span class="kw">var</span> x: <span class="ty">INT</span>; a: [<span class="number">4</span>]<span class="ty">INT</span>; |
| 112 | <span class="kw">begin</span> |
| 113 | x := <span class="number">10</span>; |
| 114 | bump(x); |
| 115 | Out.int(x); Out.ln; <span class="comment">(* 10 *)</span> |
| 116 | bumpIn(x); |
| 117 | Out.int(x); Out.ln; <span class="comment">(* 11 *)</span> |
| 118 | fill(a, <span class="number">7</span>); |
| 119 | Out.int(a[<span class="number">0</span>] + a[<span class="number">3</span>]); Out.ln; <span class="comment">(* 14 *)</span> |
| 120 | <span class="kw">end</span>; |
| 121 | |
| 122 | <span class="kw">end</span> Params.</code></pre> |
| 123 | <pre class="output"><code>10 |
| 124 | 11 |
| 125 | 14 |
| 126 | </code></pre> |
| 127 | <h2>Several results</h2> |
| 128 | <p>Instead of an output parameter, a procedure says what it returns: <code>(INT, INT)</code>, or any list of types. The caller takes them apart with a multiple assignment. A call that returns several values may stand on the right of one, alone as a statement, or alone after <code>return</code>, which passes them on as they are.</p> |
| 129 | <div class="file"><span>Several.raz</span></div><pre class="code"><code><span class="kw">module</span> Several; |
| 130 | <span class="kw">import</span> Out; |
| 131 | |
| 132 | <span class="kw">proc</span> divmod(a, b: <span class="ty">INT</span>): (<span class="ty">INT</span>, <span class="ty">INT</span>); |
| 133 | <span class="kw">begin</span> |
| 134 | <span class="kw">return</span> a <span class="kw">div</span> b, a <span class="kw">mod</span> b; |
| 135 | <span class="kw">end</span>; |
| 136 | |
| 137 | <span class="kw">proc</span> minmax(a, b: <span class="ty">INT</span>): (<span class="ty">INT</span>, <span class="ty">INT</span>); |
| 138 | <span class="kw">begin</span> |
| 139 | <span class="kw">if</span> a < b <span class="kw">then</span> <span class="kw">return</span> a, b; <span class="kw">end</span>; |
| 140 | <span class="kw">return</span> b, a; |
| 141 | <span class="kw">end</span>; |
| 142 | |
| 143 | <span class="kw">proc</span> spread(a, b: <span class="ty">INT</span>): (<span class="ty">INT</span>, <span class="ty">INT</span>); |
| 144 | <span class="kw">begin</span> |
| 145 | <span class="kw">return</span> minmax(a, b); <span class="comment">(* passes both results on *)</span> |
| 146 | <span class="kw">end</span>; |
| 147 | |
| 148 | <span class="kw">proc</span> main*; |
| 149 | <span class="kw">var</span> q, r, lo, hi: <span class="ty">INT</span>; |
| 150 | <span class="kw">begin</span> |
| 151 | q, r := divmod(<span class="number">100</span>, <span class="number">7</span>); |
| 152 | Out.int(q); Out.str(<span class="string">" rest "</span>); Out.int(r); Out.ln; |
| 153 | lo, hi := spread(<span class="number">9</span>, <span class="number">4</span>); |
| 154 | Out.int(lo); Out.str(<span class="string">" .. "</span>); Out.int(hi); Out.ln; |
| 155 | _, r := divmod(-<span class="number">7</span>, <span class="number">2</span>); <span class="comment">(* drop the quotient *)</span> |
| 156 | Out.int(r); Out.ln; |
| 157 | <span class="kw">end</span>; |
| 158 | |
| 159 | <span class="kw">end</span> Several.</code></pre> |
| 160 | <pre class="output"><code>14 rest 2 |
| 161 | 4 .. 9 |
| 162 | -1 |
| 163 | </code></pre> |
| 164 | <h2>Recursion, and <code>forward</code></h2> |
| 165 | <p>A name is declared before it is used, and procedures don't nest. When two procedures need each other, <code>forward</code> declares the first so that its body can come later.</p> |
| 166 | <div class="file"><span>Recur.raz</span></div><pre class="code"><code><span class="kw">module</span> Recur; |
| 167 | <span class="kw">import</span> Out; |
| 168 | |
| 169 | <span class="kw">proc</span> gcd(a, b: <span class="ty">INT</span>): <span class="ty">INT</span>; |
| 170 | <span class="kw">begin</span> |
| 171 | <span class="kw">if</span> b = <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> a; <span class="kw">end</span>; |
| 172 | <span class="kw">return</span> gcd(b, a <span class="kw">mod</span> b); |
| 173 | <span class="kw">end</span>; |
| 174 | |
| 175 | <span class="kw">proc</span> isOdd(n: <span class="ty">INT</span>): <span class="ty">BOOL</span>; <span class="kw">forward</span>; |
| 176 | |
| 177 | <span class="kw">proc</span> isEven(n: <span class="ty">INT</span>): <span class="ty">BOOL</span>; |
| 178 | <span class="kw">begin</span> |
| 179 | <span class="kw">if</span> n = <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="ty">true</span>; <span class="kw">end</span>; |
| 180 | <span class="kw">return</span> isOdd(n - <span class="number">1</span>); |
| 181 | <span class="kw">end</span>; |
| 182 | |
| 183 | <span class="kw">proc</span> isOdd(n: <span class="ty">INT</span>): <span class="ty">BOOL</span>; |
| 184 | <span class="kw">begin</span> |
| 185 | <span class="kw">if</span> n = <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="ty">false</span>; <span class="kw">end</span>; |
| 186 | <span class="kw">return</span> isEven(n - <span class="number">1</span>); |
| 187 | <span class="kw">end</span>; |
| 188 | |
| 189 | <span class="kw">proc</span> main*; |
| 190 | <span class="kw">begin</span> |
| 191 | Out.int(gcd(<span class="number">84</span>, <span class="number">36</span>)); Out.ln; |
| 192 | <span class="kw">if</span> isEven(<span class="number">10</span>) <span class="kw">and</span> isOdd(<span class="number">7</span>) <span class="kw">then</span> Out.str(<span class="string">"parity works\n"</span>); <span class="kw">end</span>; |
| 193 | <span class="kw">end</span>; |
| 194 | |
| 195 | <span class="kw">end</span> Recur.</code></pre> |
| 196 | <pre class="output"><code>12 |
| 197 | parity works |
| 198 | </code></pre> |
| 199 | <h2>Procedures are values</h2> |
| 200 | <p>A procedure used without parentheses is a procedure value, of a procedure type such as <code>proc(a, b: INT): INT</code>. It can be passed, stored in a variable or a record, and called. This is raz's only dynamic dispatch, and it is as plain as it looks.</p> |
| 201 | <div class="file"><span>Table.raz</span></div><pre class="code"><code><span class="kw">module</span> Table; |
| 202 | <span class="kw">import</span> Out; |
| 203 | |
| 204 | <span class="kw">type</span> Op = <span class="kw">proc</span>(a, b: <span class="ty">INT</span>): <span class="ty">INT</span>; |
| 205 | |
| 206 | <span class="kw">proc</span> add(a, b: <span class="ty">INT</span>): <span class="ty">INT</span>; <span class="kw">begin</span> <span class="kw">return</span> a + b; <span class="kw">end</span>; |
| 207 | <span class="kw">proc</span> sub(a, b: <span class="ty">INT</span>): <span class="ty">INT</span>; <span class="kw">begin</span> <span class="kw">return</span> a - b; <span class="kw">end</span>; |
| 208 | <span class="kw">proc</span> mul(a, b: <span class="ty">INT</span>): <span class="ty">INT</span>; <span class="kw">begin</span> <span class="kw">return</span> a * b; <span class="kw">end</span>; |
| 209 | |
| 210 | <span class="kw">proc</span> apply(f: Op; a, b: <span class="ty">INT</span>): <span class="ty">INT</span>; |
| 211 | <span class="kw">begin</span> |
| 212 | <span class="kw">return</span> f(a, b); |
| 213 | <span class="kw">end</span>; |
| 214 | |
| 215 | <span class="kw">proc</span> main*; |
| 216 | <span class="kw">var</span> ops: [<span class="number">3</span>]Op; names: [<span class="number">3</span>][]<span class="kw">const</span> <span class="ty">U8</span>; i: <span class="ty">INT</span>; |
| 217 | <span class="kw">begin</span> |
| 218 | ops := [add, sub, mul]; |
| 219 | names := [<span class="string">"add"</span>, <span class="string">"sub"</span>, <span class="string">"mul"</span>]; |
| 220 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">2</span> <span class="kw">do</span> |
| 221 | Out.str(names[i]); Out.str(<span class="string">": "</span>); |
| 222 | Out.int(apply(ops[i], <span class="number">12</span>, <span class="number">5</span>)); Out.ln; |
| 223 | <span class="kw">end</span>; |
| 224 | <span class="kw">end</span>; |
| 225 | |
| 226 | <span class="kw">end</span> Table.</code></pre> |
| 227 | <pre class="output"><code>add: 17 |
| 228 | sub: 7 |
| 229 | mul: 60 |
| 230 | </code></pre> |
| 231 | <p>A procedure without parameters leaves out the parentheses where it is called, as <code>Out.ln</code> does. That means <code>f</code> alone is the procedure and <code>f()</code> would not be a call, so be careful when passing one around.</p> |
| 232 | <h2>Dropped results</h2> |
| 233 | <p>A call that drops a result, as <code>Sys.write(fd, s);</code> does, is warned of; <code>_ := Sys.write(fd, s);</code> says it is meant. The compiler also warns of a variable that is assigned and never read, and of a name declared and never used, unless it starts with <code>_</code>.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="values.html">← Values and conversions</a><a href="index.html">Contents</a><a class="next" href="slices.html">Arrays, slices and strings →</a></div></article></div> |
| 234 | </main> |
| 235 | <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer> |
| 236 | </body> |
| 237 | </html> |