Functions are the bread and butter of most languages.
Tabitha implements functions in the sense of a procedural language (e.g C
).
They have a privileged existence,
and are very distinct from data.
Functions can take arguments and return values,
though are freee to do niether of these things if they so choose.
A Tabitha function is simply a function defined wholly within Tabitha source code. Here is an example:
function foo (Int a, int b) -> Int {
a > b => { return a }
return b
}
This function is pure, in that any time you run it with the same arguments the r esult will always be the same. This should make the Haskell programmers h appy. Functions in Tabitha can have side-effects however.
attach external std
function bar (Int a) {
std::printLn("Enter a number: ")
Int b = std::readInt(10) # the argument `10` refers to the maximum
# number of digits out integer will have in decimal
b > a => {
std::printLn("Greater")
}
b < a => {
std::printLn("Lesser")
}
b == a => {
std::printLn("SNAP!")
}
}
Notice that bar
does not give a return type,
and that is because this function does not return a value.
It is not technically a useless function however,
since it has statements performing input and output.
This is an example of a side-effect, and makes the function impure.
Not all functions used in a Tbaitha program need to have been defined in Tabitha.
Indeed, Tabtha can call procedures defined in static and dynamic libraries
(often written in C
).
This allows us to write Tabitha bindings to useful libraries quite easily.
Tabitha calls functions defined elsewhere external functions, and they are declared like this:
external function internalName represents
externalName(ArgType1, ArgType2, ArgTypes3, . . .) -> RetType
Note that no newline character is necessary here,
nor does it affect the meaning of the declaration anyway.
This is just for the sake of formatting.
Tabitha allows differing internalName
and externalName
.
The reason for this,
is that often an external function will have a prefix or otherwise extended name to avoid namespace pollution.
For example, there is a function in OpenGL called glBufferData
.
The gl
prefix is there to distinguish the function from any other that might be called e.g. bufferData
.
In Tabitha, functions are referred according to their home slab,
so we can make the name snappier without risking such pollution.
As with Tbaitha functions, giving a return type is optional. The argument list is also completely optional.