News:

Welcome to RetroCoders Community

Main Menu

The DEF FN statement

Started by CharlieJV, Jun 11, 2023, 06:21 PM

Previous topic - Next topic

CharlieJV

If a DEF FN function references a global variable, a BAM program will not work correctly if that global variable is declared (explicitly or implicitly) after the DEF FN statement.

For example, the following will not work correctly:

def fnb = 2 * xyz
xyz = 5
print fnb


The following will work correctly:

xyz = 5
def fnb = 2 * xyz
print fnb

In general: So any time a statement/function references an identifier, that identifier has to be previously declared.

The exception: assignment of a value to a variable.  The LET statement (explicit or implicit) also plays the role of identifier declaration before assignment of value when the identifier has not been previously declared.

ZXDunny

Is it strictly code-order or execution order? Can you gosub or somewhere that sets up the "xyz" after the def fn, but executes before the fn is def'd?

CharlieJV

#2
Quote from: ZXDunny on Jun 11, 2023, 06:32 PMIs it strictly code-order or execution order? Can you gosub or somewhere that sets up the "xyz" after the def fn, but executes before the fn is def'd?

Good question.  That is about code-order as the parsing does it's magic.

Essentially, when the parsing of the code gets to def fnb, it says "okay, xyz is at what spot in memory.  Oh it does not have a spot in memory (because nothing I've previously processed has declared this).  Then I shall treat this identifier as meaning the literal 0, and I shall continue trucking along."

CharlieJV

#3
Heads up when using DEFSTR, DEFINT etc. for default assignment of data types to DEF FN functions.

The datatype of a DEF FN function will only be impacted by a DEFSTR (or statement for the other dattypes) for the letter "F".

So the following will not work (the function will be assumed to return the default of SINGLE):

defstr c - d
def fncenter(w$)=space$((40-len(w$))/2)+w$
input word$
print fncenter(word$)

The following will work:

defstr c - f
def fncenter(w$)=space$((40-len(w$))/2)+w$
input word$
print fncenter(word$)


Side note about DEFSTR/etc.: As of this writing, BAM requires a letter range, and does not support a single letter.

EDIT: I figured out what I needed to change in wwwBASIC to allow DEFtype statements to define single letters instead of ranges.  This will be in the next version of BASIC Anywhere Machine.

ZXDunny

What an odd requirement :)

Why do you need the defstr at all for that function? It doesn't seem to use any of those letter-named variables.

CharlieJV

Quote from: ZXDunny on Jun 12, 2023, 12:00 AMWhat an odd requirement :)

Why do you need the defstr at all for that function? It doesn't seem to use any of those letter-named variables.

That's just some sample code I found in that book, and I decided to try it out for giggles.  I'm no fan of the DEFtype functions, but if I can't make BAM easily handle old code that people want to try out, I do want to point out things that some old BASIC implementations did a certain way that BAM will not do.

All in the hopes that old code can be made to run without too much reworking.