What Am I Missing (HP-71B)
11-03-2014, 04:53 AM
Post: #1
 Les Bell Member Posts: 188 Joined: Dec 2013
What Am I Missing (HP-71B)
I was messing around with some numbers this morning, preparing to explain to some not-very-technical students what a vector dot product is. Initially I grabbed my trusty 41CX, but then I thought this would be a chance to explore the 71. OK, there's no R->P conversion, but there's ANGLE(x,y) and that gets the job done just fine.

Then I thought, OK, let's write a user-definable function to calculate the dot product. First, I'll set up a couple of arrays as my 2D vectors (this is only to be a simple example!):

Code:
DIM A(1) @ DIM B(1) A(0) = 4 @ A(1) = 4 @ B(0) = 5 @ B(1) = 4

But I ran into trouble when trying to define a function for the dot product. I thought the syntax would be something like this:

Code:
DEF FND(A,B) = A(0) * B(0) + A(1) * B(1)

I get an "Illegal context" error message immediately on entering that line. Figuring that it might be the way I'm trying to pass two array variables as parameters, I thought I'd backtrack to a simpler example:

Code:
DEF FNA(A) = A * A

But no - even that gives the "Illegal context" error, yet it looks just like the examples in the User's Guide, etc.

So, what am I doing wrong?

And for the more complex dot product UDF, I expect I have to dimension A and B somehow, but I can't see any examples of how this is done for formal parameters in the manuals.

