MathGroup Archive 2004

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

Search the Archive

Re: How to define a routine to return multiple values

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49970] Re: How to define a routine to return multiple values
  • From: Daniel Herring <dherring at at.uiuc.dot.edu>
  • Date: Sun, 8 Aug 2004 05:37:58 -0400 (EDT)
  • References: <cf221u$7p7$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Xiaoxun wrote:
> In Mathematica, how to generate a function to return
> multiple values simutaneously, for example, to return
> a 3 by 3 tensor. Just as subroutine in Fortran..

Returning a list or matrix is quite straightforward.

myFun[]:=
Block[{myList,i,j},
	(* Do stuff *)
	myList=Table[Random[],{i,3},{j,3}];
	Return[myList];
];
Print["Answer: ", MatrixForm[myFun[]]];



Frequently, I want to return several unrelated items.  In these cases, I 
return a list of rules...

myFun[x_]:=
Block[{sgn,myList,i,j},
	sgn=Sign[x];
	myList=Table[Random[],{i,3},{j,3}];
	Return[{VarSign->sgn,MyList->myList}];
];

(* Store and use the results *)
result=myFun[-5];
Print["Sign: ", VarSign/.result];
Print["List: ", MatrixForm[MyList/.result]];


There may be a better way of doing things, but this way is quite powerful.

Later,
Daniel


  • Prev by Date: Re: How to define a routine to return multiple values
  • Next by Date: button to emulate Shift-Enter
  • Previous by thread: Re: How to define a routine to return multiple values
  • Next by thread: NthPermutation newbie problem .. Combinatorica Package