If values change while in the subroutine

I’m checking to see if values can change while you are in a subroutine or if they will only see the original value ‘passed’ in? In other words can I check to see if a value has changed since I passed it to the subroutine?

I am checking some things to see if they change while the subroutine is running and I want to make sure it will work. If I only get the original value passed it wont work because it wont change, but if that value is able to be updated during the subroutine then it will. If this wont work then maybe I need to do this differently in my strategy not using subroutines…?

Here’s the subroutine:

As long as the parameter passed in was a “variable” and not a “value”, then yes the value of the variable will change for charts running outside the subroutine.

I’m glad you brought this up, since it’s a more subtle point when building subs.

Rule of thumb: make all parameters Literals (vs. Variables) UNLESS you need the value to change, e.g., the subroutine is calculating/returning a value.

Here’s an example with this GetLengthOfMonth sub where I needed it to tell me the number of days in a particular month so I pass which month it is, which year, and I want to get back my answer of: 28,29,30 or 31.

Note (highlighted in blue) the DaysInMonth is the value being returned. (In computer science lingo, we called this passed by reference vs. passed by value.)

This also means, you have more choices when CALLING the subroutine. For example, if I wanted to call this sub to see if this year is a leap year, I could just do this:

GetLengthOfMonth( 2, 2017, nDaysInFeb );

or something like this:

GetLengthOfMonth( nThisMonth, nThisYear, nDaysInFeb );

Note in the first one I’m passing literals (the 2 and the 2017). If I’d made those first two subroutine parameters variables instead of literals, I wouldn’t have a choice, my only option would be to pass in varibles as shown in the second example.

That’s why you want the more flexible option of the Literal (the number or variable is like literal, unchanging, once it gets into the sub) whenever possible. You’re also less likely to inadvertently change something you didn’t mean to change.

I hope that helps!

So my question now will more clarify what you have said I think…As you both have said if it’s passed as a variable it can change for charts ‘running outside’ the subroutine, but the chart that called my subroutine is ‘paused’ when the subroutine is called, so then if try to use that variable that was passed in will it not change because the ‘calling’ chart s paused? Or will it change because the subroutine is ‘looking it up’??

No. You couldn’t use it anyway since the chart is running the subroutine, but when the subroutine exits the strategy variables will be whatever they were left as in the subroutine.

Yes, as Mary posted, strategy variables are passed “by reference” which means the subroutine is changing the same memory location as any other chart that is using that same strategy variable (including the calling chart).

Ok, to make sure I have this strait…if I have two variables in my main strategy called nLeach1On, nLeach2On, nLeach3On…etc. They aret just 0 or 1 values that is changed by my SCADA to tell each leach field to run. If I pass this variable to my subroutine as nLeachControl it will retain that as “reference” to nLeach1On (or other…) and if my SCADA turns nLeach1On to 0, the subroutine will see it (as nLeachControl) change to 0 and quit if I tell it to?

Phillip, I’m just trying to clarify because it sounds like in the first answer you gave me that the answer is no, but the second answer seems like you are saying yes…


Yes if you change nLeach1On in your HMI and that is the value you passed in to your subroutine variable nLeachControl, then the HMI will change nLeachControl.

The first answer I was just saying you wouldn’t be able to use the variable in the calling chart (the actual chart) while in the subroutine - since the chart is effectively paused waiting for the sub to return. Other charts can use them and so can your HMI.

Ok, so I’m in the debugging phase of this project and I went to test the nLeach1On / nLeachCon as I have it programmed to break out of my chart. When I change it on my SCADA it is changing the nLeach1On value, but the nLeachCon value didn’t change while it is still in the subroutine. Here is a screenshot:

So my SCADA changes the nLeach1On Value to 0, but it doesn’t change the value of nLeachCon.

How do I resolve this? I want a variable that can be changed on the fly in the SCADA that will break it out of my loop in the subroutine.

Is the nLeachCon an Integer Variable parameter or an Integer Literal?

It will need to be a variable parameter to do what you want.

I think that is it. I’ll try it out. Thanks!

It won’t let me change it or delete it?

Quite the annoyance. Create a new temporary variable in your subroutine, then do a find and replace from the parameter name to the temporary variable. Then you can edit the parameter (or delete/add). Then find and replace back. Then you can delete the temporary variable.

Thanks for all your help Phillip…Here’s another question. This next one seemed to be working fine yesterday and now it’s not. The only change that I made was the one we just talked about. In this picture I do a move from numeric table element. The current leach line is line 8 which you can see has a 1 as it’s value in the ftLeachTimes chart. It’s supposed to move that 1 into fLeachTime. It was doing this fine yesterday, but now the value of fLeachTime is stuck at 0 and won’t change. The next line multiplies it by 60 to get seconds and it is probably not working obviously because of multiplication by 0.

What does ntLeachLineCon look like?

oh, it’s the one on the bottom left. I think I just figured it out. I was pointing to ntLeachLineCon instead of ftLeachTimes. Both Charts have a 1 for the value though, so I’m not sure why it would work with the float table and not the integer table? They are both a 1 right?

Hard to tell from just the screen shot, but if ntLeachLine[8] was 1 when the “Move form Numeric Table Element” command ran, then fLeachTime would be 1 after that command.

My only idea is that you have another chart or an HMI writing to ntLeachLine or fLeachTime while you are stepping through this chart.