| 1 | title: Procedures and several results |
| 2 | part: Raz |
| 3 | summary: Value and var parameters, several results, forward, and procedures as values. |
| 4 | --- |
| 5 | ```raz Procs run |
| 6 | ``` |
| 7 | |
| 8 | A parameter is a copy, unless it is `var`, when it is the caller's variable itself. A procedure may return several results, which a multiple assignment takes apart; the blank, `_`, drops one. |
| 9 | |
| 10 | ## Value and `var` parameters |
| 11 | |
| 12 | A value parameter is the procedure's own: it may change it, and the caller's variable stays as it was. A `var` parameter is the caller's variable, and its argument must be a variable of exactly that type. |
| 13 | |
| 14 | ```raz Params run |
| 15 | module Params; |
| 16 | import Out; |
| 17 | |
| 18 | proc bump(n: INT); (* changes its own copy *) |
| 19 | begin |
| 20 | n := n + 1; |
| 21 | end; |
| 22 | |
| 23 | proc bumpIn(var n: INT); (* changes the caller's *) |
| 24 | begin |
| 25 | n := n + 1; |
| 26 | end; |
| 27 | |
| 28 | proc fill(var a: [4]INT; v: INT); (* an array is copied unless var *) |
| 29 | var i: INT; |
| 30 | begin |
| 31 | for i := 0 to 3 do a[i] := v; end; |
| 32 | end; |
| 33 | |
| 34 | proc main*; |
| 35 | var x: INT; a: [4]INT; |
| 36 | begin |
| 37 | x := 10; |
| 38 | bump(x); |
| 39 | Out.int(x); Out.ln; (* 10 *) |
| 40 | bumpIn(x); |
| 41 | Out.int(x); Out.ln; (* 11 *) |
| 42 | fill(a, 7); |
| 43 | Out.int(a[0] + a[3]); Out.ln; (* 14 *) |
| 44 | end; |
| 45 | |
| 46 | end Params. |
| 47 | ``` |
| 48 | |
| 49 | ## Several results |
| 50 | |
| 51 | Instead of an output parameter, a procedure says what it returns: `(INT, INT)`, 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 `return`, which passes them on as they are. |
| 52 | |
| 53 | ```raz Several run |
| 54 | module Several; |
| 55 | import Out; |
| 56 | |
| 57 | proc divmod(a, b: INT): (INT, INT); |
| 58 | begin |
| 59 | return a div b, a mod b; |
| 60 | end; |
| 61 | |
| 62 | proc minmax(a, b: INT): (INT, INT); |
| 63 | begin |
| 64 | if a < b then return a, b; end; |
| 65 | return b, a; |
| 66 | end; |
| 67 | |
| 68 | proc spread(a, b: INT): (INT, INT); |
| 69 | begin |
| 70 | return minmax(a, b); (* passes both results on *) |
| 71 | end; |
| 72 | |
| 73 | proc main*; |
| 74 | var q, r, lo, hi: INT; |
| 75 | begin |
| 76 | q, r := divmod(100, 7); |
| 77 | Out.int(q); Out.str(" rest "); Out.int(r); Out.ln; |
| 78 | lo, hi := spread(9, 4); |
| 79 | Out.int(lo); Out.str(" .. "); Out.int(hi); Out.ln; |
| 80 | _, r := divmod(-7, 2); (* drop the quotient *) |
| 81 | Out.int(r); Out.ln; |
| 82 | end; |
| 83 | |
| 84 | end Several. |
| 85 | ``` |
| 86 | |
| 87 | ## Recursion, and `forward` |
| 88 | |
| 89 | A name is declared before it is used, and procedures don't nest. When two procedures need each other, `forward` declares the first so that its body can come later. |
| 90 | |
| 91 | ```raz Recur run |
| 92 | module Recur; |
| 93 | import Out; |
| 94 | |
| 95 | proc gcd(a, b: INT): INT; |
| 96 | begin |
| 97 | if b = 0 then return a; end; |
| 98 | return gcd(b, a mod b); |
| 99 | end; |
| 100 | |
| 101 | proc isOdd(n: INT): BOOL; forward; |
| 102 | |
| 103 | proc isEven(n: INT): BOOL; |
| 104 | begin |
| 105 | if n = 0 then return true; end; |
| 106 | return isOdd(n - 1); |
| 107 | end; |
| 108 | |
| 109 | proc isOdd(n: INT): BOOL; |
| 110 | begin |
| 111 | if n = 0 then return false; end; |
| 112 | return isEven(n - 1); |
| 113 | end; |
| 114 | |
| 115 | proc main*; |
| 116 | begin |
| 117 | Out.int(gcd(84, 36)); Out.ln; |
| 118 | if isEven(10) and isOdd(7) then Out.str("parity works\n"); end; |
| 119 | end; |
| 120 | |
| 121 | end Recur. |
| 122 | ``` |
| 123 | |
| 124 | ## Procedures are values |
| 125 | |
| 126 | A procedure used without parentheses is a procedure value, of a procedure type such as `proc(a, b: INT): INT`. 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. |
| 127 | |
| 128 | ```raz Table run |
| 129 | module Table; |
| 130 | import Out; |
| 131 | |
| 132 | type Op = proc(a, b: INT): INT; |
| 133 | |
| 134 | proc add(a, b: INT): INT; begin return a + b; end; |
| 135 | proc sub(a, b: INT): INT; begin return a - b; end; |
| 136 | proc mul(a, b: INT): INT; begin return a * b; end; |
| 137 | |
| 138 | proc apply(f: Op; a, b: INT): INT; |
| 139 | begin |
| 140 | return f(a, b); |
| 141 | end; |
| 142 | |
| 143 | proc main*; |
| 144 | var ops: [3]Op; names: [3][]const U8; i: INT; |
| 145 | begin |
| 146 | ops := [add, sub, mul]; |
| 147 | names := ["add", "sub", "mul"]; |
| 148 | for i := 0 to 2 do |
| 149 | Out.str(names[i]); Out.str(": "); |
| 150 | Out.int(apply(ops[i], 12, 5)); Out.ln; |
| 151 | end; |
| 152 | end; |
| 153 | |
| 154 | end Table. |
| 155 | ``` |
| 156 | |
| 157 | A procedure without parameters leaves out the parentheses where it is called, as `Out.ln` does. That means `f` alone is the procedure and `f()` would not be a call, so be careful when passing one around. |
| 158 | |
| 159 | ## Dropped results |
| 160 | |
| 161 | A call that drops a result, as `Sys.write(fd, s);` does, is warned of; `_ := Sys.write(fd, s);` 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 `_`. |