Rich Talbot-Watkins wrote: ↑
Wed Feb 12, 2020 12:57 pm
Do you remember if the BBC BASIC specification required this to work, or whether it's just an undefined consequence of how BBC BASIC implements variable assignment (allocating a new variable and defaulting it to zero prior to evaluating the RHS)?
The 'specification' didn't go into that much detail, but it did imply the behaviour by calling for a good degree of compatibility with the Microsoft BASICs of the day. In those BASICs it was legitimate to write:
(where A wasn't previously defined) because all numeric variables initially 'exist', with a value of zero. There is nothing comparable to a 'No such variable' error; the very first line of a program could be:
which would print zero, not raise an error as it would in BBC BASIC. This behaviour has survived to the present day in languages like Liberty BASIC
in which all variables - even arrays with up to 10 elements - initially 'exist' but with values of zero or an empty string as appropriate.
In practice it's extremely useful to have a way of forcing a variable to exist if it doesn't, but not changing its value if it does (for example when releasing resources in a 'cleanup' routine). I tend to use the compound +=
operator, introduced in BBC BASIC V, for this:
Code: Select all
var% += 0
IF var% THEN PROCrelease(var%)