The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

Prime: Scope of Variable and functions within programs
Message #1 Posted by Thomas Chrapkiewicz on 17 Nov 2013, 9:26 a.m.

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
Message #2 Posted by Han on 17 Nov 2013, 11:47 a.m.,
in response to message #1 by Thomas Chrapkiewicz

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.

            
Re: Prime: Scope of Variable and functions within programs
Message #3 Posted by Thomas Chrapkiewicz on 17 Nov 2013, 11:56 a.m.,
in response to message #2 by Han

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
Message #4 Posted by Han on 17 Nov 2013, 12:31 p.m.,
in response to message #3 by Thomas Chrapkiewicz

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

                        
Re: Prime: Scope of Variable and functions within programs
Message #5 Posted by Thomas Chrapkiewicz on 17 Nov 2013, 1:17 p.m.,
in response to message #4 by Han

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
Message #6 Posted by Thomas Chrapkiewicz on 17 Nov 2013, 11:35 p.m.,
in response to message #4 by Han

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
Message #7 Posted by Thomas Chrapkiewicz on 18 Nov 2013, 1:30 p.m.,
in response to message #4 by Han

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
Message #8 Posted by Thomas Chrapkiewicz on 18 Nov 2013, 1:39 p.m.,
in response to message #7 by Thomas Chrapkiewicz

....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
Message #9 Posted by Marcus von Cube, Germany on 18 Nov 2013, 2:54 p.m.,
in response to message #8 by Thomas Chrapkiewicz

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

                                          
Re: Prime: Scope of Variable and functions within programs
Message #10 Posted by Thomas Chrapkiewicz on 18 Nov 2013, 3:39 p.m.,
in response to message #9 by Marcus von Cube, Germany

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


[ Return to Index | Top of Index ]

Go back to the main exhibit hall