Procedures and functions are distinct objects, although they are defined in about the same way. The difference is used to perform error-checking at runtime: a procedure must not return a value, but a function has to.
The syntax for function and procedure definitions is:
function: func NAME() stmt procedure: proc NAME() stmt
In this syntax, func and proc are keyword and must be
entered literally. NAME stand for the name of the function or
procedure, respectively. stmt is meant in the sense of the
previous section.
When calling functions and procedures, arguments separated by commas
can be passed in brackets. These are referenced inside the function or
procedure just like in e.g. the bourne shell: $3 is used to
reference the third argument. Argument counting starts with 1.
Arguments are passed as values (so arbitrary arithmetic expressions are possible) and are inside the function or procedure semantically equivalent to variables.
To reference an argument with number greater than the number of
arguments actually passed, is considered a runtime-error. This makes
it possible to construct functions and procedures which accept a
variable number of arguments, if a leading argument determines the
number of arguments to follow (just like printf()).
Functions and procedures can be used recursively (with a finite depth of the calling stack, however).
As an example, the following defines a recursive implementation of the factorial function in terms of the hoc language:
func fac()
{
if ($1 <= 0)
return 1;
else
return $1 * fac($1-1);
}
There are four predefined procedures in YODA:
stop is pressed or if EOF is encountered.