MathGroup Archive 2009

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

Search the Archive

Re: Function returns null

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104728] Re: [mg104689] Function returns null
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Sun, 8 Nov 2009 06:45:38 -0500 (EST)
  • Reply-to: hanlonr at cox.net

If the last thing inside your module is a semi-colon you will get Null.

However, you might use

sOd[x0_] :=
 Total[IntegerDigits[IntegerPart[x0]]]

sOd[3329]

17


Bob Hanlon


---- Michael Greene <mgreene at csumb.edu> 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



  • Prev by Date: Re: What is going on!?!
  • Next by Date: Re: find subsets
  • Previous by thread: Re: Function returns null
  • Next by thread: Re: Function returns null