BBC BASIC for Windows
Programming >> BBC BASIC language >> Namespace of CALLed Modules
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1277724546

Namespace of CALLed Modules
Post by Michael Hutton on Jun 28th, 2010, 11:29am

I have read http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1235907869.

I was interested in the last comment made re LOCAL variables being visible GLOBALLY.

I am thinking that it would be a good thing to be able to execute code in a CALLed file (which doesn't have Sub-routines, although I note the possibility of including them) I would like to define LOCAL variables within the CALLED module which would then be deleted on exit (or RETURN), for example create LOCAL structures within the CALLed file which would then be unavailable to the main program.

Would this be possible without reverting to the "tricks" which have been posted in the previous thread? But if it wasn't, I could use the methods posted and define them in a DEF within the CALLed module, but would they be visible on the heap in the main program?

Michael
Re: Namespace of CALLed Modules
Post by admin on Jun 28th, 2010, 1:33pm

on Jun 28th, 2010, 11:29am, Michael Hutton wrote:
I would like to define LOCAL variables within the CALLED module which would then be deleted on exit (or RETURN), for example create LOCAL structures within the CALLed file which would then be unavailable to the main program.

There's a big difference between variables (which is what you first said) and structures (which you later said) in that respect. I don't know of any reliable way in which regular scalar variables can be 'deleted'; once they're on the heap they're there for good (or until a CLEAR statement). I'm excluding attempts to modify the heap contents directly of course!

Structures are another matter. Although, like variables, once created they remain on the heap indefinitely, when you return from a procedure or function in which a LOCAL structure was used that structure is not visible to the rest of the program. It won't even appear in the List Variables window.

So if your concern is visibility of the 'variables' then putting them in a LOCAL structure solves that; once the routine exits they become invisible (you would of course have to put the 'meat' of your CALLed module in a PROC or FN, which as you say may involve using the 'tricks' in that earlier thread). If your concern is more with memory usage, then you're stuck. As always, the heap grows inexorably and you can't shorten it again.

Richard.