« What data structures does q provide out of the box? | Main | Is it possible to hold partitioned table and unpartitioned table named the same on a single kdb process? »

Does q pass-by-value or pass-by-reference?

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:

  • it is impossible for a callee to modify a caller's local variables, and
  • passing a large structure between functions is inexpensive until a callee modifies it.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (1)

but it is posible to pass a table by reference - using `t (t is a table)

June 2, 2011 | Registered Commentercyprus

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Textile formatting is allowed.