News:

  • June 26, 2026, 05:20:11 AM

Login with username, password and session length

Author Topic: Subroutines / Recursion  (Read 11510 times)

b_carlton

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 606
    • thePLCguy
Subroutines / Recursion
« on: February 24, 2017, 05:26:48 PM »
Are the variable memory locations chosen as 'internal' to the subroutine separate from similarly names locations in the primary memory block? If not how is recursion possible? I don't think this is explained well enough in the videos.
An output is a PLC's way of getting its inputs to change.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6161
  • Yes Pinky, Do-more will control the world!
Re: Subroutines / Recursion
« Reply #1 on: February 24, 2017, 06:03:15 PM »
For this version of subroutines, everything is global, so recursion is tricky. I'm pretty sure you could rig up something with an external stack, but it wouldn't be as simple as a call stack.

The engine does have a stack and the ability to pass variables on it, but the time to get it done 'right' was more than we had available. We opted to go ahead and do the global approach because it is still useful for many situations.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

b_carlton

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 606
    • thePLCguy
Re: Subroutines / Recursion
« Reply #2 on: February 24, 2017, 07:06:33 PM »
I understand the time constraints.

I understand the stack methodology in C (I never got into C+ or ++ or whatever) for subroutine calls. I was going to try the classic recursion method of generating factorial numbers but couldn't figure out how to generate the local variables.

Why then does the video imply that recursion is possible?
An output is a PLC's way of getting its inputs to change.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3827
    • Host Engineering
Re: Subroutines / Recursion
« Reply #3 on: February 24, 2017, 11:56:16 PM »
Recursion is possible in that Sub A can CALL Sub A, but you must implement your own parameter stack, which can be emulated using arrays.  Note that the video also stated that traversing a directory structure is a recursive operation, but you can't do FILE**** ops in a subroutine anyway.  :-[

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6161
  • Yes Pinky, Do-more will control the world!
Re: Subroutines / Recursion
« Reply #4 on: February 25, 2017, 12:35:37 AM »
As Franj mentioned, and I alluded to, it is entirely possible, just not as easy as using the call stack the way done in C. Just have the subroutine operate on values referenced by array index, then bump the index, stuff new values, and call the subroutine again. Since you are operating on external data, it's not even necessary to 'pass' the parameters, just increment the index, set the associated value, then CALL. Data can be returned in the same stack, and pre or post decremented as required.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

b_carlton

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 606
    • thePLCguy
Re: Subroutines / Recursion
« Reply #5 on: February 25, 2017, 10:10:57 AM »
Got it. Thanks I have a some experimenting to do.
An output is a PLC's way of getting its inputs to change.

Mike Nash

  • Hero Member
  • *****
  • Posts: 652
Re: Subroutines / Recursion
« Reply #6 on: February 26, 2017, 01:57:03 PM »
Sitting here with an internet connection, I just tried out the "Browse Videos" under help in DmD 2.0. I typed in "subroutines" and watched the two videos it found. That's pretty neat. I really didn't expect to care that much for it but I'm glad I tried it.

The video on how to use subroutines gave an interesting example of searching for a file using recursion. I have not thought of anything I need recursion for so far though.

I'm also not so sure subroutines in general are something I will find useful. It's still not an add-on-instruction type of feature. I think I am still wanting a user structure and local variables, etc. To be fair, AOIs in Rockwell can be somewhat circuitous to deal with, especially when trying to find all the references to a tag due to alias/base, local/global, definition/actual usage, etc.

When we get to user structures with multiple data type capability, I could see a bit more use for the subroutines. Then again, there would really need to be some way to monitor the status with only the values from a particular CALL or debug/troubleshooting will be really difficult.

Hmm, I think I am realizing I need to wait for structured UDT and AOI functionality rather than trying to force emulate it.