Post Reply 
Global variable
06-27-2017, 04:51 AM
Post: #10
RE: Global variable
Hello,

- About system global variables in HP Prime (specific to this setup).
These variables are intended, and used, by users in home calculations. They often will have data in them which is of value to them. As a result, a "proper" program should NOT modify them (unless it is it's explicit purpose).

Now, if one is creating a program for his personal use without any intention to give it to others, he can do as much collateral damages as he wants.
Also, use of these variables, especially for a beginner programmer who is migrating from "home screen user" to "automation" and then "programming", make the learning of programming easier.
The availability of these variables also makes development of a quick and dirty program easy...

These reasons are why these variables are available in programs.


- About global variables in a program (in general, and here we are starting to get into programming philosophy)

Use of global variables in a program/function means that your function is not independent anymore but dependent on a global state. This makes your function much harder to test and debug. It also means that you can call the function twice with the same parameters and get a different result! (don't they say that the person doing the same thing twice and expecting a different result is crazy?)...
As a general rule, global should be avoided (if/when practical and possible).

Of course, we all know that this is not always practical, which is why global exists, however minimizing them is usually a good practice.

- About global variables in Prime.
PPL offers a LOT of different ways to handle variables (global or not)... Without even going into the CAS stuff... and you should always try to use the most adapted one...

Home and app system variables (like A-Z, L2, XMin....) These should be left alone except if you explicitly intend on modifying them.

Home and app user variables (HVars and AVars). HVars should also be left alone, however AVars are fair use for the app program as they are part of the app state of which the program belong to. Their explicit aim was to allow app program to maintain some state in between compilations.

Program globals
They come in 2 favors: exported and non exported. Both have the same life span (they get reset when the program source gets edited). Both are available from outside the program (with full qualification for the non exported ones). They should be your first choice when you need a program global

program locals
function parameters and variables defined by local.

As stated, the only thing missing is a pass by reference... Although you could do it using expr... but this is kludgy...

As I said above, lots of choices! but as a general rule, you should try to always use the most restrictive version as it is the least likely to cause troubles.

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Global variable - Powersoft - 06-25-2017, 06:56 PM
RE: Global variable - DrD - 06-25-2017, 07:46 PM
RE: Global variable - toml_12953 - 06-26-2017, 12:27 PM
RE: Global variable - DrD - 06-26-2017, 11:48 AM
RE: Global variable - Han - 06-26-2017, 03:09 PM
RE: Global variable - DrD - 06-26-2017, 04:02 PM
RE: Global variable - Han - 06-26-2017, 08:29 PM
RE: Global variable - DrD - 06-26-2017, 09:04 PM
RE: Global variable - cyrille de brébisson - 06-27-2017 04:51 AM
RE: Global variable - DrD - 06-27-2017, 11:32 AM



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