Next: Input- and Output Functions
Up: The Hoc Language
Previous: Statements and Control Structures
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:
- init():
- init() is called exactly once if the task is loaded.
- analyze():
- This task is called each time an event matches a
selector.
- finish():
- It is called every time YODA stops, i.e. if
stop
is pressed or if EOF is encountered.
- refresh():
- This task is associated with the display and NOT
called in batch mode. Using the internal display of YODA, refresh()
is called just before each timer triggered update of the display and
if YODA stops. By default (see -z option), this is every 15 seconds.
Table 5.5:
List of Functions for Input and Output of Arrays to Files
Function |
Purpose |
writevec(arrayName,dim,filename) |
write array of name
arrayName and dimension dim to ASCII file with name
filename . |
dumpvec(arrayName,dim,filename) |
write array of name
arrayName and dimension dim to binary file with name
filename . |
dumpvecShort(arrayName,dim,filename) |
same as
dumpvec(...) except that double numbers are converted to short
integers before writing (this saves disk space when applicable). |
readvec(arrayName,dim,filename) |
read ASCII file named
filename and fill numbers into the array of name
arrayName and dimension dim . Lines beginning with the
'#' character are considered to be comments. |
sourcevec(arrayName,dim,filename) |
read binary file named
filename and fill numbers into the array of name
arrayName and dimension dim . |
writetext(textSelector,text_length, |
writes
text event selected by textSelector |
filename,filemode) |
to file
filename . The mode may be "w" for a new file or
"a" for append. example: select TEXT ; |
|
writetext(TEXT,TEXT_LENGTH,"test","w"); |
|
Next: Input- and Output Functions
Up: The Hoc Language
Previous: Statements and Control Structures
Heiko Rohdjess
2001-07-19