MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: multiple outputs from a function


Return is almost never needed, certainly not in this kind of function:

stats[x_List] := Module[{local variables},
  (* function body *);
  {mean, variance, std}
];

Bobby

On Mon, 13 Dec 2004 04:23:23 -0500 (EST), David Annetts <davidannetts at ihug.com.au> wrote:

>
>
> Hi Ben,
>
>> 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?
>
> Why not define your function
>
> Needs["Statistics`"] (* to be safe ...*)
> stats[x_List] := Module[
> {local variables},
>
> (* function body *)
>
> Return[{mean, variance, std}];
> ];
>
> Ie. A function that accepts a list and returns another list.  The return
> list has three elements, mean variance & std deviation of the original list.
> Rephrasing this
>
> mean = First[stats[yourList]];
> var = stats[yourList][[2]];
> std = Last[stats[yourList]];
>
> Regards,
>
> Dave.
>



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


  • Prev by Date: Re: Re: How to evaluate Exp[I Pi(1+x)]?
  • Next by Date: Need your help to solve a PDE.
  • Previous by thread: Re: multiple outputs from a function
  • Next by thread: Re: multiple outputs from a function