Functions |
There are essentially two types of functions in Proteus: library
functions and user defined functions (UDF).
In this manual, library functions are divided in categories according to their purpose;
they are the "bricks" with which the user can build new functions - as such,
they are by far faster than other functions that could be written in Proteus to accomplish
the same task.
The user can write UDFs to structure the code and to easily reuse pieces of code in many Proteus programs, by creating user libraries.
To call a function, write its name followed by an open parenthesis, without inserting blanks between them:
; Right STRDUP("string") ; Wrong STRDUP ("string")
Parameters in Proteus can be passed in three different ways: by value, by implicit reference or by explicit reference.
Passing by value is the most common and does not require any user intervention, being the default way.
Passing by implicit reference can be achieved by prefixing the character '@' to every variable name in the function definition:
FUNCTION MyFunc(@var1, var2, @var3)
var1 and var3 will be passed by reference; var2 is passed by value. When passing by implicit reference, the function is called as usual, without any special tricks; when the UDF ends, the value of the local variable is stored in the original parameter if it is a variable name (not a complex expression).
Passing by explicit reference, on the other part, does not require special tricks in the definition of the UDF, but requires to prefix the character '@' to every parameter that the user wants to pass by reference, for example in this way:
CONSOLELN MyFunc(@var1, var2, @var3)
Passing by implicit reference is useful when the parameters must always be passed by reference, thus avoiding to repeat many times the character '@' in the function calls; passing by explicit reference is most useful when parameters seldom needs to be updated (as it happens with most library functions).
Passing by value, explicit or implicit reference can be mixed without problems in the definition and in the function call.
In the following chapters, when a parameter can be passed by explicit reference it is
prefixed by the characters [@]; no library function admits passing by
implicit reference. The result of the function is stored inside the parameters passed by
reference, except when specified otherwise; if more than one parameter is prefixed by '@',
the result is stored in every parameter passed by reference.
Only variable names and calls to
function PUB can be passed by reference;
e.g. INC(@PUB(N)) is correct and
updates the public variable N with the result of the function INC;
function calls, string and numeric constants cannot be passed by
reference.
Even if passing by reference is not used, it is possibile to modify the value of a variable external to the UDF by using the method PSET (even though this is not reccomended, being a side-effect difficult to follow and understand).
A few library functions have a variable number of parameters; in the syntactical definition, optional parameters are enclosed between [brackets] and followed by two dots ("..").
Start of page | Next topic | Previous topic | Contents | Index |