Post Reply 
Variables global to a program
12-22-2013, 03:56 AM (This post was last modified: 12-22-2013 02:40 PM by Han.)
Post: #7
RE: Variables global to a program
Yes, there are two types of global variables (and this does not include built-in system global variables). As Patrice noted, there are exported global variables, and non-exported global variables. In both cases, global variables cannot be deleted. However, the contents of exported global variables can be viewed (and reset) via the memory browser. But they cannot actually be purged (in the same sense as in the HP 50G). You can, at best, have your programs reset them but storing the number 0 into them upon the completion of your program(s). However, they will still take up at least the size of a real number. Exported global variables may also be altered by the user without ever using any program or subroutine that created them. Non-exported global variables are basically the same as exported global variables, but they are not visible nor accessible to the user.

The scope of a global variable lies only within the source file that created it. That is, any program/subroutines within the same source file that created the global variable can make use of that variable. However, programs/subroutines from a different source file cannot access non-exported global variables.

In order for a program/subroutine to make use of exported global variables created from a separate source file, it must be created AFTER the other source file has already be completed AND compiled (i.e. exited from the program editor without errors). When that happens, the exported global variable is, for all intents and purposes, no different from a built-in system global variable such as X. As long as the program you create doesn't also create its own global variable of the same name, then there is no issue. And if it does, then the state of the global variable is basically ambiguous (and depends on the last compiled source file).

When you create a new program, what you are creating is really a new project source file, in which many programs can actually be created, all sharing the same resources (global variables). In fact, one may create a "new program" with file name "MYPROGRAM" and yet have no programs with that name. MYPROGRAM is more like a project name, and whether there is a program MYPROGRAM() all depends on whether you create such a routine within the source file named MYPROGRAM.

File 1 saved as "PROG1" with content:


  VAR1:=VAR1 + 1;

  MSGBOX("This is prog3 running!");

File 2 saved as "PROG3" with content:


If you edit source file "PROG1" first, and then exit, then two programs are created: PROG2() and PROG3(). There is no program PROG1(). A global variable is also created: VAR1. This variable is now visible to all other programs created from this point on. Now, if you create source file "PROG3", then the program PROG4() can use VAR1 as if it were like the global variable X. However, if you reverse the creation order (i.e. create source file "PROG3" before source file "PROG1") then it will not work and you will get syntax errors.

If we now create a new file

File 3 saved as "MYPROG5" with content:

EXPORT VAR1="123";


Then we basically break PROG2(); because of the conflict in global variable names. Even worse, the behavior of these programs are dependent on which source file is compiled first.

This was an issue that was seen in the period table of elements that we saw a while back, prior to this new forum being created. Users of the English version were getting errors because that version compiled fine on the author's machine/emulator due to the presence of an exported routine of the same name in a different source file (which would not have been the case for English users).

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
Post Reply 

Messages In This Thread
Variables global to a program - Bob Frazee - 12-22-2013, 01:09 AM
RE: Variables global to a program - Han - 12-22-2013 03:56 AM
RE: Variables global to a program - Han - 12-22-2013, 04:43 AM
RE: Variables global to a program - Han - 12-22-2013, 02:06 PM
RE: Variables global to a program - Snorre - 12-22-2013, 11:56 AM
RE: Variables global to a program - Han - 12-22-2013, 03:52 PM

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