BASIC 2 local variables

Discuss all aspects of programming here. From 8-bit through to modern architectures.
Post Reply
User avatar
Lardo Boffin
Posts: 1211
Joined: Thu Aug 06, 2015 6:47 am
Contact:

BASIC 2 local variables

Post by Lardo Boffin » Wed Mar 28, 2018 8:30 pm

Hello all

The user guide seems to recommend using Local variables in procedures. I was wondering what the overhead of this is?
Am I better off being careful about using shared global variables or creating local ones? I assume when creating local variables BASIC has to create them and then drop them at the end of the function? This seems like a fair amount of work.

Thanks

Lardo
BBC model B 32k issue 4, 16k sideways RAM, Watford 12 ROM board, Viglen twin 40/80 5.25" discs, Acorn 6502 coproc
BBC model B 32k issue 7, turboMMC, Opus Challenger 3 512k, Pi 3 coproc
BBC Master, Datacentre + HDD, pi co-proc

crj
Posts: 834
Joined: Thu May 02, 2013 4:58 pm
Contact:

Re: BASIC 2 local variables

Post by crj » Wed Mar 28, 2018 8:52 pm

I don't have any numbers for you, nor do I know Basic's innards.

However, for a dynamically scoped language, normal practice when creating a local variable would be to put a record of the variable's previous value on the call stack and then reset it to be blank. When you return from the procedure or function, the stack is unwound, reverting the values of the formerly-local variables.

So on the one hand it does a little extra work when you create or destroy a local variable. On the other, there is no overhead whatever for using a local variable. Indeed, since BBC Basic stores its variables (apart from the single-letter integer variables) as one linked list for each initial letter, having fewer variables will make your program run faster.

With LOCAL commands slowing things down and the smaller pool of variables speeding things up, I've no idea which one typically wins. (-8

User avatar
Lardo Boffin
Posts: 1211
Joined: Thu Aug 06, 2015 6:47 am
Contact:

Re: BASIC 2 local variables

Post by Lardo Boffin » Thu Mar 29, 2018 8:15 am

Thanks.

I assume based on this that a Local that does not exist globally is better avoided.
BBC model B 32k issue 4, 16k sideways RAM, Watford 12 ROM board, Viglen twin 40/80 5.25" discs, Acorn 6502 coproc
BBC model B 32k issue 7, turboMMC, Opus Challenger 3 512k, Pi 3 coproc
BBC Master, Datacentre + HDD, pi co-proc

crj
Posts: 834
Joined: Thu May 02, 2013 4:58 pm
Contact:

Re: BASIC 2 local variables

Post by crj » Thu Mar 29, 2018 2:53 pm

I grew up with BBC BASIC, but have since spent decades programming in proper high-level languages. Given all the good habits I've learned, in any but the simplest programs I'd be tempted to make everything LOCAL that possibly could be LOCAL, just to make it clearer what's going on!

There's not much problem with declaring a variable LOCAL that you don't use outside the function. The one wrinkle is that it will create the global variable with a default value (0, 0.0 or ""). But when you think about it, that's really no worse than having a global variable that you only use in that function, anyway.

Post Reply