raz / site / content / tour / 05-procedures.txt

161 lines · 4.0 KiB · raw · history · last change a5145f64

1title: Procedures and several results
2part: Raz
3summary: Value and var parameters, several results, forward, and procedures as values.
4---
5```raz Procs run
6```
7
8A 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
12A 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
15module Params;
16import Out;
17
18proc bump(n: INT); (* changes its own copy *)
19begin
20 n := n + 1;
21end;
22
23proc bumpIn(var n: INT); (* changes the caller's *)
24begin
25 n := n + 1;
26end;
27
28proc fill(var a: [4]INT; v: INT); (* an array is copied unless var *)
29var i: INT;
30begin
31 for i := 0 to 3 do a[i] := v; end;
32end;
33
34proc main*;
35var x: INT; a: [4]INT;
36begin
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 *)
44end;
45
46end Params.
47```
48
49## Several results
50
51Instead 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
54module Several;
55import Out;
56
57proc divmod(a, b: INT): (INT, INT);
58begin
59 return a div b, a mod b;
60end;
61
62proc minmax(a, b: INT): (INT, INT);
63begin
64 if a < b then return a, b; end;
65 return b, a;
66end;
67
68proc spread(a, b: INT): (INT, INT);
69begin
70 return minmax(a, b); (* passes both results on *)
71end;
72
73proc main*;
74var q, r, lo, hi: INT;
75begin
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;
82end;
83
84end Several.
85```
86
87## Recursion, and `forward`
88
89A 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
92module Recur;
93import Out;
94
95proc gcd(a, b: INT): INT;
96begin
97 if b = 0 then return a; end;
98 return gcd(b, a mod b);
99end;
100
101proc isOdd(n: INT): BOOL; forward;
102
103proc isEven(n: INT): BOOL;
104begin
105 if n = 0 then return true; end;
106 return isOdd(n - 1);
107end;
108
109proc isOdd(n: INT): BOOL;
110begin
111 if n = 0 then return false; end;
112 return isEven(n - 1);
113end;
114
115proc main*;
116begin
117 Out.int(gcd(84, 36)); Out.ln;
118 if isEven(10) and isOdd(7) then Out.str("parity works\n"); end;
119end;
120
121end Recur.
122```
123
124## Procedures are values
125
126A 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
129module Table;
130import Out;
131
132type Op = proc(a, b: INT): INT;
133
134proc add(a, b: INT): INT; begin return a + b; end;
135proc sub(a, b: INT): INT; begin return a - b; end;
136proc mul(a, b: INT): INT; begin return a * b; end;
137
138proc apply(f: Op; a, b: INT): INT;
139begin
140 return f(a, b);
141end;
142
143proc main*;
144var ops: [3]Op; names: [3][]const U8; i: INT;
145begin
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;
152end;
153
154end Table.
155```
156
157A 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
161A 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 `_`.