Page 1 of 1

Why is this in error?

Posted: Thu Aug 15, 2019 1: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 10: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 10: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 11: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 11:28 am
by Ast A. Moore
llewelyn wrote: Thu Aug 15, 2019 11: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 11: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 11:47 am
by Ast A. Moore
llewelyn wrote: Thu Aug 15, 2019 11: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 12:34 pm
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 4: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 7: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.

Re: Why is this in error?

Posted: Fri Aug 16, 2019 10:13 pm
by Zoran
Joefish wrote: Fri Aug 16, 2019 7:10 pm 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)
Python supports this syntax. The following programme:

Code: Select all

def my_function(a, b, c, d):
    return a + b, c + d

x, y = my_function(1, 2, 3, 4)

print(x)
print(y)
will give this output:
3
7
Although such a syntactic sugar is nice, you can also see in this example that you don't have to declare variables explicitly, with their types. Not a language for me. ;)

Re: Why is this in error?

Posted: Fri Aug 16, 2019 11:07 pm
by llewelyn
Oh, thats rather nice because for once I could follow it without needing someone to explain it to me! It's a little odd in terms of how the English language would state that but its understandable which is more than can be said for most languages. I've had Python recommended to me by several people including my son who's my guru, he was a Navy radar tech. He just wishes I'd let go of ZXBasic but now after all these years I just couldn't. Its so cute and cuddly!

Re: Why is this in error?

Posted: Sat Aug 17, 2019 6:51 am
by AndyC
Joefish wrote: Fri Aug 16, 2019 7:10 pm 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.
C# has had exactly that syntax for ages. The return type is a Tuple, but you can automatically deconstruct a Tuple into multiple fields. So if you have a function like:

(int Left, int Right) my_function(int a, int b)
{
return (b,a);
}

Then you can do

int x;
int y;
(x,y) = my_function(3,4)

Or even

var (x,y) = my_function(3,4)

To get the results into two variables x and y, or instead deal with the result as a whole thing, such as:

var t = my_function(3,4)
Console.Writeline($"Left value is {t.Left}, Right value is {t.Right}")

It's a super handy feature, as you can flit between the two representations as and when it makes sense.

Re: Why is this in error?

Posted: Sat Aug 17, 2019 7:31 am
by djnzx48
It's also available as a language feature in C++: https://en.cppreference.com/w/cpp/langu ... ed_binding

Re: Why is this in error?

Posted: Sat Aug 17, 2019 7:34 am
by 1024MAK
llewelyn wrote: Fri Aug 16, 2019 11:07 pm Oh, thats rather nice because for once I could follow it without needing someone to explain it to me! It's a little odd in terms of how the English language would state that but its understandable which is more than can be said for most languages. I've had Python recommended to me by several people including my son who's my guru, he was a Navy radar tech. He just wishes I'd let go of ZXBasic but now after all these years I just couldn't. Its so cute and cuddly!
One important consideration to think about is where technology was when BASIC was formed. Before microprocessors (like the 8080, 6502 and Z80) became available in quantity at relatively low prices, typically BASIC programs were run on mainframe or mini computers that had multiple users connected to them via ‘dumb’ text terminals.
Unless you had your own access, user BASIC programs would be submitted on punched cards, or on programming forms. Then they would be entered as part of a batch job. As an end user, you got a copy of the resulting line printer print out.

Then when BASIC was written for the common microprocessors (like the 8080, 6502 and Z80), because the cost of ROM and RAM was expensive, the size and complexity of the interpreter (language) had to be kept small.

The ZX80 only had a 4Kbyte ROM, the ZX81 had a 8Kbyte ROM and the BASIC ROM in a ZX Spectrum is a 16Kbyte ROM. And even then, the relevant programmer(s) that wrote the code for the BASIC that these machines use, used every trick they could think of to try to cram as much functionality in as they could.

BASIC was also based on even older computer languages.

So now hopefully you can see why there are lots of limitations in BASIC compared to more modern languages. Let alone compared to the (very inexact and vague) English language.

And to this day, we humans still manage to continue to debate what is the best way, and hence there are differences in languages, syntax and symbols even in English text and in mathematics. Just look at how many ways there are of writing the date...

Mark

Re: Why is this in error?

Posted: Thu Aug 22, 2019 2:26 pm
by llewelyn
So now hopefully you can see why there are lots of limitations in BASIC compared to more modern languages. Let alone compared to the (very inexact and vague) English language.

And to this day, we humans still manage to continue to debate what is the best way, and hence there are differences in languages, syntax and symbols even in English text and in mathematics. Just look at how many ways there are of writing the date...
Mark

I watched a very interesting lecture by 'Uncle' Bob Martin on structured programming, here's one of his, NOT the one I refer to:- https://www.youtube.com/watch?v=Z5p5r9FWrpU


I mention this because a point that he stressed was the dismal reality of there being so many programming languages whereas ideally, there should be one that covered all the bases. The reason I quoted your text is because in my opinion English (and therefore an ideal form of Basic) is a perfect example of a flexible language which you accuse of being 'inexact and vague'! Its probably the ONLY language in which gender, tense, verb/noun order don't really matter. There's a good reason why this is so because English is a pidgin itself. Which is why its easier to get ideas over to non-native speakers, or more likely vice versa.

I would suggest that the ideal computer language would have similar properties. I've been developing this argument for several years because I truly believe it has validity. Basic for all its sins and imperfections does at least have some of English's properties in that you can get something to work for you in several ways, some really clumsy, some very compact BUT the virtue lies in that flexibility. I'm going to address this in my blog at greater length but I have found some support for the concept which was encouraging.

Re: Why is this in error?

Posted: Thu Aug 22, 2019 3:33 pm
by Einar Saukas
Joefish wrote: Fri Aug 16, 2019 7:10 pm 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.
Python, Perl, Lua, Go, ...

Re: Why is this in error?

Posted: Thu Aug 22, 2019 3:55 pm
by Einar Saukas
djnzx48 wrote: Sat Aug 17, 2019 7:31 am It's also available as a language feature in C++: https://en.cppreference.com/w/cpp/langu ... ed_binding
Yes, but it's not really a natural language feature, more like a hack. It requires some extra "plumbing" to declare such methods in C++.

Re: Why is this in error?

Posted: Thu Aug 22, 2019 3:57 pm
by 1024MAK
The reason why I describe English as very inexact and vague, is because of it’s flexibility. There are many variations of meanings and words, and expressions just in the U.K., plus various more used around the world.

Humans also love to miss out, or add words to sentences. Or use slang or abbreviations.

And often, you need to have worked out the context of what has been said/written before it’s possible to work out the meaning.

Combined, this makes it rather hard work for a simple logical machine (which is all computers are, they are just really, really fast) to try to work out what a human actually means.

For example, what would a computer make of this expression “let’s see the colour of your money?”

Do you know what it means?

A programming language has to have clear rules and a clear syntax. And must be able to be decoded into logical instruction steps. All decisions ultimately come down to a binary choice (yes/no, on/off etc...).

And there is one big choice facing the people that design a computer language. Do you design it so that it is easier for the computer to translate to its native machine code (that the microprocessor can read directly, so on the ZX Spectrum, that’s Z80 machine code). This is generally called a low level language.

Or do you design it so that it is more human friendly (the requirement for BASIC), but which takes more time for the computer to decode before it can work out what machine code instructions are needed to preform the requested function (hence is part of the reason that BASIC is slow). This is generally called a high level language.

Mark