HP Forums

Full Version: How do you assign a global var in a CAS function?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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?
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.
(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
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.
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
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
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.
Thanks Parisse.....that clears it up.
-Donald
Reference URL's