laszlo wrote:I tried with the command return (as in java), but got error.
This issue arises because of the inherent difference in the design of Basic as compared to some other languages like C, C++, Java, etc. In the latter group, all procedures are functions - there are no subroutines. To get the behavior of a subroutine in these languages you simply define the procedure to be of type
void meaning that it doesn't return anything. In Basic, a
Subroutine is used for procedures that don't return a value and a
Function is used for those that do.
As Mike pointed out, every function has an associated return value variable whose name is the same as the function name. The return value variable can be used anywhere that a
normal variable can be used. To set the return value, you simply assign to the return value variable. You can assign a value to the return value variable as many time as you wish with in the function; the last value assigned will be the value that is returned. If you don't assign a value to the return value variable, the return value will be undefined.
Generally speaking, the ZBasic compiler will generate a warning if it detects that there is at least one path through the function that fails to set the return value variable. It is recommended to unconditionally assign a default return value (which you determine) to the return value variable early in the function. Thereafter, the code will only need to assign a return value in cases where it is different than the default value.
As for functions returning an array, this capability is generally not implemented in languages unless they have automatic garbage collection - Basic, C and C++ do not, Java does. As Mike mentioned, ZBasic supports functions returning a one value of a fundamental type (Byte, Integer, Single, etc.). However, it also supports returning a single value of String and structure types as special cases.
As with many other languages (including C and C++), multiple return values and arrays are implemented by passing a reference to variables/arrays allowing the function to modify them.
It is possible to design a function in ZBasic to return an UnsignedInteger value that is the address of a block of allocated memory containing an array, effectively returning an array. The caller is responsible for deallocating the memory block when it is no longer needed. This is an advanced technique that must be used carefully and it is recommended only for situations in which there are no other viable solutions.