UniBasic Tips & Hints
This page contains tips, hints and common gotchas in UniBasic.
If you have any you want added, please send them to ‘u2tech’ at ‘y7mail.com’.
Pass by Value
In UniData, if you call a subroutine and directly pass it a variable, any updates to that variable made in the program are reflected in the variable in the calling program.
A = 1
CALL AddOne( A )
If the subroutine does a
A += 1, then CRT will output 2.
Sometimes however, you want to ensure that the subroutine will not affect the value, even if someone changes the subroutine and makes a mistake.
You could assign the variable to a temporary variable and pass that. In this example, it is simple enough, but when subroutines have more parameters it can start to get messy. There is another solution however, which works on the same concept but means you do not need to explicitly declare the temporary variable. It goes like this:
A = 1
CALL AddOne( (A) )
Just to make it clearer, I’ll show you another example:
A = 1
B = 3
CALL MultiAdd( (A), (B) )
The key here is that the parentheses force the inside values to be ‘calculated’ first and held in a temporary location. This temporary location is what is passed to the subroutine, not the location of the inside variable. Of course, this one comes down to taste. If you use this, make sure everyone knows what its purpose is. You don’t want people removing the parentheses thinking they are pure excess.
Credit: Rex Gozar in Coding Standards, Section 5.7
Check EXECUTE return value.
When using EXECUTE to run a command, never assume that it has completed successfully. Using the RETURNING keyword, it is possible to retrieve whether there was an error. (Note ‘ON ERROR’ for EXECUTE is deprecated)
Unfortunately, there is no hard and fast rule for that the valued returned means. For the ECL Verbs in UniData you will find a list of all the error values and messages in the ERR.MSG file. IF you are executing a program, it will the value returned via the STOP statement. Essentially, this means you will need to work it out from trial and error along with some digging around.
Sooner or later, you will probably come across a line of similar to this:
Value = Amount + 0
You are possibly also confused by why anyone would want to add zero to a number (I know I was). The answer is quite simple: If you add a number to a non-number, the result is a number. This can be taken advantage of to automatically convert null values into zero values without additional lines of code. So, the above code is generally done to save having to type:
Value = Amount
IF NOT(NUM(Value)) THEN Value = 0
Nulls behaving badly
Null values may behave different from how you expect when used in dynamic arrays.
See my post for more information onwhen null values are not values