HP Prime Forum
HP Prime global variables - Printable Version

+- HP Prime Forum (http://forum.hp-prime.de)
+-- Forum: HP Prime (/forumdisplay.php?fid=1)
+--- Forum: Questions to HP (/forumdisplay.php?fid=3)
+--- Thread: HP Prime global variables (/showthread.php?tid=86)

HP Prime global variables - cdeaglejr - 20-03-2016 08:33 PM

I'm using the following format in the connectivity kit to define global variables;

EXPORT iap_flg, jdatei;

followed by the main program (for example)

EXPORT apmoon()

This seems to work fine when compiling and running within the connectivity kit but does not work on the physical calculator.

Does anyone known the proper technique for making global variables available to other subroutines?


RE: HP Prime global variables - brebisson - 21-03-2016 07:55 AM


First, the EXPORT statement is not necessary, unless you want the variable to be user accessible from the command line.

Normally, what you are doing there is perfectly OK and should work on the calculator.
What error are you getting?
Are you using CAS functions in your program?
Can you post a little bit more of your program?


RE: HP Prime global variables - cdeaglejr - 21-03-2016 04:41 PM


Thanks for the response.

I'm not using any CAS routines.

I'm getting Error: Syntax error pointing to the first line the uses a global constant. This is true for both the main program and any subroutines that use the global variables.

A typical example is the "geodetic coordinates" application posted in the Software area of this forum. I'm trying to define and pass the equatorial radius and flattening factor of the Earth to the "geodet1" subroutine. Again, syntax error in both the demo_geodet1 main program and geodet1 subroutine. I've also tried removing the EXPORT command from the global variables without success.

Works fine in the connectivity kit which is where I do the actual programming.

// global constants

// req = equatorial radius (kilometers)
// flat = flattening factor (non-dimensional)

EXPORT req := 6378.14, flat := 1.0/298.257;

RE: HP Prime global variables - brebisson - 22-03-2016 08:06 AM


Hard to see what is wrong without at least a good chunk of the program (at least the declartion and a sub-routine).

can you test this to see if it works:
EXPORT GlobalTest:= 123/45;
EXPORT test()
return GlobalTest;


RE: HP Prime global variables - cdeaglejr - 23-03-2016 09:06 PM

It appears a program for the HP Prime calculator must be "self-contained" in order to use global variables. That is, the main program and all support routines must be in a single source code file.

Here's a typical source code file that support global variables or constants. It contains two global constants, a main program and a single support subroutine.

// global constants

// req = equatorial radius (kilometers)
// flat = flattening factor (non-dimensional)

EXPORT req := 6378.14, flat := 1.0/298.257;

// support subroutine


// demo_geodet1

// this program demonstrates how to interact with
// the geodet1 function which computes geodetic
// altitude and latitude from geocentric radius
// and declination

// main program

EXPORT demo_geodet1()


// angular conversion factors

LOCAL rtd := 180.0 / pi, dtr := pi / 180.0;

LOCAL gdv := [0, 0];

LOCAL rmag, dec;

// geocentric radius (kilometers)

rmag := 8000;

// geocentric declination (radians)

dec := 45 * dtr;

// compute geodetic coordinates

gdv := geodet1(rmag, dec);

// print results



print(" ");

print("geocentric declination = " + dec * rtd);

print(" ");

print("geocentric radius = " + rmag + " kilometers");

print(" ");

print("geodetic latitude = " + gdv(1) * rtd + " degrees");

print(" ");

print("geodetic altitude = " + gdv(2) + " kilometers");



EXPORT geodet1(rmag, dec)

// geodetic latitude and altitude

// series solution

// input

// req = equatorial radius (kilometers)
// flat = flattening factor (non-dimensional)
// rmag = geocentric radius (kilometers)
// dec = geocentric declination (radians)
// (+north, -south; -pi/2 <= dec <= +pi/2)

// output

// alt = geodetic altitude (kilometers)
// lat = geodetic latitude (radians)
// (+north, -south; -pi/2 <= lat <= +pi/2)

// reference: NASA TN D-7522



LOCAL n, o, a, p, q, r, s;

LOCAL gdv := [0, 0];

n := req / rmag;
o := flat * flat;

a := 2.0 * dec;
p := sin(a);
q := cos(a);

a := 4.0 * dec;
r := sin(a);
s := cos(a);

// geodetic latitude (radians)

gdv(1) := dec + flat * n * p + o * n * r * (n - 0.25);

// geodetic altitude (kilometers)

gdv(2) := rmag + req * (flat * 0.5 * (1.0 - q) + o * (0.25 * n - 0.0625) * (1.0 - s) - 1.0);

return gdv;


RE: HP Prime global variables - brebisson - 24-03-2016 07:51 AM


You are partially correct.
As a general rule, the HP Prime programming language is designed so that ONE program 'file' contains ALL the related functions, user visible or no. One reason is that, by doing so, you are sure that all the functions that you need stay together (those of us who remember the HP38G time, where each file was 1 single function can probably remember the disaster that this was).

Now, this does not mean that it is a MUST, if a variable is exported, then it CAN be used in a program in another file, BUT you need to make sure that the program that does declare teh global variables is created and present when you build/compile the 2nd program.