cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Static variables in M15

SOLVED
Highlighted
Newbie

Static variables in M15

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

Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Static variables in M15

Unfortunately, no.

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

21 REPLIES 21

Re: Static variables in M15

Unfortunately, no.

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

Re: Static variables in M15

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

Re: Static variables in M15

Maybe this will do what you need.

Re: Static variables in M15

Richard. Hi!

Great method and will work well for me.

Thanks

Martin

Re: Static variables in M15

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

Re: Static variables in M15

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 ???

Re: Static variables in M15

Stuart! Long time no see. I guess you didn't get washed away in the floods then

Neat trick. I can confirm it works in MC15 too.

Re: Static variables in M15

Neat trick. I can confirm it works in MC15 too.

And it even works in Prime 3, too. Just did the conversion.

No clue how it works and whats about the 1*v

Re: Static variables in M15

Hi Richard, Werner,

Fortunately, I live halfway up a hill (I'm applying for a position with the Duke of York) and the water didn't cause me too many problems. The wind did, though - it's costing me a fortune in replacement fence panels, but not, I'm glad to say, solar panels. Lots of impressive flooding for the area and quite a few closed roads, but none that particularly affected me.

I've been quiet on the Mathcad Front because I haven't touched it much in the past year or so - a minor spot of illness, which also meant that I missed out on the Prime 3 testing putting me well behind the drag curve.

I've just dug out Mathcad again to do some data analysis at work. I'm just hoping that this time I don't have redo everything in Excel "because that's the tool that the engineers understand" ... yes, I know, but I've long ceased to wonder at the iniquities of life or puzzle over why people seem to think that a geological hammer is the ideal tool for inserting and removing watch screws. It took me minutes to get back up to speed on Mathcad and I haven't yet come across any other application that requires so little "refresh" time.

I have no idea how the 'undocumented feature' works either. I'm wildly guessing that multiplying by tricks the compiler into picking up the address of the variable the first time it's compiled.

Cheers,

Stuart