Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: Function returns null

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104739] Re: [mg104689] Function returns null
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Sun, 8 Nov 2009 06:47:58 -0500 (EST)
  • References: <200911071146.GAA09860@smc.vnet.net>

Hi Michael,

You are obviously trying to use the syntax of some C-like language in
Mathematica code, and that is the main source of confusion.

Specifically, your code returned Null because it was missing a semicolon
after a closing bracket of the <If> statement (indeed, coming from C or Java
placing such a semicolon is the last thing to think about).

A few pointers:

1. All commands (including If, For, other control flow constructs) are
statements in Mathematica. Some of them return nothing - this means they
retun Null.

2. Curly braces mean lists - a most common data structure in Mathematica.
Here they are *not* what they are in C or Java (where they are used to
block-structure the code) - use parentheses instead (not always needed). In
your code, you could have omitted any explicit block-structuring.

Here is a version of your code that works (I didn't get the purpose of the
line sodv = sOd[sodv] in the original version - I guess it's a mistake):

Clear[sOd];
sOd[x0_] :=
 Module[{sodv, x, digit},
  x = IntegerPart[x0];
  sodv = x;
  If[(x > 9),
   sodv = 0;
   While[x > 0, digit = (FractionalPart[x/10])*10;
    sodv = sodv + digit;
    x = IntegerPart[x/10];]];
  Return[sodv]]

Note that in most cases procedural programming is not the best way to code
in Mathematica. Here is how I would code the function you need:

Clear[sOdAlt];
sOdAlt[x_Integer] := Total@IntegerDigits[x];
sOdAlt[x_?NumericQ] := sOdAlt[IntegerPart[x]];

If you plan to do any serious work/programming in Mathematica,
a good time investment would be to have a closer look at the online
documentation, Mathematica book or other resources to get familiar with the
Mathematica way of doing things.

Hope this helps.

Regards,
Leonid


On Sat, Nov 7, 2009 at 3:46 AM, 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: Finding Clusters
  • Previous by thread: Function returns null
  • Next by thread: Re: Function returns null