       Re: multiple outputs from a function

• To: mathgroup at smc.vnet.net
• Subject: [mg52790] Re: [mg52753] multiple outputs from a function
• From: DrBob <drbob at bigfoot.com>
• Date: Mon, 13 Dec 2004 04:22:53 -0500 (EST)
• References: <200412111022.FAA06563@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```A function can't evaluate to more than one value, but it can evaluate to a List of values:

data = RandomArray[BinomialDistribution[12, .1], 12]
stats[s_] := {Mean@s, Variance@s, StandardDeviation@s}
stats@data

{0, 0, 3, 1, 0, 1, 1, 2, 2, 2, 2, 2}
{4/3, 32/33, 4*Sqrt[2/33]}

or

stats[s_] := Through[{Mean, Variance, StandardDeviation}@s]
stats@data

{4/3, 32/33, 4*Sqrt[2/33]}

or

Variance, StandardDeviation} -> Through[{Mean,
Variance, StandardDeviation}@s]]
stats@data
Mean /. %

{Mean -> 4/3, Variance -> 32/33, StandardDeviation -> 4*Sqrt[2/33]}
4/3

Bobby

On Sat, 11 Dec 2004 05:22:57 -0500 (EST), Ben Barrowes <barrowes at alum.mit.edu> wrote:

> I feel I must be missing something fundamental...
>
> How do you write a function (or ?) in Mathematica which produces more
> than one output?
>
> Let's say I have some data and I want a single function to calculate the
> mean, variance, std, etc, more than one thing and return those? I know
> that there are builtin functions for those parameters, but the point is
> I want to define functions with more than one output.
>
> The only examples I can find are along the lines of:
>
> f[x_,y_]:=x^2+y^2;
>
> which have only a single result.
>
> Is there a different structure altogether, such as a Subroutine, which
> allows multiple results from a single subunit?
>
> One thought I had was that because Mathematica treats everything as
> global unless defined specifically local (e.g. in a module), that
> variables used in a procedure would be accessible and would thus be a
> "result", but it seems scoping problems would arise if this was used too
> often. For example:
>
> In:=
> t1[x_,y_]:=(a1=x^2+y;a2=x+y^2;x+y)
>
> In:=
> t2=t1[5,6]
>
> Out=
> 11
>
> In:=
> a1
>
> Out=
> 31
>
> In:=
> a2
>
> Out=
> 41
>
> Is this the accepted method for extracting multiple results from one
> function definition?
>
>
> Ben Barrowes
>
>
>
>

--
DrBob at bigfoot.com
www.eclecticdreams.net

```

• Prev by Date: Graphics Conversion Bug with DensityGraphics
• Next by Date: Re: problem getting the area under a parmetric curve
• Previous by thread: Re: multiple outputs from a function
• Next by thread: Re: multiple outputs from a function