Page 1 of 2

Why is this in error?

Posted: Thu Aug 15, 2019 12:33 am
by llewelyn
LET x= (10,5)

I thought that the comma would maybe perform a math function? If not then why couldn't x equal a screen position I will later call.

The ? cursor flashes right after the 0 and before the comma so why doesnt it like the zero?

Re: Why is this in error?

Posted: Thu Aug 15, 2019 9:00 am
by Ralf
You cannot assign a pair of values to a variable. Variables can hold only a single number. So you need two variables, X and Y.

LET X=10
LET Y=5

(Actually in modern programming languages you have "objects" and "structures" and lot of different data types so something
similar to your goal would be possible. But I assume we talk about Spectrum Basic here).

Re: Why is this in error?

Posted: Thu Aug 15, 2019 9:15 am
by Ast A. Moore
Did you, by any chance, mean this?

Code: Select all

LET x= ATTR (10,5)
This will return the attribute value at that screen position.

Re: Why is this in error?

Posted: Thu Aug 15, 2019 10:05 am
by llewelyn
Thanks Ralf.
Ast.A.Moore I copied that line from an example of code Scottie put up at WoS. As per this:-
let res=AddThenSquare (10,5)

Then I tried to work out what it did

Best guess is it says ' Let the result equal Add then square 10 plus 5'
So that would be 10+5=15
15x15=225


Finally I tried entering it into Fuse and got that error which baffled me.

Re: Why is this in error?

Posted: Thu Aug 15, 2019 10:28 am
by Ast A. Moore
llewelyn wrote:
Thu Aug 15, 2019 10:05 am
I copied that line from an example of code Scottie put up at WoS. As per this:-
let res=AddThenSquare (10,5)

Then I tried to work out what it did

Best guess is it says ' Let the result equal Add then square 10 plus 5'
So that would be 10+5=15
15x15=225


Finally I tried entering it into Fuse and got that error which baffled me.
Wait, I’m confused. Scottie gave an example in JavaScript and you’re trying to type it into the Sinclair BASIC interpreter? The syntax is completely different. I mean, you’ll need to either define a function, or spell it out right in the LET statement:

Code: Select all

LET x=(a+b)^2

Re: Why is this in error?

Posted: Thu Aug 15, 2019 10:36 am
by llewelyn
I know it was a stupid thing to do. I did it out of desperation because I wondered if the comma had some weird math function I was unaware of. Yes I knew it was JavaScript that Scotties example was in but still the math should be the same whatever the language?

Re: Why is this in error?

Posted: Thu Aug 15, 2019 10:47 am
by Ast A. Moore
llewelyn wrote:
Thu Aug 15, 2019 10:36 am
but still the math should be the same whatever the language?
Well, it is. The syntax and structure, however, are completely different. He defined a new function and called it addThenSquare. The syntax implies that the function take two arguments in parentheses, separated by a comma:

Code: Select all

let res = addThenSquare(10,5)
This essentially means: Take the arguments in parentheses, pass them to the function addThenSquare, and pass the result to variable res.

The function itself is defined like this:

Code: Select all

function addThenSquare(n1, n2){
    let result = n1 + n2;
     result = result * result;
     return result;
}
Now, you can do something similar in Sinclair BASIC with the DEF FN statement and then reuse it within your code. However, the most straightforward approach would be the one I suggested above. I can rewrite it using the same argument names as in Scottie’s example:

Code: Select all

LET res=(n1+n2)^2
If you want to use DEF FN, then you could write it like this:

Code: Select all

10 DEF FN a(x,y)=(x+y)^2
20 LET res= FN a(10,5)
30 PRINT res
In fact, you can use FN directly with the PRINT statement:

Code: Select all

10 DEF FN a(x,y)=(x+y)^2
20 PRINT FN a(10,5)
Make sense?

Re: Why is this in error?

Posted: Thu Aug 15, 2019 11:34 am
by llewelyn
You are a good man Mr Moore for bothering to explain things in detail so that I can understand them and I really appreciate it. Thank you.

I must find some way to save and index all the advice people so kindly dispense for my benefit because I'm feeling like a bit of Basic might not go amiss, if only to keep my brain from seizing up! Some of these tips are absolutely invaluable.

The above sincere thanks applies to everyone else who has offered advice & encouragement.

Re: Why is this in error?

Posted: Fri Aug 16, 2019 3:44 pm
by 1024MAK
The BASIC programming language likes things to be built from simple building blocks (*).

Mark

* Note, but more human friendly than assembly language / machine code.

Re: Why is this in error?

Posted: Fri Aug 16, 2019 6:10 pm
by Joefish
When you get into the habit of forever writing:

x = my_function(a, b)

It's easy to overlook the fact that no-one seems to have written a language with - surely - the obvious syntax:

(x, y) = my_function(a, b, c, d)

Such that a function can return more than one value.

C has lots of ways around this issue (you can define 'x' as a structure (or template) made up of two variables, and the function can return a matching structure; or you can make one of the parameters a pointer to where your variable is stored in memory, so the function can manipulate it directly) but it doesn't have a simple syntax to express it like this.