Prime: Scope of Variable and functions within programs



#11

So, I'm writing a Program (prog()) which has local variables (x,y) and local functions (fcn1(x) that I do not want exposed outside of the Program.

The local function requires variables that are local to the program - thus are 'global' within prog().

Defining the function first in the program code yields a syntax error as it is trying to access variables defined within the program.

Thanks in advance,

TomC


#12

Quote:
So, I'm writing a Program (prog()) which has local variables (x,y) and local functions (fcn1(x) that I do not want exposed outside of the Program.

The local function requires variables that are local to the program - thus are 'global' within prog().

Defining the function first in the program code yields a syntax error as it is trying to access variables defined within the program.

Thanks in advance,

TomC


You can make a "global" local variable by simply not exporting it. For example:

myvar; // no visible by users but remains resident in memory
EXPORT MyGlobalVar; // can be seen and used by users
PROG2(); // makes prog 2 visible to prog 1 -- see * --

EXPORT PROG1(x) // this program is visible due to EXPORT
BEGIN
.. prog 1 source; can use myvar within ..
.. can also use x as a local var ..
.. x gets deleted after prog1 completes execution ..
.. * may call prog 2 provided prog 2 was declared above ..
END

PROG2(y) // not visible to users
BEGIN
.. prog 2 source; can use myvar within ..
.. can also use y as a local var ..
.. cannot use the same value stored in x in prog 1 ..
.. but can create a new, different local variable x ..
END;

That said, is func1(x) a mathematical function? Or is it a procedural function (i.e. a "subprogram" or "subroutine")? The answer to this question will make a huge difference because it may mean the difference between having to learn the nuances of CAS programming as well.

Edited: 17 Nov 2013, 11:49 a.m.


#13

Hello Han:

Thank you for the prompt reply.

func1(x) is a simple mathematical function that uses variables that are declared as local within prog1(). This is what has caused issues - the fact that func1(x) uses variables that are local within prog1() and must have their value passed to func1(x) - and not necessarily x itself.

I will try your recommendations later today.

Cheers,

TomC


#14

Quote:
Hello Han:

Thank you for the prompt reply.

func1(x) is a simple mathematical function that uses variables that are declared as local within prog1(). This is what has caused issues - the fact that func1(x) uses variables that are local within prog1() and must have their value passed to func1(x) - and not necessarily x itself.

I will try your recommendations later today.

Cheers,

TomC


If func1(x) is a mathematical function, then you will also need to use the CAS command. If you wish to create a function f(x,y)=x^2-y^2 then you could have:

CAS("f(x,y):=x^2-y^2");

The reason for CAS() is because what you want to create is essentially a CAS object (a mathematical function of, say, 2 variables with which you would like to use later. However, local variables in a program are currently not recognized by CAS. And in fact, the x and y in the code snippet above are in fact not local variables. You can avoid any hassle with the CAS if you can turn your mathematical function into a procedural function:

FUNC1(x,y)
RETURN(x^2-y^2);
END;

This older post might possibly be relevant

Edited: 17 Nov 2013, 7:31 p.m. after one or more responses were posted


#15

Thank you again Han.

In my particular case now, func1() only modifies one (or more) of the variables local to prog1().

I'll test things out later.

Regards,

TomC

#16

My specific case is a bit simpler, but different -

The func1() does not have any variables passed to it nor any returned from it - it simply modifies one of the variables local within prog1().

TomC

#17

What I'm finding/learning, is the scope of functions and variables is not clearly defined (to me, anyway).

When one writes a function within the program environment, it can have variables and sub-functions that are global within that 'super'function, that if not 'EXPORTED', will not appear outside of this particular program.

This seems to be working fine, but seems to be sensitive to the variable names. eg a global variable called 'DIR' works just fine, but 'X' and 'Y' cause syntax errors.

TomC


#18

....but lower case 'x' and 'y' do not cause any issues.

I suspect I have interference with some app. (?)

TomC


#19

Thomas, X, Y and all other single letter uppercase names are reserved global real variables.


#20

Marcus:

Well, all I can say for myself is DUH!!!

In the heat of development, where I had used X and Y as local variables, everything seemed to be fine, but once I took them outside the top-level function, Syntax errors arose. Changing them to x and y and everything is fine.

This environment obviously requires a lot of care!

Thank you for the reminder (hit 'upside the head'!).

TomC


Possibly Related Threads...
Thread Author Replies Views Last Post
  Does the HP Prime really compiles the user programs? CompSystems 3 269 12-13-2013, 01:55 PM
Last Post: Mike Morrow
  hp-prime solver and variable name fabrice48 22 867 12-10-2013, 03:25 AM
Last Post: fabrice48
  HP Prime: Lists in programs Alberto Candel 7 423 12-04-2013, 02:16 AM
Last Post: Alberto Candel
  HP Prime: FLOOR, iPart , and their use in programs Alberto Candel 6 395 12-01-2013, 10:17 PM
Last Post: Alberto Candel
  HP Prime: matrices in programs, in need of help Alberto Candel 9 412 11-26-2013, 01:33 AM
Last Post: cyrille de Brébisson
  HP Prime: password protection for programs Davi Ribeiro de Oliveira 2 200 11-22-2013, 12:45 PM
Last Post: Geoff Quickfall
  Question about transfering programs to the HP Prime Namir 10 435 11-17-2013, 04:01 PM
Last Post: Namir
  [Download] PrimeComm: Alternative small utility to upload/download programs to the HP Prime Erwin Ried 6 361 11-17-2013, 10:18 AM
Last Post: Erwin Ried
  HP Prime: do not send programs and shuts down by pressing Apps Davi Ribeiro de Oliveira 1 188 11-12-2013, 11:05 AM
Last Post: Joseph Ec
  HP Prime - CAS functions in Spreadsheet App CR Haeger 6 351 11-11-2013, 12:37 AM
Last Post: Michael de Estrada

Forum Jump: