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
David Bailey
http://www.dbaileyconsultancy.co.uk
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
>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 *)
>]
Module returns the result of the last expression which is
Return[sodv];. This is a compound expression equivalent to
Return[sodv];Null which is why you get the Null. Change this to
Return[sodv] and things will behave as you expected.
Also, I note the code is much more complex than need by to
compute the sum of digits in an integer. The following code has
the same functionality as your code:
sOd[x_Integer] := FixedPoint[Total[IntegerDigits@#] &, x]
However, you might use
sOd[x0_] :=
Total[IntegerDigits[IntegerPart[x0]]]
sOd[3329]
17
Bob Hanlon
---- Michael Greene <mgr...@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