@@ -5,7 +5,7 @@ New functions are declared with the syntax
55fn <id> ( [<param> [, <param> ...]] ) : <expr>
66```
77where
8- * ` <id> ` is an identifier representing the name of the function,
8+ * ` <id> ` is an [ identifier] ( ../queries.md#identifiers ) representing the name of the function,
99* each ` <param> ` is an identifier representing a positional argument to the function, and
1010* ` <expr> ` is any [ expression] ( ../expressions/intro.md ) that implements the function.
1111
@@ -14,9 +14,9 @@ Function declarations must appear in the declaration section of a [scope](../que
1414The function body ` <expr> ` may refer to the passed-in arguments by name.
1515
1616Specifically, the references to the named parameters are
17- field references of the special value ` this ` , as in any expression.
17+ field references of the [ special value] ( ../intro.md#pipe-scoping ) ` this ` , as in any expression.
1818In particular, the value of ` this ` referenced in a function body
19- is formed as record from the actual values passed to the function
19+ is formed as a [ record] ( ../types/record.md ) from the actual values passed to the function
2020where the field names correspond to the parameters of the function.
2121
2222For example, the function ` add ` as defined by
@@ -25,21 +25,21 @@ fn add(a,b): a+b
2525```
2626when invoked as
2727```
28- values {x:1} | values add(x,1 )
28+ values {x:1} | values add(x,2 )
2929```
3030is passed the record the ` {a:x,b:1} ` , which after resolving ` x ` to ` 1 ` ,
31- is ` {a:1,b:1 } ` and thus evaluates the expression
31+ is ` {a:1,b:2 } ` and thus evaluates the expression
3232```
3333this.a + this.b
3434```
35- which results in ` 2 ` .
35+ which results in ` 3 ` .
3636
37- Any function-as-value arguments passed to a function do not appear in the ` this `
37+ Any [ function references ] ( ../expressions/functions.md#function-references ) passed to a function do not appear in the ` this `
3838record formed from the parameters. Instead, function values are expanded at their
39- call sites in a macro-like fashion.
39+ call sites in a [ macro] ( https://en.wikipedia.org/wiki/Macro_(computer_science) ) -like fashion.
4040
4141Functions may be recursive. If the maximum call stack depth is exceeded,
42- the function returns an error value indicating so. Recursive functions that
42+ the function returns an [ error] ( ../types/error.md ) value indicating so. Recursive functions that
4343run for an extended period of time without exceeding the stack depth will simply
4444be allowed to run indefinitely and stall the query result.
4545
@@ -56,7 +56,7 @@ fn <id> ( [<param> [, <param> ...]] ) : (
5656where ` <query> ` is any [ query] ( ../queries.md ) and is simply wrapped in parentheses
5757to form the subquery.
5858
59- As with any subquery, when multiple results are expected, an array subquery
59+ As with any subquery, when multiple results are expected, an [ array subquery] ( ../expressions/subqueries.md#array-subqueries )
6060may be used by wrapping ` <query> ` in square brackets instead of parentheses:
6161```
6262fn <id> ( [<param> [, <param> ...]] ) : [
@@ -67,24 +67,35 @@ fn <id> ( [<param> [, <param> ...]] ) : [
6767Note when using a subquery expression in this fashion,
6868the function's parameters do not appear in the scope of the expressions
6969embedded in the query. For example, this function results in a type error:
70- ```
70+ ``` mdtest-spq fails {data-layout='no-labels'} {style='margin:auto;width:85%'}
71+ # spq
7172fn apply(a,val): (
7273 unnest a
73- | collect(this+val))
74+ | collect(this+val)
7475)
7576values apply([1,2,3], 1)
77+ # input
78+
79+ # expected output
80+ "val" no such field at line 3, column 18:
81+ | collect(this+val)
82+ ~~~
7683```
7784because the field reference to ` val ` within the subquery does not exist.
78- Instead the parameter ` val ` can be carried into the subquery using the
79- alternative form of [ unnest] ( ../operators/unnest.md ) :
80- ```
85+ Instead the parameter ` val ` can be carried into the subquery using
86+ the compound form of [ unnest] ( ../operators/unnest.md ) :
87+ ``` mdtest-spq {data-layout='no-labels'} {style='margin:auto;width:85%'}
88+ # spq
8189fn apply(a,val): (
8290 unnest {outer:val,item:a}
8391 | collect(outer+item)
8492)
8593values apply([1,2,3], 1)
94+ # input
95+
96+ # expected output
97+ [2,3,4]
8698```
87- See the example below.
8899
89100### Examples
90101
@@ -139,7 +150,7 @@ values stats(a)
139150{avg:4.,min:4,max:4,mode:4}
140151```
141152---
142- _ Function arguments are actually fields in the " this" record_
153+ _ Function arguments are actually fields in the ` this ` record_
143154
144155``` mdtest-spq
145156# spq
@@ -151,7 +162,7 @@ values that(x,y,3)
151162{a:1,b:2,c:3}
152163```
153164---
154- _ Functions passed as values do not appear in the " this" record_
165+ _ Functions passed as values do not appear in the ` this ` record_
155166
156167``` mdtest-spq
157168# spq
0 commit comments