A function is the only other object type in Q-BAL, besides the queue. We will start with functions on numbers, as we did with queues of numbers. A function is composed of three elements: an input queue, an output queue, and an instruction queue.
A function is "called" (although that term is not usually used in Q-BAL) whenever an object is placed on its input queue. This is normally done by treating the function name as if it were a queue name and attaching something to it. When this occurs, a "sub-program-counter" is invoked and the instruction queue of the function activates.
A function on numbers is declared in the same way as a queue of numbers,
except with an
F instead of a
Q for the type, as
The same restrictions on the name of a function apply as on the name of a queue.
The instruction queue of a function is the only part that can be initialized. It is done as follows:
F square Q x *in -> x x \ in -> x x -> out F
Don't worry about what that function does; we'll get to that in the next section.
Any statement valid outside a function is valid inside a function, and
some others as well, with a few modifications. When the program counter is
referred to inside a function, it refers to the function's
sub-program-counter. When the predefined queues
out are referred to, they refer to the input and output queues
of the function. For this reason, one can attach to
in or read
out, although it is considered bad form in most cases. A
function can declare its own local variables, but they are reinitialized
every time the function executes (see advanced functions for how to prevent
The higher-level prefix operator, when used inside a function, forces the
name to refer to the object of the same name outside the function.
:; inside a function will refer to the original
program counter, while
:in will refer to stdin rather than the
function's input queue.
Whenever the name of a function is used on the source side of an expression it refers to the output queue, and is treated as if it were a normal queue. Whenever it is used as the destination, it refers to the input queue, and again is treated as a normal queue.