Reducing Basic Program Size

Discuss all aspects of programming here. From 8-bit through to modern architectures.
User avatar
Richard Russell
Posts: 1096
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk

Re: Reducing Basic Program Size

Post by Richard Russell » Wed Feb 12, 2020 2:05 pm

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:

Code: Select all

      A = A + 1
(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:

Code: Select all

      PRINT A
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%)

User avatar
Rich Talbot-Watkins
Posts: 1578
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca

Re: Reducing Basic Program Size

Post by Rich Talbot-Watkins » Wed Feb 12, 2020 6:18 pm

I guess I have the C++ orthodoxy of "declare all variables before using them" too ingrained!

Post Reply