Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

I use return on a subroutine

by vroom (His Eminence)
on May 25, 2000 at 18:02 UTC ( [id://14757]=poll: print w/replies, xml ) Need Help??

Vote on this poll

Always
[bar] 93/52%
Never
[bar] 14/8%
When I need it to break out of loop
[bar] 54/30%
Only when my boss makes me
[bar] 7/4%
What's a subroutine?
[bar] 12/7%
180 total votes
Replies are listed 'Best First'.
RE: I use return on a subroutine
by Simplicus (Monk) on May 25, 2000 at 19:44 UTC
    As an old C/C++ coder, I probably use it more as a reflex than anything else. But I almost always use return
    Simplicus
      Yeah. I tend to use it more often then I don't. It makes the code more readable if you explicitly state what you are returning. But if my routine only returns an error state or the return value is irrelevant then I don't use return. (Its not like Perl knows about void functions)
RE: I use return on a subroutine
by Maclir (Curate) on May 29, 2000 at 01:37 UTC
    As a matter of "good programming practice", I always state an explicit return, like "return 1;", or "return $status_code;" to make it easier for the programmers that will follow on from me. As well, when a subroutine is performing a task with a number of tests - say a data validation exercise - having a return after each failure avoids incredible messy code. Ken
RE: I use return on a subroutine
by anders (Initiate) on May 26, 2000 at 22:16 UTC

    Since perl allways returns the value of the last expression, I usually just states $something if the sub's supposed to return some object.

    For subs that prints stuff, I leave it to print() to decide.

    If an error occurs, I use return 0 after setting an error value if possible.

      Interesting... I do it the other way around!
      my $txt2print = MySub('This text') or warn('MySub said 0'); print $txt2print; sub MySub { my $txt = shift; $txt = { do something here } or return(0); return($txt); }
      or is that what you where saying all along?
        Sometimes I do this too. I guess it's just a matter of preference. Does anyone have a reason to use one or the other?
Re: I use return on a subroutine
by iblech (Friar) on Nov 09, 2004 at 15:44 UTC

    Usually, I don't use return for very small subs:

    sub property { $_[0]->{property} }

    I do use it for most larger subs.

RE: I use return on a subroutine
by Jonathan (Curate) on May 25, 2000 at 18:46 UTC
    I use goto instead ;-)

View List Of Past Polls


Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-25 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found