HP Forums

Full Version: Bug? accessing LOCAL var in shared scope
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have been using LOCAL vars declared outside the programs, to get the equivalent of variables shared between routines in the same program, but not exported globally.

There is something strange going on whenever I try to use these variables as function arguments; they are not accessed at all. I always have to copy them to a local variable first. Example:

LOCAL foo=[[1,2],[3,4]];
EXPORT GlobalVarIgnored()

  LOCAL bar=foo;

I would expect both PRINT(colDim(foo)) and PRINT(colDim(bar)) to print out same number (2), but instead the output of the program is:
 Error: Bad Argument Value
What's going on? Is this a bug or have I missed something?

(Discovered in 6030; confirmed in 6940.)
Hello lenborje,

I think you shouldn't use LOCAL outside functions.
To my knowledge there are 3 types of variables:
a: LOCALs inside functions: only accessible and existent within the function.
b: global (in program context) but not EXPORTed: only accessible within the program, but keeping their value even if the program is left (a kind of private vars).
c: EXPORTed: accessible from everywhere and also keeping their value.

The first type has to be declared inside a function, the last two types have to be declared outside functions:

myFunction() BEGIN LOCAL a; END;

Type b variables are a nice feature to preserve some program state without polluting the variable menu and without having to fear the user or another program may accidently set them to an invalid value.

(sorry, English is not my mother tongue)
(12-03-2014 11:42 PM)Snorre Wrote: [ -> ]I think you shouldn't use LOCAL outside functions.

I get the same error even if I leave out the "LOCAL" on the declaration of "foo".

Your "global" variable kind is exactly what I'm trying to achieve, i.e. variables local to the program, accessible to all its routines, but not exported to other programs.
Sorry. My fault. You're right. Now it seems to me as a bug too.
Sorry to dig up an old thread.

This is an issue related to CAS commands and the separation of Home and CAS views. They (CAS commands) currently can only see local variables inside HPPPL (i.e. non-CAS) programs, not ones declared outside. Even EXPORTed variables don't always behave as expected (referencing an entry inside a matrix using square brackets, for example, comes to mind).
May not be relevant but should
Local foo equals ... =
Local foo assignment :=
Or are these equivalent?

The local in the global scope of the program is optional (it is the default, EXPORT is a must if you want to export however).
= and := in variable creation are absolutely equivalent.

so, the syntax is (if my memory on syntax writing is correct):
[LOCAL|EXPORT] name[[:]=expression][, name[[:]=expression]]+;


may I ask if you have some insight on my original question in this thread?

Best regards,

Yes, the problem, as explained to some extend by Han is that the CAS is not local or program aware.

To access a local (as is function local), or program global variable (exported or local), the CAS would need to know what program/function is running. Which it does not because the Home/Cas interconnection is not 'that great'.

The result is that you can access from the CAS a program exported variable (because the CAS can find it), although it is dangerous because if you have 2 programs that export a variable with the same name (remember, they are still 2 variables in this case), then the CAS might pick any of them (actually, the one from the first program in the list...) but can not find a function local or program non exported variable.

Now, in order to try to 'hide' the problem as much as possible for 'non advanced' programs, when you call a cas function from a HPPL function, if they are any function local variables in the arguements, they will be replaced by their values, but this does not happen with program globals (maybe it should)...

One of the issue is that HPPL never knows when the cas need symbolic data or value data when it is called, this makes it fairly hard to know/guess what the user wants to do.

Thank you for the explanation.

I must add that what confused me in Han's hint was that I did not understand what CAS had to do with it.

I understand now that colDim is a CAS function. This was not immediately apparent to me.

Are all functions with mixed case names CAS functions, or is there some other way to distinguish between CAS and "normal" functions?

As a general rule, lower case function are CAS function. UPPERCASE functions are home functions. When CAS and HOME share a function, to allow HOME/CAS compatibility, CAS will also use uppercase.
The above is mostly legacy from the 38 series times.

Home later on added more functions which are often Mixed case (because it's more modern and nicer).
As a general rule, HOME functions are NOT case sensitive TeXtOuT will be recognized. CAS does accept some functions in both UPPERCASE and lowercase form, but never using a random mixed case. But they are some exceptions :-)
Most Mixed case functions in home are in fact app functions, not 'main' functions.

Hope that this does help.

Well, at least I understand I must be more careful in order to avoid CAS functions.

(I thought I had to prefix them all with "CAS." when called from HOME.)

You learn something new every day.


the CAS.name syntax is only necessary if there is a conflict with a home function.
for example CAS.sin will call the CAS sin from home. sin would call the home sin.

But when a function exists only in cas, you can call it directly.

Hi Cyrille,

Thank you for your explanation about Home and CAS variables.
You write about; general rule, CAS/Home share functions, mixed cases, some exceptions, most mixed case functions, app/main functions, etc.

I have read more threads about Home/CAS functions/commands. I think it is still confusing, specially for me.
My question is; which of ALL functions are specially for Home, which are for CAS, which are the same for Home and CAS and what are the exceptions?

As an example:
SIN for Home and sin for CAS.
In the catalog you can only select SIN in uppercase and not in lowercase. My simple conclusion is that SIN in uppercase is a Home function, but you can also use it in CAS but you must type in lowercase and that is not selectable in the catalog.
Don't use a general rule but use a straight forward rule with no exceptions; everything in uppercase are Home functions and everything in lowercase are CAS functions.
So, implement in the catalog also sin in lowercase, do this for all other (shared) functions.
Do it consistently/logically, then you have never questions about which are Home and CAS functions (I hope).
Implement also other hidden functions in the catalog.

(11-26-2015 12:24 PM)Dirk.nl Wrote: [ -> ]As an example:
SIN for Home and sin for CAS.

The function used depends on the environment from where the catalog is opened. While in CAS, sin() is returned to the command line. If you insert π in the parentheses and hit ENTER you get SIN(π) in the history and 0 as the result.

The rewriting to uppercase looks a bit strange but the zero result proves that the CAS.sin function is called.
Hi Marcus,

When I select the catalog in CAS, I see SIN in uppercase. Conclusion, it's a Home function, but maybe yes or no, after selecting it, it will be copied in lowercase to the command line. New conclusion, if you are not be sure if it is also a CAS function, try to copy it to the command line!

I have used length() in a Home program, it was not copied in uppercase, but it works in my program! A lowercase CAS function that works well in a Home program without CAS. before the function!
I think that the difference (maybe not the good word for it) between Home/CAS is not consistent enough. Request to HP; make this more clearer!!
Reference URL's