BBC BASIC for Windows
Programming >> BBC BASIC language >> variables - using already defined http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1399089290 variables - using already defined
Post by larry on May 3rd, 2014, 03:54am
Ok so total newbie to the whole BBC basic. Just started studying it a few months ago in earnest. I wrote before in various other languages and now want to try this one out.
Looked at the character definition program out of the help and compiled it to exe. Then decided to make the suggested modifications. That's where it all went south. How is it that I can create a procedure that uses already declared variables and it says no such variable? Also, how do I make global assignments. I tried the obvious command global varname, but it did not color it in like it does on keywords so I guessed that that wasn't right.
There is a variable in the original program called Grid% which acts like an array but it is never declared as such. That is the variable giving me the error when I try to use it in my procedure. Any help received will be greatly appreciated, and once I've gotten my feet wet so to speak, I might be able to learn enough to help the next newbie out. Re: variables - using already defined
Post by rtr on May 3rd, 2014, 09:46am
Variables are global by default in BBC BASIC, so any variable that you have not explicitly declared as LOCAL or PRIVATE, and is not a formal parameter, is global (see below). Sometimes, for clarity, I add a REMark listing the global variables accessed by a function or procedure, but that's only for my benefit.
Quote:
There is a variable in the original program called Grid% which acts like an array but it is never declared as such.
It is. Here is the line in which it is declared (it's the very first line of the program after the initial comments):
Code:
DIM Grid%(8,8)
Because it's never declared as LOCAL or PRIVATE, and is not a formal parameter, it's a global array.
Quote:
That is the variable giving me the error when I try to use it in my procedure.
My best guess is that you've added a space between Grid% and the opening parenthesis, which BBC BASIC doesn't like. If it's not that you'll need to list some code illustrating the problem.
Richard.
Note: Variable scoping in BBC BASIC is similar to what it is in C, i.e. variables can be seen by any 'inner' function unless they are explicitly declared as local. However, being an interpreted language the scope is dynamic (determined at run time) rather than the static lexical scoping of C.