[futurebasic] Re: [FB] Dimming vars

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : August 1999 : Group Archive : Group : All Groups

From: Rick Brown <rbrown@...>
Date: Sun, 01 Aug 1999 10:46:38 -0500

Richard Phillips wrote:

>     I wasn't aware that parameter variables should NOT be dimensioned.  I have
> always DIMmed them.  After reading Derek's message I just assumed that I'd have
> to go back over the ten zillion routines I've written and eliminate the lines
> that dimension parameter variables.
>
>     But then I remembered that when it comes to defining string variables, if a
> new string length is NOT defined using the "DIM" or "DEF LEN" instructions,
> then the most recently defined string length value is instead used.
>
>     So what happens, for example, if the previously allocated string is defined
> at seven bytes in length (8 with length byte), but I need my "not to be DIMmed"
> parameter string to occupy a full 255 bytes long (256 with length byte)?
>
>     Can I use the "DEF LEN" instruction to force a new length?  And if so,
> where do I place this instruction so that the compiler will "see" it BEFORE my
> parameter string is actually defined?
>

Regarding DIM'ing parameter variables:
When the compiler first encounters a variable, it uses that _first_ encounter to
decide what the variable's length & type should be.  If the first encounter is in
a parameter list, it uses that; the variable is "implicitly" DIM'ed in the
parameter list.  If you put the same (parameter) variable in a DIM statement
_after_ the LOCAL FN statement, the DIM statement has no effect:

LOCAL FN myTest(x$)
  DIM 13 x$     '<--- ignored, because x$ was already "implicitly" DIM'ed

On the other hand, if you put the (parameter) variable in a DIM statement _before_
the LOCAL FN statement, the compiler encounters the DIM statement first, and thus
uses that to decide the type & size:

LOCAL
DIM 13 x$     '<--- this time x$ will really be DIM'ed as 13
LOCAL FN myTest(x$)

In fact, this is the main reason why it's sometimes useful to DIM above the LOCAL
FN statement.

Regarding the "default length" of string variables you wrote this:

"...when it comes to defining string variables, if a
new string length is NOT defined using the "DIM" or "DEF LEN" instructions,
then the most recently defined string length value is instead used."

This is not quite true.  The rules are actually more like this:
1. If a string variable is first encountered in a statement other than DIM, then
its max length is determined by the most recent DEF LEN statement; or it defaults
to 255 if no DEF LEN has yet been encountered.

2. If a string variable is first encountered in a DIM statement, then the rules
are the same as in #1, _unless_ there's an explicit length parameter that precedes
that variable (anywhere!) within the _same_ DIM statement.

Example:

a$ = "Hello"
DEF LEN 35
DIM b$, 99 c$, d$
DIM e$
f$ = "Goodbye"

Max lengths of the above variables (assuming no prior DEF LEN):
a$: 255
b$: 35
c$: 99
d$: 99
e$: 35
f$: 35

- Rick