MathGroup Archive 2006

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

Search the Archive

Re: Help with functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg65668] Re: [mg65612] Help with functions
  • From: "David Park" <djmp at earthlink.net>
  • Date: Wed, 12 Apr 2006 06:00:37 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

I always find Return truly enigmatic. The documentation is not at all clear.
Exactly what structure is the Return returning from, where is it returning
to and what is it returning?

You don't really need the function definition. What is x doing? So consider:

Module[{i},
  Do[If[i == 2, Return[False]], {i, 5}];
  Print["hit"];
  Return[True];]
hit
Return[True]

What happened!? Do usually returns Null. With a Return statement evaluated
inside the Do it returns False as the output of the Do statement (not the
Module or function when you had that). The False isn't used for anything so
now the other two statements are evaluated. With the last statement, why
don't we get True out instead of Return[True]?

It is much better to use the Throw-Catch mechanism. Then you know precisely
where the Throw is throwing to and what it is throwing.

Catch[Module[{i},
    Do[If[i == 2, Throw[False]], {i, 5}];
    Throw[True]]]
False

Catch[Module[{i},
    Do[If[i == 7, Throw[False]], {i, 5}];
    Throw[True]]]
True

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/



From: IronBone [mailto:iron.bone at op.pl]
To: mathgroup at smc.vnet.net


I have written a simmple function which (in my opinion) should always
return False. But it returns True.

It seems that Return[False] breaks the Do loop but not returns from the
function.
I thing I don't understand,  what is defining of functions. Can somebody
explain me this.

fun[x_] := Module[{i},
     Do[If[i == 2, Return[False]], {i, 5}];
     Return[True];
     ]


I.B.



  • Prev by Date: Re: Integral problem
  • Next by Date: Re: using Legend package
  • Previous by thread: Re: Help with functions
  • Next by thread: using Legend package