MathGroup Archive 2004

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

Search the Archive

Re: Re: multiple outputs from a function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg52865] Re: [mg52802] Re: [mg52753] multiple outputs from a function
  • From: DrBob <drbob at bigfoot.com>
  • Date: Wed, 15 Dec 2004 04:26:29 -0500 (EST)
  • References: <!~!UENERkVCMDkAAQACAAAAAAAAAAAAAAAAABgAAAAAAAAAF7cxFMSSe0KJWsUBrjMYtEKCAAAQAAAAhwJyuxYUCkSbKM0krbbaMQEAAAAA@ihug.com.au>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

>> Indeed, as others have pointed out, the entire Module[] can also be avoided.

On the contrary, Module is often totally necessary; it controls scoping so that we can avoid conflicts with Global variables.

But Return is never necessary, and it allows (even encourages) writing spaghetti code with multiple return points. You may avoid that pitfall, but Return is a temptation that will lure others into the trap.

>> As a clear statement that a list (or any other variable) is returned

In any Block or Module I write, it's very easy to pick out the last expression. Nothing can be clearer than that.

Bobby

On Tue, 14 Dec 2004 18:09:49 +1100, David Annetts <davidannetts at ihug.com.au> wrote:

> Insofar as Mathematica is concerned, no, it is not needed.  Indeed, as others have
> pointed out, the entire Module[] can also be avoided.
>
> As a clear statement that a list (or any other variable) is returned from a
> Module[] or Block[], it is invaluable.  In my experience, simple defensive
> things like this save hours (or worse) trying to debug code.  Leave it out
> if you want.
>
> Dave.
>
>> -----Original Message-----
>> From: DrBob [mailto:drbob at bigfoot.com]
To: mathgroup at smc.vnet.net
>> Sent: Tuesday, 14 December 2004 07:55
>> To: David Annetts; mathgroup at smc.vnet.net
>> Subject: [mg52865] Re: [mg52802] Re: [mg52753] 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
>



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


  • Prev by Date: GUIKit - ScrollPane Tables within Wizard
  • Next by Date: Re: Iteration of Initial Conditions for required Boundary Value with NDSolve
  • Previous by thread: Re: multiple outputs from a function
  • Next by thread: Bug in 5.1,Linux, GUIKit?