Prime: Scope of Variable and functions within programs - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: Prime: Scope of Variable and functions within programs (/thread-256077.html) |
Prime: Scope of Variable and functions within programs - Thomas Chrapkiewicz - 11-17-2013 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
Re: Prime: Scope of Variable and functions within programs - Han - 11-17-2013 Quote: You can make a "global" local variable by simply not exporting it. For example:
myvar; // no visible by users but remains resident in memory
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.
Re: Prime: Scope of Variable and functions within programs - Thomas Chrapkiewicz - 11-17-2013 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
Re: Prime: Scope of Variable and functions within programs - Han - 11-17-2013 Quote: 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) This older post might possibly be relevant
Edited: 17 Nov 2013, 7:31 p.m. after one or more responses were posted
Re: Prime: Scope of Variable and functions within programs - Thomas Chrapkiewicz - 11-17-2013 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
Re: Prime: Scope of Variable and functions within programs - Thomas Chrapkiewicz - 11-17-2013 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
Re: Prime: Scope of Variable and functions within programs - Thomas Chrapkiewicz - 11-18-2013 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
Re: Prime: Scope of Variable and functions within programs - Thomas Chrapkiewicz - 11-18-2013 ....but lower case 'x' and 'y' do not cause any issues. I suspect I have interference with some app. (?)
TomC
Re: Prime: Scope of Variable and functions within programs - Marcus von Cube, Germany - 11-18-2013 Thomas, X, Y and all other single letter uppercase names are reserved global real variables.
Re: Prime: Scope of Variable and functions within programs - Thomas Chrapkiewicz - 11-18-2013 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
|