Re: multiple outputs from a function
- To: mathgroup at smc.vnet.net
- Subject: [mg52774] Re: [mg52753] multiple outputs from a function
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Mon, 13 Dec 2004 04:22:18 -0500 (EST)
- Reply-to: hanlonr at cox.net
- Sender: owner-wri-mathgroup at wolfram.com
Use a list.
stats[data_] := {mu -> Mean[data], sigma->StandardDeviation[data]}
data=Table[Random[],{100}];
stats[data]
{mu -> 0.5086542300219064, sigma -> 0.2993179460132339}
Bob Hanlon
>
> From: Ben Barrowes <barrowes at alum.mit.edu>
To: mathgroup at smc.vnet.net
> Date: 2004/12/11 Sat AM 05:22:57 EST
> To: mathgroup at smc.vnet.net
> Subject: [mg52774] [mg52753] multiple outputs from a function
>
> 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[67]:=
> t1[x_,y_]:=(a1=x^2+y;a2=x+y^2;x+y)
>
> In[68]:=
> t2=t1[5,6]
>
> Out[68]=
> 11
>
> In[69]:=
> a1
>
> Out[69]=
> 31
>
> In[70]:=
> a2
>
> Out[70]=
> 41
>
> Is this the accepted method for extracting multiple results from one
> function definition?
>
>
> Ben Barrowes
>
>