How do I set the return value of a function?

Short answer:      : return_value

Unless you use : (return) or ‘ (signal) to specify otherwise, a q function returns the value of its last expression. Since functions are lists of expressions separated by semicolons, both of the following functions return 3:

q)single_expression: {3}
q)single_expression[] 
3
q)last_expression: {1; 2; 3}
q)last_expression[] 
3
q)

To return a value other than that of the last expression, use : (return):

q)explicit_return: {: 3; 4}
q)explicit_return[] 
3
q)

The last expression in explicit_return (i.e., 4) is never reached.

Meanwhile, if a function that does not return via : (return) (or ‘ (signal)) does not have a final expression (i.e, no expression after its last semicolon), that function returns a generic null:

q)no_return: {3; }
q)no_return[] 
q)
q)null no_return[]
1b
q)type no_return[]
101h
q)no_return[] ~ (::)
1b
q)

Lastly, functions can exit in two other ways:

  1. Calling the exit function:
q)die: {exit 0}
q)die[] 
$

(By the way, you can add code to run automatically at exit using .z.exit.)

  1. Signaling an error:
q)signal: {‘ “oops”}
q)signal[] ‘oops
q)

See this related faq for more information on the use of ‘ (signal)