Grateful for any help! (Meanwhile, I'll do the whiteboard work on dot products using the 32SII!)

--- Les
[http://www.lesbell.com.au]
11-03-2014, 06:08 AM (This post was last modified: 11-03-2014 06:10 AM by Gerson W. Barbosa.)
Post: #2
 Gerson W. Barbosa Senior Member Posts: 1,199 Joined: Dec 2013
RE: What Am I Missing (HP-71B)
10 OPTION BASE 0
20 DIM A(1), B(1)
30 MAT INPUT A,B
40 DISP DOT(A,B)

Or

40 DISP A(0)*B(0)+A(1)*B(1)

if no Math ROM.

11-03-2014, 10:45 AM
Post: #3
 Les Bell Member Posts: 188 Joined: Dec 2013
RE: What Am I Missing (HP-71B)
(11-03-2014 06:08 AM)Gerson W. Barbosa Wrote:  10 OPTION BASE 0
20 DIM A(1), B(1)
30 MAT INPUT A,B
40 DISP DOT(A,B)

Or

40 DISP A(0)*B(0)+A(1)*B(1)

if no Math ROM.

Oh, a Math ROM would be bliss, but I don't have one (would need it for the MAT INPUT A,B, too, I think?).

Yes, the real problem is the "Illegal context" error messages on what looks like just about the simplest DEF FN examples. I must be overlooking something very obvious, but I can't think what. . .

--- Les
[http://www.lesbell.com.au]
11-03-2014, 01:41 PM
Post: #4
 cruff Member Posts: 157 Joined: Dec 2013
RE: What Am I Missing (HP-71B)
(11-03-2014 10:45 AM)Les Bell Wrote:  Yes, the real problem is the "Illegal context" error messages on what looks like just about the simplest DEF FN examples. I must be overlooking something very obvious, but I can't think what. . .

The manuals and some testing suggest that a DEF FN can not accept an array as a parameter because it says all parameters are passed by value. Later on, the documentation for subprograms mentions array parameters can be passed by reference, but would require an output parameter: CALL DOT(A,B,D)
11-03-2014, 02:03 PM
Post: #5
 Sylvain Cote Senior Member Posts: 1,126 Joined: Dec 2013
RE: What Am I Missing (HP-71B)
(11-03-2014 10:45 AM)Les Bell Wrote:  Yes, the real problem is the "Illegal context" error messages on what looks like just about the simplest DEF FN examples. I must be overlooking something very obvious, but I can't think what. . .

I have entered your example and I do not get the illegal context message but I do get a parameter mismatch thought.

I did some reading and experimentations, this is my finding ...
- FN parameters are passed by values
- FN can access global variables
- FN local variables mask global variables
- FN parameters only accept string and simple numeric variables (no numeric array)

so the following works

10 OPTION BASE 0
20 DIM A(1), B(1)
30 A(0)=4 @ A(1)=4 @ B(0)=5 @ B(1)=4
40 DISP FND;FNE(0,1);FNF(A(0),B(0),A(1),B(1))
100 DEF FND=A(0)*B(0)+A(1)*B(1)
110 DEF FNE(I1,I2)=A(I1)*B(I1)+A(I2)*B(I2)
110 DEF FNF(V1,V2,V3,V4)=V1*V2+V3*V4

If I find a way to pass an array I will post it.

Best regards,

Sylvain
11-03-2014, 03:20 PM
Post: #6
 Sylvain Cote Senior Member Posts: 1,126 Joined: Dec 2013
RE: What Am I Missing (HP-71B)
(11-03-2014 02:03 PM)Sylvain Cote Wrote:  If I find a way to pass an array I will post it.

(11-03-2014 01:41 PM)cruff Wrote:  The manuals and some testing suggest that a DEF FN can not accept an array as a parameter because it says all parameters are passed by value. Later on, the documentation for subprograms mentions array parameters can be passed by reference, but would require an output parameter: CALL DOT(A,B,D)

cruff was faster than me and he is right.

The following program should do what you where searching for ...

10 OPTION BASE 0
20 DIM A(1), B(1)
30 A(0)=4 @ A(1)=4 @ B(0)=5 @ B(1)=4
40 R=0 @ CALL SBD(R,A,B) @ DISP R
200 SUB SBD(Z,X(),Y()) @ Z=X(0)*Y(0)+X(1)*Y(1) @ END SUB

Sylvain
11-03-2014, 10:21 PM
Post: #7
 Les Bell Member Posts: 188 Joined: Dec 2013
RE: What Am I Missing (HP-71B)
(11-03-2014 03:20 PM)Sylvain Cote Wrote:  cruff was faster than me and he is right.

The following program should do what you where searching for ...

10 OPTION BASE 0
20 DIM A(1), B(1)
30 A(0)=4 @ A(1)=4 @ B(0)=5 @ B(1)=4
40 R=0 @ CALL SBD(R,A,B) @ DISP R
200 SUB SBD(Z,X(),Y()) @ Z=X(0)*Y(0)+X(1)*Y(1) @ END SUB

Sylvain

Thanks, both cruff and Sylvain; you're right, that does exactly what I want.

The two things I'd missed were:

1) The need to pass arrays by reference, and the consequent need to use a subprogram with an output parameter, and

2) The fact that DEF FN doesn't allow keyboard execution, only use as a program statement (that's why my simple non-array example was failing).

Interestingly the manual says, "A function can be executed only when it is referenced in a numeric or string expression, either in a program or from the keyboard." (p. 218) which, along with the examples at the bottom of that page, to believe that a UDF could be defined from the keyboard - but it can't, only executed. (Which leads me to a question I'm going to have to explore further - does the UDF exist only within the scope of the program that defines it? A collection of UDF's that are executable in CALC mode would be handy, but I guess that's not how it works.)

Thanks again - and btw, I did that little lecture on dot products using a 32SII, which fit in my jacket pocket a bit more neatly than the 71, anyway.

--- Les
[http://www.lesbell.com.au]
11-03-2014, 11:51 PM
Post: #8
 Sylvain Cote Senior Member Posts: 1,126 Joined: Dec 2013
RE: What Am I Missing (HP-71B)
(11-03-2014 10:21 PM)Les Bell Wrote:  1) The need to pass arrays by reference, and the consequent need to use a subprogram with an output parameter, and

In my example all variables where passed by reference.

Here is another example ...

10 R1=0 @ CALL SBT(R1) ! pass by ref
20 R2=0 @ CALL SBT((R2)) ! pass by val
30 DISP R1;R2 ! shows 10 0
40 SUB SBT(Z) @ Z=10 @ END SUB

(11-03-2014 10:21 PM)Les Bell Wrote:  2) The fact that DEF FN doesn't allow keyboard execution, only use as a program statement (that's why my simple non-array example was failing).
... does the UDF exist only within the scope of the program that defines it?

No UDF are local and can only be called inside the program file they reside.

Sylvain
11-04-2014, 01:01 AM
Post: #9
 Les Bell Member Posts: 188 Joined: Dec 2013
RE: What Am I Missing (HP-71B)
(11-03-2014 11:51 PM)Sylvain Cote Wrote:
(11-03-2014 10:21 PM)Les Bell Wrote:  1) The need to pass arrays by reference, and the consequent need to use a subprogram with an output parameter, and

In my example all variables where passed by reference.
Yes, I spotted that - what I meant was that, because arrays can only be passed by reference (p.206 of the Owner's Manual), and a user-defined function can only take simple variables as arguments (bottom of p. 218), it's necessary to use a subprogram, rather than a UDF. Trying to enter a UDF with an array as a parameter gives an "Invalid var" error. I'm not sure why a UDF is different from a subprogram in this respect, but I can live with it.
(11-03-2014 11:51 PM)Sylvain Cote Wrote:  Here is another example ...

10 R1=0 @ CALL SBT(R1) ! pass by ref
20 R2=0 @ CALL SBT((R2)) ! pass by val
30 DISP R1;R2 ! shows 10 0
40 SUB SBT(Z) @ Z=10 @ END SUB
Nice little demo - thanks!
(11-03-2014 11:51 PM)Sylvain Cote Wrote:
(11-03-2014 10:21 PM)Les Bell Wrote:  2) The fact that DEF FN doesn't allow keyboard execution, only use as a program statement (that's why my simple non-array example was failing).
... does the UDF exist only within the scope of the program that defines it?
No UDF are local and can only be called inside the program file they reside.

That's what I figured; it would nice to have UDF's that were callable in CALC mode, but then, it would nice to have multi-letter variable and function names, so one could have more UDF's. Sigh. I guess what I'm saying is: I would love to have a Python-powered 71. . .

Thanks for your help with this!

--- Les
[http://www.lesbell.com.au]
11-05-2014, 05:25 AM
Post: #10
 Joe Horn Senior Member Posts: 1,479 Joined: Dec 2013
RE: What Am I Missing (HP-71B)
(11-03-2014 10:21 PM)Les Bell Wrote:  A collection of UDF's that are executable in CALC mode would be handy, but I guess that's not how it works.

(11-04-2014 01:01 AM)Les Bell Wrote:  ... it would nice to have UDF's that were callable in CALC mode...

UDF's can be used in CALC mode as long as their defining program is the current program (the one displayed when you execute CAT).

Example:
EDIT FOOBY
10 DEF FNQ(X)=X^2
CALC
FNQ(5)+1 ENDLINE --> 26
CALC
PURGE

<0|ɸ|0>
-Joe-
11-05-2014, 07:24 AM
Post: #11
 Les Bell Member Posts: 188 Joined: Dec 2013
RE: What Am I Missing (HP-71B)
(11-05-2014 05:25 AM)Joe Horn Wrote:  UDF's can be used in CALC mode as long as their defining program is the current program (the one displayed when you execute CAT).

Aha! Thanks for that, Joe! And I notice that the program doesn't even have to be run once to execute the DEF FN - it just gets found, which seems slightly counter-intuitive to me. But then, BASIC is an interpreted language.

So it looks like a file full of useful functions for CALC mode is the way to go.

--- Les
[http://www.lesbell.com.au]
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)