MathGroup Archive 2009

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

Search the Archive

Re: Function returns null

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104735] Re: Function returns null
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Sun, 8 Nov 2009 06:47:09 -0500 (EST)
  • References: <hd3mlc$9j5$1@smc.vnet.net>

Michael Greene wrote:
> I'm getting an unexpected result when I remove a print statement from my
> function. The function computes a single digit sum of digits and returns
> what I expect as long as the print statement just prior to the function exit
> is present. I can verify that by invoking the function within a print
> statement, e.g.,
> 
> Print["sOd returned - ",sOd[3329]]
> 
> When I remove the print  within the function, the function returns a null
> result. What simple point am I missing ? Does the := operator have something
> to do with this?
> 
> Here is the function: (with embedded print statement)
> 
> sOd[x0_] := Module[{sodv, x, digit},
>   x = IntegerPart[x0]; (* ensure we have an integer input*)
>   sodv = x;
>   If[(x > 9), {              (* sum digits if input has two or more digits*)
>      sodv = 0;
>      While[x > 0,
>       digit = (FractionalPart[x /10])*10;
>       sodv = sodv + digit;
>       x = IntegerPart[x/10];
>       ];
>      sodv = sOd[sodv];  (* check for multiple digit sum  *)
>      }
>     ]
>    Print["sOd returns -", sodv];  (* remove this and function returns null*)
>   Return[sodv];             (* return single digit sum *)
>   ]
> 
> 
> Thanks,
> Michael Greene
> 
> 
On the line above the Print there is the closing ] belonging to an If, 
and this should be followed by a semicolon. Because you omitted the 
semicolon, Mathematica sees the If statement multiplied by the following 
statement - the Print. Without the Print, the If gets multiplied by the 
Return, and stops it working correctly!

David Bailey
http://www.dbaileyconsultancy.co.uk


  • Prev by Date: Re: Strange behavior when using pattern match and HoldAll
  • Next by Date: Re: What is going on!?!
  • Previous by thread: Re: Function returns null
  • Next by thread: Re: Passing function arguments as lists of replacement