develooper Front page | perl.beginners | Postings from May 2003

Re: return multiple values from subroutine

Thread Previous | Thread Next
From:
R. Joseph Newton
Date:
May 16, 2003 10:55
Subject:
Re: return multiple values from subroutine
Message ID:
3EC5278D.C3B067C9@efn.org
Joseph Paish wrote:

> i want to return multiple values from a subroutine.  all the examples i have
> seen show just a single value (sometimes an array) being returned, and until
> now i have been creating an array just for this purpose.

I think the reason behind this has to do with the roots of algorithm development
in theoretical math.  Subroutines have long been associated with functions, which
are used in mathematics as conceptual funnels, taking one or more inputs,
performing a single operation, and returning the output of that operation.  This
has some benefits, one of them being that a function in C could be interpolated
directly as a variable.

In the development of Perl, Larry Wall rebelled against the injunction that
functions must always be used in that way.  See the intro to Learning Perl for
his story on this.  Thus Perl does provide facilities for returning an entire
data set from a subprocedure, where that is the natural product of the activity.

A caveat here.  Taking advantage of this Perl feature too much can lead to
importable programming habits, since few other languages support multiple-value
return.  For this reason, I would recommend that you also explore the use of
reference parameters for processes intended to modify multiple values.  Passing
in a reference allows your function to reach out to the calling scope.  This is
the strategy that tends to have more cross-language applicability, IMHO.

Joseph



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About