HP Forums
How do you assign a global var in a CAS function? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: How do you assign a global var in a CAS function? (/thread-9445.html)



How do you assign a global var in a CAS function? - webmasterpdx - 11-05-2017 11:18 PM

e.g., if you use EXPORT as below...

#cas
TEST(l):=
BEGIN
EXPORT MMM:=256;
RETURN l[3];
END;
#end

...it compiles OK, but when you try to run TEST(L1), it'll give a bad argument error.
However, if you comment out the EXPORT statement, it'll work fine.

How do you create a global variable in a CAS function if EXPORT doesn't work?

Thanks
-Donald


RE: How do you assign a global var in a CAS function? - webmasterpdx - 11-05-2017 11:27 PM

I did figure this out...

EXPORT MMM:=256;

#cas
TEST(l):=
BEGIN
RETURN MMM*l[3];
END
#end

...but MMM is a Home variable when done this way.
Let's say I want to create a global CAS list, which can be indexed using []. How do I do that from a CAS program?


RE: How do you assign a global var in a CAS function? - webmasterpdx - 11-05-2017 11:58 PM

Never mind! I think I got it figured....

If you do the following...

#cas
TEST(l):=
BEGIN
m:={5,4,3,2,1};
RETURN l[3]*m[2];
END
#end

m is now a global CAS variable of type list.

Also, if the program was a Home program (defined with EXPORT), then assigning m the same way (not declared as LOCAL), it would be a Home variable of type List...and global. i.e. EXPORT is only needed for global variable definition if outside a program definition.


RE: How do you assign a global var in a CAS function? - salvomic - 11-06-2017 08:20 PM

(11-05-2017 11:27 PM)webmasterpdx Wrote:  I did figure this out...

EXPORT MMM:=256;

#cas
TEST(l):=
BEGIN
RETURN MMM*l[3];
END
#end

...but MMM is a Home variable when done this way.
Let's say I want to create a global CAS list, which can be indexed using []. How do I do that from a CAS program?

Export is to use first of the #cas #end or BEGIN END; block, with default values, like:
Code:
export MMM:=[[0,0],[0,0]];

In another part of the program you must use STO (or sto) to store a value in the var exported, like:
Code:
sto({varrow, varcol}, MMM);
not return, that you can use for other purpose...
When you store something, you can also read the variable in another part of the program (or other program)...

Is this your goal?

Salvo


RE: How do you assign a global var in a CAS function? - webmasterpdx - 11-06-2017 08:33 PM

My goal was just figuring out when variables are cas vars vs home vars. e.g. just assigning a var in a cas program (that isn't declared as local) will make it global, and if assigned in a cas program, it will be a cas variable. If it's a list for example, you'll need to use [] to index it instead of (). i.e. It makes a difference if it's a cas vs a home variable, even if the same type.


RE: How do you assign a global var in a CAS function? - salvomic - 11-06-2017 10:01 PM

Donald,
probably you have already read this tutorial by Han.
There is a lot of things to know about CAS programming.
I hope you could find help in there.

Salvo


RE: How do you assign a global var in a CAS function? - webmasterpdx - 11-07-2017 06:47 AM

That article says that in CAS programs variable initialization must be done seperately from declaration.
...as in:

local c;
c:=5;

However, I just tried initialization on the same line as in:

local c:=5;

...and it worked.

So....is this still the case or is it OK to initialize in the same line as declaration with the latest firmware?
Let me know and I'll edit the article to remove that....

???
Thx
-Donald


RE: How do you assign a global var in a CAS function? - parisse - 11-07-2017 07:10 AM

You can assign variables in a local statement, but you must take care of priorities, for example
Code:
local a:=1,c:=3;
will assign a to 1,3 like
Code:
local a:=(1,c:=3);
unlike
Code:
local (a:=1),(c:=3);
That's why it is recommended to assign local variables outside of the local declaration.
In a CAS program, non declared variables are global, if they already exist in Home (i.e. you made an EXPORT before) then they are HOME variables, otherwise they are global CAS variables.


RE: How do you assign a global var in a CAS function? - webmasterpdx - 11-07-2017 07:44 AM

Thanks Parisse.....that clears it up.
-Donald