Definition of user functions
In OWBasic it is possible to define user functions.
User functions and procedures must be defined at the start of the program. The main program always begins after that.
A function definition begins with the procedure heading and ends with the keyword RETURN.
FUNC test A, B#, VAR C
<statements>
RETURN <value> |
The function heading begins with the keyword FUNC, followed by the name of the function and the parameter list. The type of the function is determined by the suffix of the function name as with variables if it is not the default type. The type can also be a pointer (FUNC then gets the pointer prefix &), but not a user-defined type.
The parameter list describes the parameters of the function.
Variables used within the procedure are local, i.e., outside of the procedure they are unknown.
It is possible to assign default values to the parameters; if the function is called without a default parameter, the compiler implicitly assigns the default value. Default values can also be pointers.
Example:
PROC clearscreen mode%=1
CLS mode
ENDP
PROC my_messagebox text$, caption$="", time#=0.0
msgbox text, caption, time
ENDP
PROC memory_size var size%=#NULL
IF &size THEN
FLASHSIZE size, dummy
ENDIF
ENDP |
If a parameter has a default value, the following parameters must also have a default value.
Functions calculate a value und return this value to the caller. They can be used as part of an expression. The value which a function returns, has to be given with the RETURN statement, which terminates the function definition.
The use of user functions is the same as for builtin functions. A user defined function is used also, if a builtin function with the same name exists.
If the return value of a function is not needed, the function can be called as procedure.
Example:
FUNC vlen x,y: ! length of 2d vector
PRINT "vlen called"
RETURN sqr(x*x+y*y)
x=5: y=7: ! These variables do not have to do anything with
! the function parameters.
print vlen(x-5,y-2): ! Example-Call
vlen(1,2): ! call as procedure |
|