From Kx Wiki
Jump to: navigation, search

1.2 Variables

In q, as in most languages that allow mutable state, a “variable” should properly be called an “assignable”. (See Mathematical Refresher above for a discussion). Nevertheless, a variable in q is a name together with associated storage that holds whatever value has been most recently assigned to the variable. As a consolation, at least q does not misuse ‘=’ for assignment, as do many languages; in q ‘=’ actually means “test for equality.” Declaring a variable and assigning its value are done in a single step with the operator (:) which is called “amend” and is read “is assigned” or “gets.” Here is how to create and assign variable a with integer value 42.


When you entered this in your q session, you noted that nothing is echoed to the console. In order to see that a has indeed been assigned, simply enter the variable.


A variable name must start with an alphabetic character, which can be followed by alpha, numeric or underscore.

Naming Style Recommendations

  1. Choose a name long enough to make the purpose of the entity evident, but no longer. The purpose of a name is to communicate to a reader of the code at another time— perhaps even you. Long names may not make code easier to read. For example, ‘checkDisk’ is clearer than ‘cd’ or ‘chk’ but is no less clear than ‘checkDiskForFileConsistency’.
  2. Use verbs for function names; use nouns for data.
  3. Be consistent in your use of abbreviations. Be mindful that even “obvious” abbreviations may be opaque to readers whose native language is different than yours.
  4. Be consistent in your use of capitalization, such as initial caps, camel casing, etc. Pick a style and stick to it.
  5. Use contexts for name spacing functions.
  6. Do not use names such as ‘int’, ‘float’ or other words that have meaning in q. While not reserved, they carry special meaning when used as arguments for certain q operators.
  7. Accomplished q programmers avoid using the underscore character in q names. If you insist on using underscore in names, do not use it as the last character. Expressions involving the built-in (_) operator and names with underscore will be difficult to read.

Prev: Starting q, Next: Whitespace

Reprinted with the author's permission from: q for Mortals Version 3, An Introduction to Q Programming by Jeffry A. Borror.

The book is available on Amazon. In the United Kingdom, it is available at Amazon UK.

©2015 Jeffry A. Borror/ q4m LLC

Personal tools