## Why is this in error?

The place for codemasters or beginners to talk about programming any language for the Spectrum.
llewelyn
Dizzy
Posts: 94
Joined: Thu Feb 22, 2018 3:27 pm
Location: virginias eastern shore
Contact:

### Why is this in error?

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?
0 x

Ralf
Dynamite Dan
Posts: 1306
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

### Re: Why is this in error?

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).
1 x

Ast A. Moore
Dynamite Dan
Posts: 1216
Joined: Mon Nov 13, 2017 3:16 pm

### Re: Why is this in error?

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.
0 x
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.

llewelyn
Dizzy
Posts: 94
Joined: Thu Feb 22, 2018 3:27 pm
Location: virginias eastern shore
Contact:

### Re: Why is this in error?

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.
0 x

Ast A. Moore
Dynamite Dan
Posts: 1216
Joined: Mon Nov 13, 2017 3:16 pm

### Re: Why is this in error?

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``
0 x
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.

llewelyn
Dizzy
Posts: 94
Joined: Thu Feb 22, 2018 3:27 pm
Location: virginias eastern shore
Contact:

### Re: Why is this in error?

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?
0 x

Ast A. Moore
Dynamite Dan
Posts: 1216
Joined: Mon Nov 13, 2017 3:16 pm

### Re: Why is this in error?

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?
1 x
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.

llewelyn
Dizzy
Posts: 94
Joined: Thu Feb 22, 2018 3:27 pm
Location: virginias eastern shore
Contact:

### Re: Why is this in error?

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.
0 x

1024MAK
Manic Miner
Posts: 527
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

### Re: Why is this in error?

The BASIC programming language likes things to be built from simple building blocks (*).

Mark

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

Joefish
Manic Miner
Posts: 634
Joined: Tue Nov 14, 2017 10:26 am

### Re: Why is this in error?

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.
0 x