x, y and z. This applies both to named and anonymous functions:

``````q)f: {x + y + z}
q)f[1; 2; 3] 6
q){x + y + z}[1; 2; 3] 6
q)
``````

If z appears in the definition of an argument-less function, then the function takes 3 arguments. It is not necessary for x or y to appear:

``````q)f: {z}
q)f
{z}
q)f[3; 4; 5]
5
q)
``````

Similarly, if y appears but z does not appear, then the function takes 2 arguments. What may surprise you is that, if none of x, y, or z appear, then the function takes a single argument called x:

``````q)f: {42}
q)f[] 42
q)value f
0xa00001
,`x
`symbol\$()
,`
42
“{42}”
q)
``````

This is only of practical import if you want to test a function for how many arguments it takes. If you really want to define a function taking no arguments, you might try to use an empty parameter list:

``````q)f: {[] 47}
q)f[] 42
q)value f
0xa00001
,`
`symbol\$()
,`
47
“{[] 47}”
q)
``````

While we’ve succeeded only in defining a function with a single, unnamed parameter, all is not lost. If you follow a convention of always declaring nullary functions in this way, that’s enough to test at runtime how many arguments a function requires.

kdbfaq

208 Words

2011-03-12 00:32 +0000