Function returns null
- To: mathgroup at smc.vnet.net
- Subject: [mg104689] Function returns null
- From: Michael Greene <mgreene at csumb.edu>
- Date: Sat, 7 Nov 2009 06:46:14 -0500 (EST)
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
- Follow-Ups:
- Re: Function returns null
- From: Leonid Shifrin <lshifr@gmail.com>
- Re: Function returns null