Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Function returns null

23 views
Skip to first unread message

Michael Greene

unread,
Nov 7, 2009, 6:45:48 AM11/7/09
to

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


David Bailey

unread,
Nov 8, 2009, 6:49:13 AM11/8/09
to
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

Leonid Shifrin

unread,
Nov 8, 2009, 6:50:08 AM11/8/09
to
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

Bill Rowe

unread,
Nov 8, 2009, 7:05:06 AM11/8/09
to
On 11/7/09 at 6:46 AM, mgr...@csumb.edu (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 *)
>]

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]


Bob Hanlon

unread,
Nov 8, 2009, 7:06:15 AM11/8/09
to
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 <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


0 new messages