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

I use return on a subroutine

by vroom (Pope)
on May 25, 2000 at 18:02 UTC ( #14757=poll: print w/ replies, xml ) Need Help??

vote on I use return on a subroutine

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
Comment on I use return on a subroutine
RE: I use return on a subroutine
by Jonathan (Curate) on May 25, 2000 at 18:46 UTC
    I use goto instead ;-)
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 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 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 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.

View List Of Past Polls


Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2014-09-23 05:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls