Skip to main content
1-Visitor
March 5, 2014
Solved

Static variables in M15

  • March 5, 2014
  • 3 replies
  • 7047 views

In MathCAD 15 programming are static variable available? That is if one subroutine is called from another programmed section can a variable in the subroutine be marked such that it remembers its value from the last time the subroutine was called?

I know this is probably a re-occurring question but the last answer could find was in 2006.

Thanks. Martin

Best answer by Werner_E

Unfortunately, no.

You would have to pass that value(s) as parameter.

3 replies

Werner_E25-Diamond IAnswer
25-Diamond I
March 5, 2014

Unfortunately, no.

You would have to pass that value(s) as parameter.

memorris1-VisitorAuthor
1-Visitor
March 5, 2014

Thanks Werner for your prompt reply. Static variables would make life so much easier.

19-Tanzanite
March 5, 2014

Maybe this will do what you need.

memorris1-VisitorAuthor
1-Visitor
March 6, 2014

Richard. Hi!

Great method and will work well for me.

Thanks

Martin

23-Emerald V
March 7, 2014

I don't know whether this works in Prime, but it works in M13 and M14 (haven't tested M15).

There is an undocumented feature of local function that we can make use of to act as a global memory. If an array is defined before a local function definition, then that function can manipulate the array and store values that get retained between successive calls to the function. This capability is particularly useful for generating sequences, such as the Fibonacci sequence or random number generators such as the Mersenne Twister.

The Fibonacci functions given below allow the user to generate successive Fibonacci numbers simply by calling the function setfib and retrieve the current list using the function getfib.

Stuart

25-Diamond I
March 7, 2014

I never heard of that undocumented feature but it sounds great.

The picture you inserted is not showing up.

Am I right that the return "value" of that kind of program must be one or a set of fuctions? So if I want to rewrite an existing function to accomodate that kind of global/static variable, I would have to wrap it and make it a local function of the outer wrap?

I am not sure if we are supposed to understand what exactly is going here. I am still wondering about the effect that deleting the factor 1 in the local get() function has. Either any subsequent call of set() has no effect (which seems not to be the case given the return value of set) or set() and get() are playing with different "global" vectors, or ???