Return at the End of Subroutines

gbradley

Lifetime Supporting Member
Join Date
Apr 2002
Location
Corona, Ca.
Posts
1,637
Why is it good practice to put a return at the end of Subroutines?

SubReturn.jpg

Most Ladders will work fine with or without it.
I have to confess that I've omitted the Return on more than a few occasions.
I always put it in now, but I don't really know why.
Thanks
 
gbradley said:
Why is it good practice to put a return at the end of Subroutines?

SubReturn.jpg

Most Ladders will work fine with or without it.
I have to confess that I've omitted the Return on more than a few occasions.
I always put it in now, but I don't really know why.
Thanks


Its only needed if you want to end early and not process the rest of the routine.
 
I only use the RET if I'm passing data back from the sub-routine or, as PeterW stated, return before end.

99.9% of my routines do not have a RET.
 
What processor do you have?
Its only required if you want to return before the end of the subroutine. ( SLC and Micrologix)
In the PLC5 platform its used if you are passing parameters and/or if you want to return before the end of the subroutine.

EDIT: I don't type fast enough.
 
I'd say it's good practice because;
  1. It adds to the documentation. Someone coming behind you may not realize what's been done and chase a problem down a blind alley.
  2. Down the road you might be using another processor or language which requires an explicit RET. If you casually omit it troubles could arise.
.02
 
Doug-P said:
I'd say it's good practice because;It adds to the documentation. Someone coming behind you may not realize what's been done and chase a problem down a blind alley.

I'm sorry, I cannot see any advantage for anyone following to have a RET as an instruction on the last rung. Being as you are on the last rung, it would appear obvious that the routine is ending.


Doug-P said:
Down the road you might be using another processor or language which requires an explicit RET. If you casually omit it troubles could arise.

W
hy program one PLC to the rules of another?.
 
The return marks the end of the subroutine. Some systems demand them, others make them optional. If you include a return in a program, then run it directly (as opposed to calling it as a subroutine) you'll get an error. So it helps distinguish subroutines from parent programs as well.

It's just a good habit, like checking your car's brake lights. Sure, it can run without them, and you might not miss them for a very long time, but it can get you pulled over, and wreak havoc on the guy coming up behind you (figuratively and literally)

TM
 
PeterW said:
Why program one PLC to the rules of another?.
One wouldn't. I'm thinking of the case where you spent some time immersed in programming one PLC which doesn't require RET statements and then go to one which does require them. Human nature and memory being what they are it seems likely one could inadvertently revert to the first case with possible bad results.

YMMV
 
There have been several mentions of possible multiple returns from a single subroutine, but what about multiple subroutines?
Let's say we have a PLC with a structure like -
Code:
-----------------[SBR 01]--
---
---
---
-------------------[RET]---
-----------------[SBR 02]--
---
---
---
-------------------[RET]---
-----------------[SBR 03]--
---
---
---
-------------------[RET]---
Surely if you didn't formally end each sub-routine portion with a corresponding return the processing would just follow with the next rung? If you called subroutine 01 you'd end up processing all three sub's if there were no returns? My very limited experience of subroutines in PLCs makes me puzzled - how can returns be optional as someone suggested? The problem remains if you only have one subroutine, you just don't see the symptoms of it.

Ken
 
Ken,

Your thinking Modicon, in that case you are correct.

With Allen Bradley, a sub-routine is similar to an FC call, when you reach the end of the FC, you don't need to program in a block end unconditional, as the FC will end anyway.
 
...in which case I'm also thinking TI (I think).

Yep....The routines in TI are bracketed by a SBR and a RET. The reason being, the ladder in TI is flat. No seperate program files, it is all contained in one. So, you have to delineate each section or routine with a SBR at the beginning and a RET at the end. Call each with a GTS.
 
The OP did not actually state that he was talking about A-B PLCs. However, it was apparent from the screen shot that it was an A-B.

In this context it is definitely NOT good practice to end every program file that is called with a JSR instruction with a RET instruction.

It's not an optional instruction, it has a specific purpose and it should be used when you need that function (PeterW, gmferg, and Mickey covered when you need it). If you put extra RET instructions in your program (and every one that is at the end of a program file *is* extra), that is dead code and dead code is never "good practice".
 
Yes I was talking about Micrologix/SLC 500.

I kept seeing the RET instruction at the end of subroutine files in example code posted here and elsewhere.

I figured that there was some important reason that the RET should be there, so I started using it in my Code.

What I gather from the posts here is:
An RET instruction right before the End instruction, is redundant and therefore unnecessary.

Thanks for the replies.
George
 

Similar Topics

Just wondering what the consensus is on this, 50% of programs I see, function blocks are ended with a return instruction, though if it wasnt there...
Replies
6
Views
1,987
I have a Type C to RS485 adapter connect to my Siemens RWF55. I use modscan to scan it to get a value from the Siemens controller. In the...
Replies
4
Views
102
This is for editing HMI texts en masse in Excel. My problem is that in my HMI texts, some are in multiple lines, even if they are single texts...
Replies
26
Views
9,140
Hello, I have been tasked to create routines that has a JSR with zero return parameters to go to a routine with a RET with zero parameters. Is...
Replies
2
Views
1,409
Hi all, I have a PIDE block (Logix5000) where I am introducing interlocks. I am using the ProgOverrideReq to set the CV to 0% (shut a control...
Replies
3
Views
1,638
Back
Top Bottom