Does q pass-by-value or pass-by-reference?
Tuesday, March 1, 2011 at 10:34PM
kdbfaq in Language, evaluation strategy, pass by reference, pass by value

Semantically, q is pass-by-value. However, q only copies values of structures (i.e., lists, dictionaries, or tables) if they are modified by the callee, so q often performs as well as pass-by-reference.

What do you do if you want to have the callee modify variables passed by the caller? As our reader cyprus points out, you can pass the name of a global variable:

modifier: {[variable_name]
    variable_name set 47;
    }

caller: {[]
    global:: "global";
    modifier `global;
    show global; // prints 47
    }

Whether this should be called pass-by-reference is the subject of many flame wars; Wikipedia's entry on Evaluation strategy provides a cogent, balanced explanation of the varying interpretations of that (and related) terms. What's important to know are the following:

Article originally appeared on online book - kdb+/q faq - Kdb+ is the registered trademark of Kx Systems, Inc. (http://www.kdbfaq.com/).
See website for complete article licensing information.